views:

628

answers:

5

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="GeneralButton" 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?

A: 

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.
}
John Allers
I always wondered what NamingContainer was for...
PhilPursglove
NamingContainer works in this situation, at least for my interpretation of the question. Not sure if it's really the best solution. I like your approach better. :)
John Allers
thanks, that's new for me too.
Budda
A: 

I did something with grids and datagrid has a row databound event http://stackoverflow.com/questions/1848631/how-do-i-data-bind-a-drop-down-list-in-a-gridview-from-a-database-table-using-vb/1853267#1853267

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%5Fevents.aspx

ram
A: 

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.

kervin
Didn't know about such approach. Thanks.
Budda
A: 

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);
}
PhilPursglove
That's new for me... Can you please describe how can I cause "ItemCommand" event for a repeater?
Budda
If you mean how you trigger it when your application is running, it should be the event you get when you click on GeneralButton (if I've read an interpreted your markup right). If you mean how you get to it in Visual Studio, I put the markup in then changed to the Design view and double-clicked on GeneralButton to generate the event handler stub.
PhilPursglove
I meant "how to trigger it when application is running". Your answer helped me to understand what I need :)http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcommand.aspxOccurs when a button is clicked in the Repeater controland we can get event sender with: e.CommandSourceThanks!
Budda
A: 

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.

Budda