Get the current index in an ASP.Net 2.0 Repeater control

It may be very easy to do, but so far it has been taking me all day for something to work.

I have a repeater populated with a table. Each line in the repeater has a set of controls. The most important of these is a drop-down list with AutoPostback = true.

This ddl should postback when the user changes the selected index so that I can hide / show the controls in ddl.

The problem is that when the user changes the selected index in ddl and the server-side postback, I cannot get the index of the row containing the ddl that made the postback.

Hope this is clear enough and that someone can help me here.

Thanks!

EDIT: Maybe this is something that was not clear enough: when I put the control with ID = "ddlSomething" inside the ItemTemplate in the repeater and I have 5 lines in the data source, I will have 5 lines in the repeater with 5 ddl with the same identifier (on the server side, on the client side there will be something like "ctl01 $ ddlSomething", "ctl02 $ ddlSomething". My problem is that when returning ddl I do not know which of these 5 (for example) ddl is the one that postback did, because just looking at the Request.Form variables, I see that the ddlSomething control is it.

+3
source share
5 answers

Try:

<asp:DropDownList runat="server" id="myDDL" OnSelectedIndexChanged="myDDL_Changed" />

//fired when the DDL selected index changes
void myDDL_Changed(object sender, EventArgs e)
{
    //sender is the ddl
    DropDownList theDropDown = sender as DropDownList;
    int repeaterItemIndex = ((RepeaterItem)theDropDown.NamingContainer).ItemIndex;
}
+5

FormView , , :

HTML DDL id DDL:

MyID='<%# Eval("MyID") %>'

postback :

int intID = Convert.ToInt32((sender as DropDownList).Attributes["MyID"]);
.....

EDIT: Employees with employee_name, employee_type employee_id , , , - DLL , .. DDL:

MyID='<%# Eval("employee_id") %>' 

DDL:

int employeeID = Convert.ToInt32((sender as DropDownList).Attributes["MyID"]);
+1

, , .

, , - :

<asp:Repeater id="myRepeater" runat="server">
    <ItemTemplate>
        <asp:DropDownList ID="ddlSomething" AutoPostBack="true" runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

, "OnItemCommand", :

<asp:Repeater id="myRepeater" OnItemCommand="SomeEvent_ItemCommand" runat="server">
        <ItemTemplate>
            <asp:DropDownList ID="ddlSomething" AutoPostBack="true" runat="server"></asp:DropDownList>
        </ItemTemplate>
    </asp:Repeater>

:

protected void SomeEvent_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandSource.GetType() == typeof(DropDownList))
            {
                DropDownList ddlSomething = (DropDownList)e.Item.FindControl("ddlSomething");

                //Now you can access your list that fired the event
                SomeStaticClass.Update(ddlSomething.SelectedIndex);
            }
        }

, , , - , .

EDIT:

, "e.Item" - , . , , .

+1

, , , ""

. , , .

, . , DDL, :

   for( ... )
   {
       if( containingElement.Controls.Find(sender.ID) )
      { 
         // You found it!
      }
      // otherwise keep looking...
   }
0

, SelectedIndexChanged DropDownList :

Dim ddl = DirectCast(sender, DropDownList)
Dim lbl = DirectCast(DirectCast(ddl.NamingContainer,RepeaterItem).FindControl("Label1"),Label)

.

0

Source: https://habr.com/ru/post/1716518/


All Articles