2

I cannot get my SelectedIndexChanged of my dropdownlist to fire. I have the following:

<form id="form1" runat="server">
<div>
<asp:GridView id="grdPoll" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" 
                 AutoPostBack="true"
                 OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
                    <asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

<asp:Label ID="lblCity" runat="server" Text="Label"></asp:Label>  
</div>
</form>

In my code behind I have this:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}

If I put this same ddl outside of the gridview, it fires.

The postback is occurring and the autopostback is set to true. The event just never fires. Why can't I get my event to fire from within the gridview?

Thank you.

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
johnny
  • 19,272
  • 52
  • 157
  • 259

6 Answers6

7

Well, this question was asked more than a month ago and may be irrelevant now, but @LFSR was kind enough to edit it recently, it's in the "Active questions" list.

Since it remains unanswered (224 views!), I thought I should give it a go:


The problem is that in the context of a GridView, the DropDownList(referred to hereafter as DDL) is a dynamic control and therefore its events need to be reattached upon Postback.

When this concept is understood, the solution becomes relatively simple :

ASPX:

<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DDL1_SelectedIndexChanged">
  <asp:ListItem Text="Review" Value="Review" Selected="True">Review</asp:ListItem>
  <asp:ListItem Text="Level1" Value="lvl1">Send Back to Level1</asp:ListItem>
</asp:DropDownList>

CS Code:

protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
  {
    // Bind the GridView to something.
    DataBindGrid();
  }
}

protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
  this.lblCity.Text = ((DropDownList)sender).SelectedValue;
}

protected void grdPoll_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(Page.IsPostBack)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      DropDownList ddl = e.Row.FindControl("DDL1") as DropDownList;
      if(ddl != null)
      {
        ddl.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
      }
    }
  }
}
Cerebrus
  • 25,615
  • 8
  • 56
  • 70
  • Thank you. I eventually had to change to another way of doing things altogether that didn't require this anymore but I know I'll need it in the future. Thank you again. – johnny Mar 12 '09 at 13:50
  • Unfortunately, even if it makes some sense, it didn't work for me, and here we are more than 13 years later with the same problem, so I don't think it's a bug, there must be something wrong we are doing, I whish I knew what though, but I tried all possible solutions found on internet and none worked for me, I had to use javascript, bummer.. – Santos Aug 07 '22 at 16:51
1

Watch out when you do the databinding. I had same problem on a test page. The DD selectedIndex event would not fire. It turns out I was rebinding the gridview on every page serve which effectively kills the event. As soon as I only bound only on page.ispostback=false the events fired as expected and was picked up by the generic DD hander. From there you can iterate the gridview clientIDs of your DDs to find the one that matches sender.clientID in the generic handler.

cbeckner
  • 1,808
  • 15
  • 17
computski
  • 11
  • 1
1

I found attaching an event handler inside ItemDataBound didn't work; also the DropDownList doesn't have CommandName or CommandArgument properties, so the grid's ItemCommand event won't fire in response to a dropdown selected index change.

However, you can do this in Page_Load to determine what caused the post: -

        var target = Request.Form["__EVENTTARGET"];

        if (!string.IsNullOrEmpty(target) && target.Contains("cboAttribute"))
        {
            var cboAttribute = Page.FindControl(target) as DropDownList;

            if (cboAttribute != null)
            {
                // this one fired the event
            }
        }

My grid contains a heap of dropdowns called cboAttribute, and this gives me a reference to the one that caused the post.

sh1rts
  • 1,874
  • 1
  • 13
  • 14
0

I encountered a similar problem with a combobox in a grid not firing. In my case the reason that the method wasn't firing was because there was an item that needed to be validated that was hidden. So make sure to check that you don't have any hidden validators that could be firing and preventing the combobox from executing.

0

Well, that did not fix the problem for me. I must say that this was working correctly on my box even without having to wire the event in the RowDataBound. That's not the only problem the gridview seem to have right now, even datakey collections disappear, I think this grid is only designed to work with the asp.net datasources to act correctly. I'll rewrite my code to work with an object data source to validate my suspicion.

  • You did not get back with more feedback but I do not think your suspicion that the GridView works only with DataSource controls is valid at all. I have used it with many kinds of IEnumerable structures. – Cerebrus Apr 14 '09 at 16:27
0

I cannot explain why, but I experience the same behavior when I dynamically add ListItem's to the DropDown. Only useful if you don't need the ListItem value.

Try adding the string value instead of a ListItem:

For example:

//  change this
DDL1.Items.Add(new ListItem("Review","Review"));

// To this
DDL1.Items.Add("Review");
Cerebrus
  • 25,615
  • 8
  • 56
  • 70
  • @Jeff: Not sure what your point is and how it is relevant to the question that was asked, but the above code should theoretically have the same effect. – Cerebrus Apr 14 '09 at 16:29