<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="False" AllowSorting="true" AllowPaging="true" PageSize="10" ShowFooter="true" OnPageIndexChanging="GridView_PageIndexChanging" OnRowDataBound="gridMember_RowDataBound" >
        <RowStyle CssClass="GridViewRowStyle" />
        <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle"/>
        <HeaderStyle CssClass="GridViewHeaderStyle"/>
        <FooterStyle CssClass="GridViewFooterStyle" />
        <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
        <Columns>
            <asp:TemplateField HeaderText="编号">
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                <ItemTemplate>
                    <asp:Label ID="Nids" runat="server" Text='<%# Bind("Nid")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField  HeaderText="当前状态" SortExpression="state">
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="60px" /> 
                <ItemTemplate>
                        <asp:HiddenField runat="server" ID="hidtxt" Value='<%# Bind("state")%>' />
                        <asp:HiddenField runat="server" ID="nId" Value='<%# Bind("Nid")%>' />
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" BackColor="Yellow" ForeColor="DarkGreen" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
                        <asp:ListItem Value="0">正常显示</asp:ListItem>
                        <asp:ListItem Value="1">推荐显示</asp:ListItem>
                        <asp:ListItem Value="2">新内容</asp:ListItem>
                        <asp:ListItem Value="3">标记删除</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
                <EditItemTemplate>
                    &nbsp;
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
        <PagerTemplate>
                    <div style=" font-size:12px;">
                        第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />页
                        共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页 
                        <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
                        <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
                        <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                          
                        <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" /> 
                     </div>                                              
         </PagerTemplate>    </asp:GridView>后台
   
 protected void gridMember_RowDataBound(object sender, GridViewRowEventArgs e)
    {        if ((DropDownList)e.Row.FindControl("DropDownList1")!=null)
        {
            DropDownList drp=(DropDownList)e.Row.FindControl("DropDownList1");
            drp.SelectedValue = ((HiddenField)e.Row.FindControl("hidtxt")).Value.ToString();
            //drp.SelectedValue = "1";
        }
    }
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList drp = sender as DropDownList;
        GridViewRow row = drp.NamingContainer as GridViewRow;
        row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
        Response.Write(String.Format("选中第 {0} 行", row.RowIndex + 1));
        Label lbl = row.FindControl("Nids") as Label;
        Response.Write(lbl.Text);
            
    }问题:当选择DropDownList触发SelectedIndexChanged, 跟踪程序第一次触发你所选的ListItem没问题,但接着再选另一个ListItem则把上一次选择再触发一遍然后再执行这次触发,这样依次选择触发 但是程序总是执行你所有已经执行了的触发事件 不知道是怎么回事,而且翻页也会使页面所有DropDownList自动触发SelectedIndexChanged  请各位帮忙 有解决办法吗?谢谢!

解决方案 »

  1.   

    原因是因为:
     protected void gridMember_RowDataBound(object sender, GridViewRowEventArgs e)
        {        if ((DropDownList)e.Row.FindControl("DropDownList1")!=null)
            {
                DropDownList drp=(DropDownList)e.Row.FindControl("DropDownList1");
                drp.SelectedValue = ((HiddenField)e.Row.FindControl("hidtxt")).Value.ToString();
                //drp.SelectedValue = "1";
            }
        }这个方法里面,你给DropDownList赋值了,这样它肯定会触发那事件了
      

  2.   

    drp.SelectedValue 你给DropDownList赋值以后,就会认为有人做了SelectedIndexChanged事件
    所以会触发可以SelectedIndexChanged判断你的RowDataBound事件给DropDownList是否真的改变了值.
    如果不改变,就不要执行里面的内容.
      

  3.   

    主要是在RowDataBound里改动了ddl
      

  4.   

    RowDataBound的绑定是在翻页的时候 对DropDownList1_SelectedIndexChanged有影响。 DropDownList的连续触发是怎么回事呢? 有处理的办法吗?