|
Subject:
|
DropDown Within Repeater
|
|
Posted By:
|
ninel
|
Post Date:
|
4/16/2008 11:26:48 AM
|
I have a dropdown inside a repeater control that I cannot for the life of me figure out how to populate. There are also a couple of other fields displayed in the repeater. These are bound by a datareader. Although I would like to bind that dropdown from a value in the datareader right now I'd like to just understand how to bind the dropdown to some values I want to set on the onload event.
This is what I have so far:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border='0' width='100%' cellspacing='0' cellpadding='2'
style='background:#DDDDDD; border-top: black 0.3pt solid; border-left: black 0.3pt solid;
border-bottom: black 0.3pt solid; border-right: black 0.3pt solid'>
<tr>
<th align='left' class='TableHeader'> </th>
<th align='left' class='TableHeader'> </th>
<th align='left' class='TableHeader'>Question</th>
<th align='left' class='TableHeader'>Type</th>
<th align='left' class='TableHeader'>Presentation</th>
<th align='left' class='TableHeader'>Ordinal Number</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
<td class='BaseText' style="width:30px"><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
<td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
<td class='BaseText'><asp:DropDownList ID="ddlOrdinal" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="300px" ></asp:DropDownList></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor='#ffffff'>
<td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
<td class='BaseText' style="width:30px" ><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
<td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
<td class='BaseText'><asp:DropDownList ID="ddlOrdinal" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="300px" ></asp:DropDownList></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I read somewhere that I have to use the ItemCreated event of the repeater, but it's not working. What am I doing wrong?:
Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlOrdinal"), DropDownList)
'objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(e.Item.DataItem.ToString))
objDDL.Items.Add(New ListItem("SELECT", "-1"))
objDDL.Items.Add(New ListItem("1", "1"))
objDDL.Items.Add(New ListItem("2", "2"))
objDDL.Items.Add(New ListItem("3", "3"))
objDDL.Items.Add(New ListItem("4", "4"))
objDDL.Items.Add(New ListItem("5", "5"))
objDDL.Items.Add(New ListItem("6", "6"))
objDDL.Items.Add(New ListItem("7", "7"))
objDDL.Items.Add(New ListItem("2", "2"))
End If
End Sub
Thanks, Ninel
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 12:30:33 PM
|
Have you tried hooking up the event handler to the event like this:
Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemCreated
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 12:50:25 PM
|
I actually got the dropdown to populate by adding OnItemCreated="Repeater1_ItemCreated" like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemCreated="Repeater1_ItemCreated">
Now my problem is capturing the value of the dropdown when an item is selected. Any ideas?
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 1:21:46 PM
|
You could do that as well, but the "VB style" is to use the Handles keyword.
You could hook up an handler to the SelectedIndexChanged event of the drop down and then retrieve the value by casting the Sender to a DropDownList:
Dim myList As DropDownList = CType(sender, DropDownList) ...
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 1:43:36 PM
|
Thank you Imar for your help. I got that part figured out as well.
Now what I am trying to do is remove the hardcoded values that I used to populate the dropdown and populate it with values from a stored procedure. The stored proc that I am currently using populates the other items in the repeater, but also returns the values I need for the dropdown. Can I use that same stored proc or do I need to create a new one that returns values for the dropdown only?
Thank you.
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 1:53:27 PM
|
It all depends on your setup and personal preference. I prefer to have stored procedures that target one scenario, so you may be better off creating a new procedure that gets the relevant items.
Imar --------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 2:07:56 PM
|
So I created a new stored proc that returns the values of the ID and values of field OrdinalNumber and I set it like this:
Dim dtr As SqlDataReader = objCommand.ExecuteReader
objDDL.DataSource = dtr
objDDL.DataBind()
What should the dropdown look like? This is what I have:
<asp:DropDownList ID="ddlOrdinal" autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="100px" ></asp:DropDownList>
Is this correct?
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 2:11:57 PM
|
Does it work? Then I guess it is correct....
In other words: impossible for me to say without a description of the situation and relevant sample code....
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 2:30:16 PM
|
No, it doesn't work. Here's the code:
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
<HeaderTemplate>
<table border='0' width='100%' cellspacing='0' cellpadding='2'
style='background:#DDDDDD; border-top: black 0.3pt solid; border-left: black 0.3pt solid;
border-bottom: black 0.3pt solid; border-right: black 0.3pt solid'>
<tr>
<th align='left' class='TableHeader'> </th>
<th align='left' class='TableHeader'> </th>
<th align='left' class='TableHeader'>Question</th>
<th align='left' class='TableHeader'>Type</th>
<th align='left' class='TableHeader'>Presentation</th>
<th align='left' class='TableHeader'>Ordinal Number</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
<td class='BaseText' style="width:30px"><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
<td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
<td class='BaseText'><asp:DropDownList ID="ddlOrdinal" autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="100px" ></asp:DropDownList></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor='#ffffff'>
<td class='BaseText' style="width:20px"><asp:CheckBox ID="CheckBox1" runat="server" /><asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%#DataBinder.Eval(container.DataItem , "ID")%>'></asp:TextBox></td>
<td class='BaseText' style="width:30px" ><a href ='MapResponses.aspx?ProjectID=<%=ProjectID%>&SurveyID=<%=SurveyID2%>&QuestionID=<%#DataBinder.Eval(container.DataItem , "ID")%>' class="BaseText">Edit</a></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "ShortQuestion")%></td>
<td class='BaseText'><%#DataBinder.Eval(container.DataItem , "TypeDescription")%></td>
<td class='BaseText'><%#DataBinder.Eval(Container.DataItem, "PresentationDescr")%></td>
<td class='BaseText'><asp:DropDownList ID="ddlOrdinal" autopostback = "true" DataTextField="OrdinalNumber" DataValueField="ID" runat="server" width="100px" ></asp:DropDownList></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Sub Repeater1_ItemCreated(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlOrdinal"), DropDownList)
objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(e.Item.DataItem.ToString))
Dim objConn As nsSqlClient.SqlConnection
Dim returnValue As Int32
Dim objCommand As nsSqlClient.SqlCommand
objConn = New nsSqlClient.SqlConnection
objConn.ConnectionString = m_strConnection
objConn.Open()
objCommand = New nsSqlClient.SqlCommand("rspGetOrdinalAllQuestionsBySurvey", objConn)
objCommand.CommandType = nsData.CommandType.StoredProcedure
objCommand.CommandTimeout = 1320
'Creating parameter for Procedure
objCommand.Parameters.Add(CreateParam("@RETURN_VALUE", nsData.SqlDbType.Int, 0, nsData.ParameterDirection.ReturnValue, returnValue))
objCommand.Parameters.Add(CreateParam("@ClientID", nsData.SqlDbType.VarChar, 10, nsData.ParameterDirection.Input, ClientID))
objCommand.Parameters.Add(CreateParam("@SurveyID", nsData.SqlDbType.VarChar, 20, nsData.ParameterDirection.Input, surveyID))
objCommand.ExecuteNonQuery()
returnValue = DirectCast(objCommand.Parameters("@RETURN_VALUE").Value, Int32)
If returnValue <> 0 Then
Exit Sub
Else
Dim dtr As SqlDataReader = objCommand.ExecuteReader
'Repeater1.DataSource = dtr
'Repeater1.DataBind()
objDDL.DataSource = dtr
objDDL.DataBind()
End If
AddHandler objDDL.SelectedIndexChanged, AddressOf ddlordinal_SelectedIndexChanged
End If
end sub
The stored proc returns the following: ID OrdinalNumber --> This is the text I want to see in the dropdown 140 NULL 103 1 5 1 4 4 100 6
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 2:56:36 PM
|
And what exactly isn't working?
This would be so much easier if you:
a) Posted only the relevant pieces of code. No need to post long blocks of HeaderTemplates for example
b) Added a few line breaks in your code so the code doesn't get 10 feet wide forcing me to scroll in all directions to see what's going on.
c) Added a specific description of "does not work", including any error messages you get, the values that your variables contain during debugging and so on.
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 3:01:44 PM
|
I'm sorry.. The repeater loads with the correct amount of records. Each record needs to have a dropdown associated with it which I am using the separate stored proc to populate. The dropdowns are empty when the page loads. Thats the only issue.
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/16/2008 3:10:54 PM
|
So many more questions to be asked and answered....
Did you debug the code? What happens when you debug it? Is Repeater1_ItemCreated being triggered? Does e.Item.FindControl("ddlOrdinal") result in a valid control reference? Does objDDL have a valid selected value? Is your data access code firing correctly? Is the right data passed to the database? Is the right data returned from the database?
And so on and so forth....
In other words: impossible for me to answer without relevant information.
My advice: set a breakpoint in Repeater1_ItemCreated (press F9), debug the application (press F5) and see how it behaves. Step through the code line by line and see what happens....
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|
|
Reply By:
|
ninel
|
Reply Date:
|
4/16/2008 3:58:33 PM
|
I did debug through the code. WHen I get to objDDL.DataSource = dtr I placed the mouse over the dtr and it showed hasrows = true, fieldcount=2, recordsaffected =-1. Is there anyway I can display the result of the datareader before I set it to the dropdown to make sure data is returned?
I am not getting any errors, the dropdowns are all just completely empty.
|
|
Reply By:
|
Imar
|
Reply Date:
|
4/17/2008 8:39:27 AM
|
If HasRows is true, it's safe to say the reader has data. So, the problem must be elsewhere.
Is the code triggered for the right ItemTypes? Is the code called too often so you override the exiting items with an empty list? Is objDDL holding a reference to the correct DropDownList? What happens when you manually add an item to the DropDownList directly after the call to DataBind? Does that item end up in the list?
Again, since I don't have the whole code, can't see what you've posted because the page is way too wide to read and can't try out your code as I don't have your database, I can only ask questions, not recommend a true fix.
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004 Want to be my colleague? Then check out this post.
|