DropDownList inside Repeater: how to handle SelectedIndexChange and get DataItem?

I put a DropDownList using an AutoPostBack inside a repeater.
(ListItems are populated by the ItemDataBound repeater)

<asp:Repeater ID="rptWishlist" OnItemCommand="rptWishlist_ItemCommand" onItemDataBound="rptWishlist_ItemDataBound" runat="server"> <ItemTemplate> ... <asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSize_SelectedIndexChanged" /> ... 
  • Firstly, this function was not even launched after the message

    protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
    {
    //This function is never called
    }

  • How can I get a DataItem after I earn it?

Am I doing it wrong?

Thanks in advance.

+6
source share
5 answers

To register a drop-down list for postback, add the following code:

  protected virtual void RepeaterItemCreated(object sender, RepeaterItemEventArgs e) { DropDownList MyList = (DropDownList)e.Item.FindControl("ddlSize"); MyList.SelectedIndexChanged += ddlSize_SelectedIndexChanged; } 

And in your aspx file, add this to the repeater markup:

 OnItemCreated="RepeaterItemCreated" 

Then, in your ddlSize_SelectedIndexChanged function, you can access the parent control as follows:

  DropDownList d = (DropDownList)sender; (RepeaterItem) d.Parent... 

Hope this helps.

+12
source

I do not see a problem with part of your code.

Do you call DataBind () on your repeater when IsPostBack is true, but during PageLoad? If so, you will lose SelectedIndexChanged on DDLs

You must store identifiers, for example, in values ​​or HiddenField, to load certain DataItems during reverse processing (ListView has a DataKey for this purpose)

As a general guide:

  • it is often better to call DataBind () during PreRender
  • you should not call DataBind () during postback if the underlying data has not changed
  • if you do these two points above, you cannot use the DataItems in item_created (since your DataItems will only be available when calling DataBind ())

     protected void Page_Load(object sender, EventArgs e) { this.PreRender += new EventHandler(test_PreRender); } void test_PreRender(object sender, EventArgs e) { if (!IsPostBack) { rptWishlist.DataSource = new int[] { 1, 2, 3, 4 }; rptWishlist.DataBind(); } } protected void rptWishlist_ItemCommand(object sender, RepeaterCommandEventArgs e) { //Command Code Here } protected void rptWishlist_ItemDataBound(object sender, RepeaterItemEventArgs e) { var i = (int) e.Item.DataItem; var ddl = (DropDownList)e.Item.FindControl("ddlSize"); for(int j=1; j<=i;j++) { ddl.Items.Add(new ListItem(){Text = j.ToString()}); } } protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e) { Response.Write("changed"); } 
+2
source

If you just want to run OnSelectedIndexChanged, this is how it should look:

Page.aspx - Source

 <FooterTemplate> <asp:DropDownList ID="ddlOptions" runat="server" AutoPostBack="true" onselectedindexchanged="ddlOptions_SelectedIndexChanged"> <asp:ListItem>Option1</asp:ListItem> <asp:ListItem>Option2</asp:ListItem> </asp:DropDownList> </FooterTemplate> 

Page.aspx.cs - Code for

 protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e) { //Event Code here. } 

What is it. Your event will be called now.

+1
source

The answer here is good, but there is no critical check. If you are wondering why you get a reference to an object that is not installed on an instance of object errors, it is important to note that the relay will first create its HEADER before any data elements.

Perform this check:

 protected void rptProjects_ItemCreated(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { ((DropDownList)e.Item.FindControl("yourcontrol")).SelectedIndexChanged += ddlAction_SelectedIndexChanged; } } 
+1
source

try it

 DropDownList drp = sender as DropDownList; int RepeaterItemIndex = ((RepeaterItem)drp.NamingContainer).ItemIndex; 
-1
source

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


All Articles