[英]avoid postback dynamically created checkbox in asp.net c#
在一個Web表單中,我動態創建了一系列chekbox,它們被添加到asp:panel中,而該面板位於更新面板中。
我的代碼段:
Aspx代碼:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlCuisine" runat="server"></asp:Panel>
</ContentTemplate>
C#代碼:
public static CheckBox[] chkbx = new CheckBox[15];
public static Label[] lblCuisine = new Label[15];
private void LoadCuisine(string searchStr)
{
.....
.....
.....
int i = 1;
foreach(var item in cuisineList)
{
chkbx[i]=new CheckBox();
chkbx[i].ID = item.CategoryName;
chkbx[i].Text = item.CategoryName;
chkbx[i].AutoPostBack = true;
lblCuisine[i] = new Label();
lblCuisine[i].ID = "lblCuisine" + Convert.ToString(i);
lblCuisine[i].Text = "(" + item.Count + ")";
chkbx[i].CheckedChanged += new EventHandler(chkbx_CheckedChanged);
pnlCuisine.Controls.Add(chkbx[i]);
pnlCuisine.Controls.Add(lblCuisine[i]);
pnlCuisine.Controls.Add(new LiteralControl("<br/>"));
i++;
}
}
void chkbx_CheckedChanged(object sender, EventArgs e)
{
string searchString = ((CheckBox)sender).ClientID;
populateGrid(searchString);
}
請更改此行代碼
chkbx[i].AutoPostBack = true;
至
chkbx[i].AutoPostBack = false;
那么您可以避免在復選框已更改的事件觸發時自動回發
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.