点击复选框会导致onCheckedChanged开火两次 [英] Clicking checkbox causes onCheckedChanged to fire twice

查看:156
本文介绍了点击复选框会导致onCheckedChanged开火两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框一个TemplateField的GridView。我的目标是使用自动回发,并设置数据库标志捕捉onclick事件。我唯一​​的问题是,事件火的两倍。第一次的复选框(在发送参数)持有点击的价值,所以我将它基于点击。第二次sender参数都具有始终检查=假的复选框。我很高兴地招待接近解决这一问题上的其他建议,但我的目标是基于用户勾选复选框来设置数据库标志。我针对的.NET Framework 2.0。

下面是相关的code:

 < D​​IV的风格=保证金左:1EM;保证金右:1EM;>
    < ASP:GridView控件ID =RouteGridView=服务器AllowPaging =真
            的AutoGenerateColumns =FALSE的cellpadding =4的DataKeyNames =ROUTE_NUMBER
            前景色=#333333网格=无的风格=宽度:100%;
        onselectedindexchanged =RouteGridView_SelectedIndexChanged
        AllowSorting =真onpageindexchanging =RouteGridView_PageIndexChanging
        onsorting =RouteGridView_Sorting>
            <柱体和GT;
<% - 一列 - %GT;
< ASP:的TemplateField的HeaderText =路由SORTEX pression =ROUTE_NUMBER>
    <&ItemTemplate中GT;
        < ASP:LinkBut​​ton的ID =HyperLink1=服务器的CommandName =选择CommandArgument ='<%#的eval(ROUTE_NUMBER)%>'
                            文字='<%#的eval(ROUTE_NUMBER)%>' >< / ASP:LinkBut​​ton的>
    < / ItemTemplate中>
    < ItemStyle Horizo​​ntalAlign =中心/>
< / ASP:的TemplateField><% - 第2列,这是哪里出了问题复选框 - %GT;
< ASP:的TemplateField的HeaderText =阅读?
    SORTEX pression =READ_FLAG>
    <&ItemTemplate中GT;
        < ASP:复选框ID =CheckBox1=服务器
            OnCheckedChanged =ChangeReadFlag
            的AutoPostBack =真
            选中='<%#(字符串)的DataBinder.Eval(的Container.DataItem,READ_FLAG)==1%GT;'启用='<%#isSelectedRow(集装箱)%GT;' />
    < / ItemTemplate中>
    < ItemStyle Horizo​​ntalAlign =中心/>
< / ASP:的TemplateField><% - 更多的列 - %
<% - 更多的列 - %GT;
< /专栏>
< / ASP:GridView的>

下面是从code背后的事件处理程序:

 保护无效ChangeReadFlag(对象发件人,EventArgs的发送)
{
    如果(RouteGridView.SelectedIndex!= -1)
    {
        复选框CB =((复选框)发送者);
        DataKey键= RouteGridView.SelectedDataKey;        // ...做的东西在这里...
    }
}


解决方案

有可能有几个原因,这样的行为。在我的情况是,事件被注册两次:
作为复选框定义结果的一部分自动一次
< ASP:复选框ID =CheckBox1=服务器
            ** OnCheckedChanged =ChangeReadFlag**
            的AutoPostBack =真
            选中='<%#(字符串)的DataBinder.Eval(的Container.DataItem,READ_FLAG)==1%GT;'启用='<%#isSelectedRow(集装箱)%GT;' />

和第二次 - 一个明确的定位介于code,通常在OnInit方法:结果
CheckBox1.CheckedChanged + =新的EventHandler(ChangeReadFlag);

为了解决这个问题,您应该从后面或从控制code删除第二个注册。

I have a GridView with a TemplateField with a checkbox. My goal is to capture the onclick event using autopostback and setting a database flag. My only problem is that the event fire's twice. The first time The Checkbox (In the sender parameter) holds the clicked value so I set it based on the click. The second time the sender parameter has a checkbox that is always checked=false. I am happy to entertain suggestions on other approached to solving this problem but my goal is to set a database flag based on the user checking a checkbox. I am targeting .NET Framework 2.0.

Here is the associated code:

<div style="margin-left : 1em;margin-right:1em;">
    <asp:GridView ID="RouteGridView" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ROUTE_NUMBER" 
            ForeColor="#333333" GridLines="None" style="width:100%;" 
        onselectedindexchanged="RouteGridView_SelectedIndexChanged" 
        AllowSorting="True" onpageindexchanging="RouteGridView_PageIndexChanging" 
        onsorting="RouteGridView_Sorting" >
            <Columns>
<%-- Column one --%>
<asp:TemplateField HeaderText="Route" SortExpression="ROUTE_NUMBER">
    <ItemTemplate>
        <asp:LinkButton ID="HyperLink1" runat="server" CommandName="Select" CommandArgument='<%#Eval("ROUTE_NUMBER")%>'  
                            Text='<%# Eval("ROUTE_NUMBER") %>' ></asp:LinkButton>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<%-- Column 2 this is where the problem CheckBox is--%>
<asp:TemplateField HeaderText="Read?" 
    SortExpression="READ_FLAG">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" 
            OnCheckedChanged="ChangeReadFlag"  
            AutoPostBack="true"
            Checked='<%# (string)DataBinder.Eval(Container.DataItem, "READ_FLAG") == "1" %>' Enabled='<%# isSelectedRow(Container)  %>' />
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<%-- more columns --%
<%-- more columns --%>
</Columns>
</asp:GridView>

Here is the event handler from the code behind:

protected void ChangeReadFlag(object sender, EventArgs e)
{
    if (RouteGridView.SelectedIndex != -1)
    {
        CheckBox cb = ((CheckBox)sender);
        DataKey key = RouteGridView.SelectedDataKey;

        //... do stuff here ...
    }
}

解决方案

There could be several reasons for such behavior. In my case it was that the event was being registered twice: one time automatically as a part of the checkbox definition
<asp:CheckBox ID="CheckBox1" runat="server" **OnCheckedChanged="ChangeReadFlag"** AutoPostBack="true" Checked='<%# (string)DataBinder.Eval(Container.DataItem, "READ_FLAG") == "1" %>' Enabled='<%# isSelectedRow(Container) %>' />

and the second time - an explicit registration somewhere in code, usually in OnInit method:
CheckBox1.CheckedChanged += new EventHandler(ChangeReadFlag);

In order to fix it, you should remove the second registration either from the code behind or from your control.

这篇关于点击复选框会导致onCheckedChanged开火两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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