未触发CheckedChanged事件 [英] CheckedChanged event is not fired
问题描述
在我的 WebForm
中,我希望用户检查一种产品时更改总价,但是我的代码存在此问题
In my WebForm
I want the total price changed when the user check one product but I have this problem with my code ,
当我检查 CheckBox
仅当我单击 Button
(用于清除按钮)时才会触发,并且我没有在button事件中包含该代码!
It is firing only when I click the Button
(used for as clear button) , and I didn't include that code within the button event !
这是我的代码:
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
属性为 false
,因此您应设置 在您的 CheckBox
中:
All ASP.NET Server controls except Button
, Hyperlink
and LinkButton
have a default AutoPostBack
property of false
, So you should set AutoPostBack="true"
in your CheckBox
:
<asp:CheckBox ID="ckb1" runat="server" AutoPostBack="true" OnCheckedChanged="ckb1_CheckedChanged" />
仅当我单击按钮时才会触发
It is firing only when I click the button
正如我所说,这是因为默认情况下, Button
具有 true
的 AutoPostBack
属性,因此在您选中 CheckBox
,然后单击按钮, CheckBox
状态自动回发到服务器.
As I said this is because the Button
have AutoPostBack
property of true
by default so after you checked the CheckBox
and then click the button the CheckBox
state automatically posts back to the server.
这篇关于未触发CheckedChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!