为什么SelectedIndexChanged事件从GridView中的下拉列表中触发? [英] Why isn't the SelectedIndexChanged event firing from a dropdownlist in a GridView?

查看:153
本文介绍了为什么SelectedIndexChanged事件从GridView中的下拉列表中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的SelectedIndexChanged我的下拉列表触发。我有以下内容:

 < form id =form1runat =server> 
< div>
< asp:GridView id =grdPollrunat =serverAutoGenerateColumns =false>
<列>
< asp:TemplateField>
< ItemTemplate>
< asp:DropDownList ID =DropDownList1runat =server
AutoPostBack =true
OnSelectedIndexChanged =DropDownList1_SelectedIndexChanged>
< asp:ListItem Text =ReviewValue =ReviewSelected =True> Review< / asp:ListItem>
< asp:ListItem Text =Level1Value =lvl1>发送回Level1< / asp:ListItem>
< / asp:DropDownList>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>

< asp:Label ID =lblCityrunat =serverText =Label>< / asp:Label>
< / div>
< / form>

在我的代码背后我有这样的:

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

如果我把这个相同的ddl放在gridview外面,它会触发。 p>

回发正在发生,并且autopostback设置为true。事件永远不会触发。为什么我不能让我的事件从gridview中触发?



谢谢。

解决方案

嗯,这个问题是在一个多月前被问到的,现在可能是无关紧要的,但是@LFSR最近很好地编辑它,它在活动问题列表中。



由于它仍然没有得到答复(224次观看),我以为我应该去一个:






问题是在GridView的上下文中,DropDownList(以下称为DDL)是一个动态控件,因此其事件需要在Postback上重新连接。



当这个概念被理解时,解决方案变得相对简单:



ASPX:

 < asp:DropDownList ID =DDL1runat =serverAutoPostBack =trueOnSelectedIndexChanged =DDL1_SelectedIndexChanged> 
< asp:ListItem Text =ReviewValue =ReviewSelected =True> Review< / asp:ListItem>
< asp:ListItem Text =Level1Value =lvl1>发送回Level1< / asp:ListItem>
< / asp:DropDownList>

CS代码:

  protected void Page_Load(object sender,EventArgs e)
{
if(!Page.IsPostBack)
{
//将GridView绑定到某个东西。
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)作为DropDownList;
if(ddl!= null)
{
ddl.SelectedIndexChanged + = new EventHandler(DDL1_SelectedIndexChanged);
}
}
}
}


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.

解决方案

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

这篇关于为什么SelectedIndexChanged事件从GridView中的下拉列表中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆