在我的WebForm中,我希望当用户检查一个产品时总价格发生变化,但是我的代码有这个问题,
当我检查CheckedChanged事件时,CheckBox事件不会触发
只有当我单击Button(用于清除按钮)时,它才会启动,而且我没有在按钮事件中包含该代码!
这是我的代码:
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;
}
}发布于 2015-09-15 19:44:11
所有ASP.NET服务器控件除了Button、Hyperlink和LinkButton都具有false的默认AutoPostBack属性,因此您应该在CheckBox中设置AutoPostBack="true"
<asp:CheckBox ID="ckb1" runat="server" AutoPostBack="true" OnCheckedChanged="ckb1_CheckedChanged" />只有当我单击按钮时,它才会启动。
如前所述,这是因为默认情况下,Button具有AutoPostBack属性true,所以在您选中CheckBox之后,单击按钮,CheckBox状态将自动回发到服务器。
https://stackoverflow.com/questions/32594278
复制相似问题