c# – 未触发CheckedChanged事件

c# – 未触发CheckedChanged事件,第1张

概述在我的WebForm中,我希望在用户检查一个产品时总价格发生变化,但我的代码存在此问题, 检查CheckBox时,CheckedChanged事件没有触发 它只在我单击按钮(用作清除按钮)时触发,并且我没有在按钮事件中包含该代码! 这是我的代码: public partial class _Default : System.Web.UI.Page{ int total = 0; 在我的WebForm中,我希望在用户检查一个产品时总价格发生变化,但我的代码存在此问题,

检查CheckBox时,CheckedChanged事件没有触发

它只在我单击按钮(用作清除按钮)时触发,并且我没有在按钮事件中包含该代码!

这是我的代码:

public partial class _Default : System.Web.UI.Page{    int total = 0;    String strtotal;    protected voID ckb1_CheckedChanged(object sender,EventArgs e)    {                    if (ckb1.Checked)        {                            total = total + 100;            strtotal = total.ToString();            lbl2.Text = strtotal;        }     }    protected voID ckb2_CheckedChanged(object sender,EventArgs e)    {        if (ckb2.Checked)        {            total = total + 80;            strtotal = total.ToString();            lbl2.Text = strtotal;        }    }    protected voID ckb3_CheckedChanged(object sender,EventArgs e)    {        if (ckb3.Checked)        {            total = total + 70;            strtotal = total.ToString();            lbl2.Text = strtotal;        }    }    protected voID button3_Click(object sender,EventArgs e)    {        TextBox1.Text = " ";        ckb1.Checked = false;        ckb2.Checked = false;        ckb3.Checked = false;        }}
解决方法 除button,Hyperlink和linkBut​​ton之外的所有ASP.NET Server控件都具有默认的AutopostBack属性,因此您应该在CheckBox中设置 AutoPostBack="true"
<asp:CheckBox ID="ckb1" runat="server" AutopostBack="true" OnCheckedChanged="ckb1_CheckedChanged" />

It is firing only when I click the button

正如我所说,这是因为默认情况下button的AutopostBack属性为true,因此在您选中CheckBox然后单击按钮后,CheckBox状态会自动回发到服务器.

总结

以上是内存溢出为你收集整理的c# – 未触发CheckedChanged事件全部内容,希望文章能够帮你解决c# – 未触发CheckedChanged事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1252305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存