views:

6002

answers:

3

Hi, 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.

+4  A: 

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
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
Most welcome, Johnny! Glad to assist. :-)
Cerebrus
A: 

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
A: 

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