单击复选框会导致 onCheckedChanged 触发两次

发布于 2024-07-11 17:37:16 字数 2211 浏览 11 评论 0原文

我有一个带有带有复选框的 TemplateField 的 GridView 。 我的目标是使用自动回发并设置数据库标志来捕获 onclick 事件。 我唯一的问题是事件发生了两次。 第一次复选框(在发送者参数中)保存单击的值,因此我根据单击设置它。 第二次sender参数有一个复选框,总是checked=false。 我很高兴接受有关解决此问题的其他方法的建议,但我的目标是根据用户选中复选框来设置数据库标志。 我的目标是.NET Framework 2.0。

这是相关的代码:

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

这是后面代码中的事件处理程序:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怼怹恏 2024-07-18 17:37:16

您是否在页面加载事件中将复选框的选中状态设置为 false ?

Do you set the checked status of the checkbox to false at all in your page load event?

感性 2024-07-18 17:37:16

造成这种行为的原因可能有多种。 就我而言,该事件被注册了两次:
一次自动作为复选框定义的一部分

<asp:CheckBox ID="CheckBox1" runat="server" 
    OnCheckedChanged="ChangeReadFlag"
    AutoPostBack="true"
    Checked='<%# (string)DataBinder.Eval(Container.DataItem, "READ_FLAG") == "1" %>'
    Enabled='<%# isSelectedRow(Container)  %>' />

,第二次 - 在代码中的某处显式注册,通常在 OnInit 方法中:
CheckBox1.CheckedChanged += new EventHandler(ChangeReadFlag);

为了修复此问题,您应该从后面的代码或控件中删除第二个注册。

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.

世俗缘 2024-07-18 17:37:16

您想要捕获 GridView 的偶数; 识别导致回发的命令(发送者),然后您可以执行数据库更新。

You want to capture the even of the GridView; identify the command (sender) that is causing the postback, and then you can execute your DB update.

乱了心跳 2024-07-18 17:37:16

问题出现的原因之一(可能还有其他原因)是事件被注册了两次。

检查“InitializeComponent”后面的代码中的代码。 这将导致事件被注册两次,从而触发两次。

您需要在一处将其删除才能解决问题。

内存。

One of the reasons(there may be others) the problem comes is the event is registered twice.

Check your Code in the code behind, inside "InitializeComponent". This will cause the event to be registered twice and hence fire twice.

You need to remove it at one place to solve the issue.

rAm.

春花秋月 2024-07-18 17:37:16

另一个潜在的问题是数据绑定发生的位置 - 我在前面的代码中没有看到数据源,所以我猜您是在后面的代码中进行数据绑定。

如果您在回发时执行数据绑定,并且在第一个 onChange 事件触发后,数据绑定事件很可能会重置复选框的状态,从而导致该事件再次触发。

Another potential problem is where your databinding is happening - I don't see a DataSource in your code-in-front, so I guess you're databinding in the code-behind.

If you are doing the databind on postback, and after the first onChange event has fired, it's quite likely that the databind event is reseting the checkbox's status, and so causing the event to fire again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文