检查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和linkButton之外的所有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事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)