|
-
Aug 2nd, 2011, 03:48 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Data bind DropDownList inside a repeater contol
I am trying to bind a DropDownList inside a repeater control to an existing data set (to be able to edit the data) and I have been searching for an answer to this problem for a day and a half now without any luck. Can anyone point me in the direction of a clear example of how to do this? (Half the time, if you don't know exactly what you are looking for you may find it but never recognize the fact that you have )
Or if you know how to do this and can provide some directions that would probably be cool too.
Thanks
NOTE: Solution is found in post #13
Last edited by Working.Net; Aug 3rd, 2011 at 03:30 PM.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 07:08 AM
#2
Fanatic Member
Re: Data bind DropDownList inside a repeater contol
You haven't posted much information here so I may be misunderstanding your problem.
An example of the ItemDataBound event for a repeater: http://www.codeguru.com/csharp/.net/...cle.php/c12065
You can databind the DropDownList in the ItemDataBound event of the repeater.
-
Aug 3rd, 2011, 10:01 AM
#3
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
 Originally Posted by x-ice
You haven't posted much information here so I may be misunderstanding your problem.
A valid point x-ice, so I'll give you what I got, and then some more on the bottom. First the ASP for the repeater control:
Code:
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<table class="formTbl" border="0" cellspacing="2px">
<colgroup>
<col width="75px" />
<col width="75px" />
<col width="50px" />
<col width="60px" />
<col width="60px" />
<col width="125px" />
<col width="100px" />
</colgroup>
<tr>
<td class="top" align="center" colspan="7">Sanitary Pipes Cleaned</td>
</tr>
<tr class="grayBack" valign="bottom">
<td align="center">From MH</td>
<td align="center">To MH</td>
<td align="center">Basin</td>
<td align="right">Footage</td>
<td align="center">Size</td>
<td align="center">WorkType</td>
<td align="center">Times Jetted</td>
</tr>
<tr>
<td colspan="7">
<hr />
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:Label ID="From_MH" runat="server" Text='<%# Eval("UP_MH_NO")%>'></asp:Label>
</td>
<td align="center">
<asp:Label ID="To_MH" runat="server" Text='<%# Eval("DN_MH_NO")%>'></asp:Label>
</td>
<td align="center">
<asp:Label ID="Basin" runat="server" Text='<%# Eval("BASIN")%>'></asp:Label>
</td>
<td align="right">
<asp:Label ID="Asbuilt_Length" runat="server" Text='<%# Eval("ASBUILT_LENGTH")%>'></asp:Label>
</td>
<td align="right">
<asp:Label ID="Diameter" runat="server" Text='<%# Eval("DIAMETER")%>'></asp:Label>
</td>
<td align="center">
<asp:DropDownList ID="lstWorkType" runat="server"
AutoPostBack="True" width="125px">
<asp:ListItem Selected="True">Maintenance</asp:ListItem>
<asp:ListItem>Grease</asp:ListItem>
<asp:ListItem>Root Cutting</asp:ListItem>
<asp:ListItem>Televising</asp:ListItem>
<asp:ListItem>Trouble</asp:ListItem>
</asp:DropDownList>
</td>
<td align="center">
<asp:DropDownList ID="lstTimesJetted" runat="server"
AutoPostBack="True" width="50px"
OnSelectedIndexChanged="lstTimesJetted_SelectedIndexChanged">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr >
<td align="center">Comment:</td>
<td colspan="5">
<asp:TextBox ID="txtComment" runat="server" Text='<%# Eval("Comment")%>' Width="370px"></asp:TextBox>
</td>
<td >
<asp:Label ID="PipeID" runat="server" Text='<%# Eval("MAIN_ID")%>' Visible="false" ></asp:Label>
</td>
</tr>
<tr visible="false" >
<td >
<asp:Label ID="lblWorkType" runat="server" Text='<%# Eval("WorkType")%>'></asp:Label>
</td>
<td >
<asp:Label ID="lblTimesJetted" runat="server" Text='<%# Eval("TimesJetted")%>'></asp:Label>
</td>
<td colspan="5">
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<td colspan="7">
<hr />
</td>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code behind:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim vCleaningId As String
If Not IsPostBack Then
vCleaningId = Request.QueryString("CLEANINGID")
' Assign the data source GetSanMainRecord to Repeater1 control and DataBind
' GetSanCleaningSegmentRecords() returns a datatabel
Repeater1.DataSource = GetSanCleaningSegmentRecords(vCleaningId)
Repeater1.DataBind()
End If
End Sub
Sub Repeater1_ItemDataBound(ByVal Sender As Object, _
ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim vRepeaterItem As RepeaterItem = e.Item
Dim vLstWorkType As DropDownList
Dim vLstTimesJetted As DropDownList
Dim vLblWorkType As Label
Dim vLblTimesJetted As Label
vLstWorkType = DirectCast(vRepeaterItem.FindControl("lstWorkType"), DropDownList)
vLblWorkType = DirectCast(vRepeaterItem.FindControl("lblWorkType"), Label)
vLstTimesJetted = DirectCast(vRepeaterItem.FindControl("lstTimesJetted"), DropDownList)
vLblTimesJetted = DirectCast(vRepeaterItem.FindControl("lblTimesJetted"), Label)
' At this point it was my intend to call SetRepeaterDropDownList to
' determine and set the recorded value but when I tested for the
' listbox it did not yet exist as the exceptions would have me believe.
' I think there is supposed to be a way to directly bind the
' DropDdownBox to the data but I could not figure out what
' they were doing in the examples.
End Sub
Protected Sub SetRepeaterDropDownList(ByVal vDropDownList As DropDownList, ByVal vStoredVal As String)
' Check each item in lstOperators to see if it matches the stored value
For i As Integer = 0 To vDropDownList.Items.Count - 1
'If lstOperators.Items(i).Selected = True Then
If vDropDownList.Items(i).Text = vStoredVal Then
' If the stored value matches the item in the list then set
' the selected attribute of the dropdown list item to true.
vDropDownList.Items(i).Selected = True
End If
Next
End Sub
I looked at the example you provided, and their first instruction:
Code:
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
rptData.ItemDataBound +=
new RepeaterItemEventHandler(rptData_ItemDataBound);
}
Which would translate into:
Protected Overrides Sub OnInit(ByVal e As EventArgs)
MyBase.OnInit(e)
Repeater1.ItemDataBound (*Error 1) += _
New RepeaterItemEventHandler(Repeater1_ItemDataBound (*Error 2))
End Sub
Gives me the following errors:
Error 1: 'Public Event ItemDataBound(sender As Object, e As
System.Web.UI.WebControls.RepeaterItemEventArgs)' is an event, and
cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Error 2: Delegate 'System.Web.UI.WebControls.RepeaterItemEventHandler'
requires an 'AddressOf' expression or lambda expression as the only
argument to its constructor.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 12:40 PM
#4
Frenzied Member
Re: Data bind DropDownList inside a repeater contol
Try this:
Inside the repeater control
Code:
<FooterTemplate>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="field1" DataValueField="field1">
</asp:DropDownList>
<br />
</td>
</tr>
</FooterTemplate>
For the data source, you could either do it on the aspx page itself or programmatically in the code behind. My example will be done on the aspx page.
Code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SampleDbConnectionString %>"
SelectCommand="SELECT [field1] FROM [sampletable]"></asp:SqlDataSource>
-
Aug 3rd, 2011, 12:53 PM
#5
Frenzied Member
Re: Data bind DropDownList inside a repeater contol
Here's how I would bind a dropdownlist during runtime:
Code:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SampleDbConnectionString"].ToString());
string sSQL = "Select name1, age1 from sampletable";
SqlCommand cmd = new SqlCommand(sSQL, con);
con.Open();
SqlDataReader dtrClient = cmd.ExecuteReader();
DropDownList ddl = (DropDownList)myRepeater.Controls[myRepeater.Controls.Count - 1].FindControl("myDropDownList");
ddl.DataSource = dtrClient;
ddl.DataTextField = "name1";
ddl.DataValueField = "name1";
ddl.DataBind();
Last edited by benmartin101; Aug 3rd, 2011 at 01:01 PM.
-
Aug 3rd, 2011, 01:18 PM
#6
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
benmartin, can you explain what you are doing in post #5, I don't quite understand? As far as I can tell you are returning the second to the last (count -1) control from the controls contained within the repeater. Then from the returned control you are finding (findcontrol) the control named "lstWorkType"
Also, binding the data to the labels and textboxes works OK. Just don't know how to bind the data/display the data in the DropDownBoxes.
Tricky, editing these posts at the same time. I will give what you suggest in 5 a try. Do I do that in the Repeater_ItemDatBound event?
Last edited by Working.Net; Aug 3rd, 2011 at 01:25 PM.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 01:32 PM
#7
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
OK, I see you are creating and binding the DropDownBox "list" items. That is not the issue here. Sorry if I wasn't clear. I am opening two related tables with a one to many relationship for editing in a form. The repeater displays the "many" part of the relationship. In short I want to display all the records related to the main record, and allow the user to edit them. So the problem is how to display the contents of the fields WorkType and TimesJetted in their corresponding DropDownBoxes.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 01:52 PM
#8
Frenzied Member
Re: Data bind DropDownList inside a repeater contol
 Originally Posted by Working.Net
OK, I see you are creating and binding the DropDownBox "list" items. That is not the issue here. Sorry if I wasn't clear. I am opening two related tables with a one to many relationship for editing in a form. The repeater displays the "many" part of the relationship. In short I want to display all the records related to the main record, and allow the user to edit them. So the problem is how to display the contents of the fields WorkType and TimesJetted in their corresponding DropDownBoxes.
I'm a bit confused. So isn't binding basically what you are looking for? Maybe you could give an example of what a user could do with the page, and what you expect would happen.
-
Aug 3rd, 2011, 02:02 PM
#9
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
Thanks ben for looking into this. When you are dealing with DropDownBoxes there are two types of data binding that go on. First the kind that populates the list from which the user selects the value that they want stored in the database. The second type of databinding consists of binding the control to the field in the database where the selected value gets stored. When you databind a DropDownBox as you have shown in post #5 the default value is displayed. What I am trying to do is display the data from an existing record. So somehow I have to get the DropDownList to "default" (for lack of a better term) to the value that is coming from the WorkType and TimesJetted fields of the current record.
I did discover that you have to filter for the appropriate ListItemType, in this case ListItemType.Item or ListItemType. AlternatingItem
Like so:
Code:
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
' Do something with the controls here
End If

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 02:36 PM
#10
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
Take a look at this:
Code:
Sub Repeater1_ItemDataBound(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim vRepeaterItem As RepeaterItem = e.Item
Dim vLstTimesJetted As DropDownList
Dim vLblTimesJetted As Label
vLstTimesJetted = DirectCast(vRepeaterItem.FindControl("lstTimesJetted"), DropDownList)
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
vLblTimesJetted = DirectCast(vRepeaterItem.FindControl("lblTimesJetted"), Label)
vLstTimesJetted.ClearSelection()
For i As Integer = 0 To vLstTimesJetted.Items.Count - 1
If vLstTimesJetted.Items(i).Text = vLblTimesJetted.Text Then
' If the stored value matches the item in the list then set
' the selected attribute of the dropdown list item to true.
vLstTimesJetted.Items(i).Selected = True
End If
Next
End If
End Sub
Pretty exciting, we're making progress This sort of works. I tested the values, and the comparisons work (If vLstTimesJetted.Items(i).Text = vLblTimesJetted.Text Then) But I get the following error:
An unhandled exception was generated during the execution of the current web request.
Information regarding the origin and location of the exception can be identified
using the exception stack trace below.
Stack Trace:
[HttpException (0x80004005): Cannot have multiple items selected in a DropDownList.]
Which makes me think that I am refering to the same vLstTimesJetted DropDownBox, and not the unique one (as in for each record in the repeater) Any thoughts?
NEVER MIND, HA HA, That is what you get when you don't clear the DropDownBox before setting other items.selected = true. (see blue line in code above) Let me clean this up before I post the final solution. benmartin, thanks for your encouragement and insights
Last edited by Working.Net; Aug 3rd, 2011 at 02:44 PM.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 02:54 PM
#11
Frenzied Member
Re: Data bind DropDownList inside a repeater contol
But could you give me an example of what happens when a user works with it? Like, like say, user clicks a button, then....what happens with the controls (ie. labels, dropdownlist). What values end up showing?
On your code regarding "Repeater1_ItemDataBound" (I'm assuming this is an event), are you now able to make that whole function work since you can get the dropdownlist control? From my understanding, the SetRepeaterDropDownList() will basically set the selected item based on whether it matches the "vStoredVal". Is that working for you now? If not, why?
edit: this was sent before I could see your last reply
-
Aug 3rd, 2011, 03:03 PM
#12
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
Hours of frustration, but then when I finally get to the answers I need to solve the problem and things start falling into place it is all woth it. Ok, maybe not quite like that, but it is cause to celebrate in any event. All in a good day's work.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 03:17 PM
#13
Thread Starter
Hyperactive Member
Re: Data bind DropDownList inside a repeater contol
Here is the final solution. The ASP code hasn't changed so you can find that in post #3. I am binding the data in the page_load event (also in post #3) using a function that passes a datatable. The code for the Repeater's ItemDataBound event:
Code:
Sub Repeater1_ItemDataBound(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim vRepeaterItem As RepeaterItem = e.Item
Dim vLstWorkType As DropDownList
Dim vLstTimesJetted As DropDownList
Dim vLblWorkType As Label
Dim vLblTimesJetted As Label
vLstWorkType = DirectCast(vRepeaterItem.FindControl("lstWorkType"), DropDownList)
vLstTimesJetted = DirectCast(vRepeaterItem.FindControl("lstTimesJetted"), DropDownList)
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
vLblWorkType = DirectCast(vRepeaterItem.FindControl("lblWorkType"), Label)
vLblTimesJetted = DirectCast(vRepeaterItem.FindControl("lblTimesJetted"), Label)
SetRepeaterDropDownList(vLstWorkType, vLblWorkType.Text)
SetRepeaterDropDownList(vLstTimesJetted, vLblTimesJetted.Text)
End If
End Sub
Protected Sub SetRepeaterDropDownList(ByVal vDropDownList As DropDownList, ByVal vStoredVal As String)
' Clear current DropDownBox selection
vDropDownList.ClearSelection()
' For each item in the passed DropDownList
For i As Integer = 0 To vDropDownList.Items.Count - 1
' Compare the item's value to the passed value
If vDropDownList.Items(i).Text = vStoredVal Then
' If it matches set the selected attribute of the dropdown list to true.
vDropDownList.Items(i).Selected = True
End If
Next
End Sub
As a side note and possible continuing discussion, I personally feel that this would be considered a work-around. You should be able to specify that the DropDownBoxes are bound to their respective fields right in the ASP. Thus when you bind the repeater to its data source the DropDownBoxes show their correct values without having to do all this mucking about in the repeater's ItemDataBound event. If I get some good suggestions on this issue I will test them and post the results. For now this thread is done.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 3rd, 2011, 03:25 PM
#14
Frenzied Member
Re: Data bind DropDownList inside a repeater contol
To be honest, i'm still a bit confused. But as long as you got it working.
-
Aug 3rd, 2011, 03:28 PM
#15
Thread Starter
Hyperactive Member
Re: [RESOLVED] Data bind DropDownList inside a repeater contol
Which part do you find confusing?

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 4th, 2011, 01:49 AM
#16
Re: Data bind DropDownList inside a repeater contol
 Originally Posted by Working.Net
Thus when you bind the repeater to its data source the DropDownBoxes show their correct values without having to do all this mucking about in the repeater's ItemDataBound event. If I get some good suggestions on this issue I will test them and post the results. For now this thread is done. 
I would argue that this "mucking" about is actually the correct way to do it.
IMO, adding "eval" and "bind" into your ASPX markup is the wrong approach, as you are mixing the UI portion of the work with the data that you are binding to, and this isn't always the best approach.
Gary
-
Aug 4th, 2011, 07:53 AM
#17
Thread Starter
Hyperactive Member
Re: [RESOLVED] Data bind DropDownList inside a repeater contol
Thanks Gary,
It is good to know that you are doing the right thing, especially when it seems like you're just "mucking" about.

As I stand here, on the fringes of my understanding, and look out over the
vast void before me, I realize all that lies ahead: The rest of Dot.Net. . .
-
Aug 5th, 2011, 01:13 PM
#18
Frenzied Member
Re: [RESOLVED] Data bind DropDownList inside a repeater contol
I kinda got interested in doing this using in a more "binding" way. I figured creating you're own DropDownList would do the trick.
Here's my code for the class:
Code:
public class MyDropDownList : DropDownList
{
private static int index = 0;
public MyDropDownList()
{
}
//override the OnDataBound event so after data has been bound,
//you can then set the selected index to the next one using
//a static integer variable
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
this.SelectedIndex = index;
index++;
}
}
Then on your aspx page, you would add this code inside your Repeater:
Code:
<cc1:MyDropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="field1" AutoPostBack="true">
</cc1:MyDropDownList>
Btw, I finally understood what you were trying to do.
edit: maybe this idea was already mentioned in earlier posts, but i think it's a cleaner way of doing it.
Last edited by benmartin101; Aug 5th, 2011 at 01:23 PM.
-
Aug 5th, 2011, 05:11 PM
#19
Frenzied Member
Re: [RESOLVED] Data bind DropDownList inside a repeater contol
 Originally Posted by benmartin101
I kinda got interested in doing this using in a more "binding" way. I figured creating you're own DropDownList would do the trick.
Here's my code for the class:
Code:
public class MyDropDownList : DropDownList
{
private static int index = 0;
public MyDropDownList()
{
}
//override the OnDataBound event so after data has been bound,
//you can then set the selected index to the next one using
//a static integer variable
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
this.SelectedIndex = index;
index++;
}
}
Then on your aspx page, you would add this code inside your Repeater:
Code:
<cc1:MyDropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="field1" AutoPostBack="true">
</cc1:MyDropDownList>
Btw, I finally understood what you were trying to do.
edit: maybe this idea was already mentioned in earlier posts, but i think it's a cleaner way of doing it.
edit: Nevermind, currently, this code is no good. The static variable will cause an issue.
-
Aug 5th, 2011, 05:59 PM
#20
Frenzied Member
Re: [RESOLVED] Data bind DropDownList inside a repeater contol
I figured out a fix. Just add this code in the class:
Code:
public class MyDropDownList : DropDownList
{
private static int index = 0;
public MyDropDownList()
{
}
//override the OnDataBound event so after data has been bound,
//you can then set the selected index to the next one using
//a static integer variable
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
this.SelectedIndex = index;
index++;
}
//add this
protected override void OnUnload(EventArgs e)
{
index=0;
base.OnUnload(e);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|