Getting DropDownList values in a repeater
There is a following mark-up code:
ASPX Page
<asp:Repeater ID="GeneralRepeater" runat="server"
OnItemDataBound="GeneralRepeater_OnItemDataBound">
<ItemTemplate>
<tr>
<td>
Place:
<asp:DropDownList ID="GeneralDDL" DataTextField="Text"
DataValueField="Arena" runat="server" />
</td>
<th>
<asp:Button ID="GeneralBu开发者_开发技巧tton" runat="server"
Text="Принять запрос" onclick="GeneralButton_Click" />
</th>
</tr>
</ItemTemplate>
</asp:Repeater>
Code-behind
protected void GeneralRepeater_OnItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList myDDL = (DropDownList)e.Item.FindControl("GeneralDDL");
myDDL.DataSource = lstArenaSelect;
myDDL.DataBind();
MyObject obj= (MyObject)e.Item.DataItem;
Button GeneralButton = (Button)e.Item.FindControl("GeneralButton");
AcceptGeneralRequestButton.CommandArgument = obj.Id;
}
}
This shows the initialization of each DropDownList
with a list of objects, and each button in the row linking to the row object.
In the GeneralButton_Click
method I can get ID
of the object bound to the repeater.
Question
How do I get value from the DropDownList
that is located in the same repeater row?
Thanks for all, I've used another approach:
Control parent = ((Control)sender).Parent;
DropDownList GeneralDDL = (DropDownList)parent.FindControl("GeneralDDL");
Code is called in the OnClick button event handler.
Maybe something like this would work:
protected void GeneralButton_Click(object sender, EventArgs e)
{
Button myGeneralButton = (Button)sender;
DropDownList myDDL = (DropDownList) myGeneralButton.NamingContainer.FindControl("GeneralDDL");
// myDDL.SelectValue should be what you are looking for.
}
Use the 'Items' member and the supplied item index.
See... http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.items.aspx for examples.
Basically...
DropDownList currDDL = GeneralRepeater.Items[currButtonItemIndex].FindControl('GeneralDDL') as DropDownList;
or
DropDownList currDDL = GeneralRepeater.Items[e.Item.ItemIndex].FindControl('GeneralDDL') as DropDownList;
In the case of an event handler.
PS. If you can, use a ListView instead of a repeater.
I think what you want is to get the repeater row from the RepeaterCommandEventArgs:
protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
DropDownList myDDL;
myDDL = (DropDownList) e.Item.FindControl("GeneralDDL");
System.Diagnostics.Debug.WriteLine(myDDL.SelectedValue);
}
I did something with grids and datagrid has a row databound event How do I data bind a drop down list in a gridview from a database table using VB?
if you are using ItemDataBound event on the repeater you can also get the index using e.Item.ItemIndex
I do not think repeater has a row databound event though.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx
精彩评论