Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...