johnny
johnny

Reputation: 19755

Why isn't the SelectedIndexChanged event firing from a dropdownlist in a GridView?

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.

Upvotes: 2

Views: 24295

Answers (6)

user3731495
user3731495

Reputation: 9

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.

Upvotes: 0

computski
computski

Reputation: 11

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.

Upvotes: 1

sh1rts
sh1rts

Reputation: 1884

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.

Upvotes: 1

Jeff
Jeff

Reputation:

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");

Upvotes: 0

Kehinde Adewusi
Kehinde Adewusi

Reputation:

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.

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

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);
      }
    }
  }
}

Upvotes: 7

Related Questions