动态创建的复选框上的事件 asp.net
Event on dynamically created checkbox asp.net
我开始使用 asp.net 进行编程,我有一个带有一些复选框的 table。
问题是,我无法创建静态 tables,因为此操作与某些参数相关联。无论如何.. 当我点击第一个复选框时,我想反转这个 table 中的其他复选框。
我怎样才能赶上这个活动?
<%@ Page Title="Fragebogen generieren" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Generate.aspx.cs" Inherits="MAXXREC.Generate" SmartNavigation="True" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2><br /> <br />
<asp:Panel id="pCustomize" runat="server"></asp:Panel> <br /><br />
<asp:Button id="btnSave" class="btn btn-default" Text="Save" runat="server" OnClick="btnSave_Click"></asp:Button>
</asp:Content>
private bool SelectTheData()
{
dtQuestionBlock = taQuestionBlock.GetData();
try
{
int rows = dtQuestionBlock.Rows.Count;
for (int i = 0; i < rows; i++)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ID = "up" + dtQuestionBlock.Rows[i][1].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dtQuestionBlock.Rows[i][1].ToString();
lbl.CssClass = "h4";
lbl.Attributes.Add("runat", "server");
lbl.Text = dtQuestionBlock.Rows[i][1].ToString();
pCustomize.Controls.Add(lbl);
pCustomize.Controls.Add(new Literal() { ID = "br" + i, Text = "<br /><br />" });
HtmlTable tbl = new HtmlTable();
tbl.Width = "100%";
tbl.Attributes.Add("class", "table");
tbl.ID = "htmltbl" + dtQuestionBlock.Rows[i][1].ToString();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell hcella = new HtmlTableCell();
CheckBox acb = new CheckBox();
acb.ID = "cb" + dtQuestionBlock.Rows[i]["name"].ToString();
//acb.CheckedChanged += new EventHandler(cb_CheckedChanged);
hcella.Width = "30px";
hcella.Controls.Add(acb);
htr.Cells.Add(hcella);
HtmlTableCell hcellf = new HtmlTableCell();
hcellf.InnerText = "Frage";
hcellf.Style.Add("font-weight", "bold");
hcellf.Style.Add("font-size", "15px");
htr.Cells.Add(hcellf);
tbl.Rows.Add(htr);
string cont = dtQuestionBlock.Rows[i]["ID"].ToString();
dtQuestion = taQuestion.GetDataBy1(Convert.ToInt32(cont));
nCountTables = i;
for (int j = 0; j < dtQuestion.Rows.Count; j++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
acb = new CheckBox();
acb.ID = "cb" + dtQuestion.Rows[j]["content"].ToString();
cell.Width = "30px";
cell.Controls.Add(acb);
tr.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dtQuestion.Rows[j]["content"].ToString();
cell.ID = "cell" + j + "_" + dtQuestion.Rows[j]["content"].ToString();
tr.Cells.Add(cell);
tbl.Rows.Add(tr);
}
updatePanel.ContentTemplateContainer.Controls.Add(tbl);
//tbl.Visible = false;
pCustomize.Controls.Add(updatePanel);
pCustomize.Controls.Add(new Literal() { ID = "br" + i + rows, Text = "<br />" });
}
return true;
}
catch (Exception ex)
{
Type cstype = ex.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = ex.ToString();
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
return false;
}
finally
{
taQuestionBlock.Dispose();
dtQuestionBlock.Dispose();
}
}
你可以试试这个代码
List<CheckBox> lstChckBox;
protected void Page_Load(object sender, EventArgs e)
{
// you can create controls programaticaly or html page, doesnt important
//only you should know controls ID and all controls share same checked event
CheckBox chc1 = new CheckBox();
chc1.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc2 = new CheckBox();
chc2.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc3 = new CheckBox();
chc3.CheckedChanged += new EventHandler(chck_CheckedChanged);
// Now, you can create a List so event is fired, you can catch which controls checked or not
lstChckBox = new List<CheckBox>();
lstChckBox.Add(chc1);
lstChckBox.Add(chc2);
lstChckBox.Add(chc3);
}
void chck_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (sender as CheckBox);
foreach (CheckBox item in lstChckBox)
{
if (item != checkBox)
{
item.CheckedChanged -= new EventHandler(chck_CheckedChanged);
item.Checked = !checkBox.Checked;
item.CheckedChanged += new EventHandler(chck_CheckedChanged);
}
}
}
我开始使用 asp.net 进行编程,我有一个带有一些复选框的 table。
问题是,我无法创建静态 tables,因为此操作与某些参数相关联。无论如何.. 当我点击第一个复选框时,我想反转这个 table 中的其他复选框。 我怎样才能赶上这个活动?
<%@ Page Title="Fragebogen generieren" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Generate.aspx.cs" Inherits="MAXXREC.Generate" SmartNavigation="True" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2><br /> <br />
<asp:Panel id="pCustomize" runat="server"></asp:Panel> <br /><br />
<asp:Button id="btnSave" class="btn btn-default" Text="Save" runat="server" OnClick="btnSave_Click"></asp:Button>
</asp:Content>
private bool SelectTheData()
{
dtQuestionBlock = taQuestionBlock.GetData();
try
{
int rows = dtQuestionBlock.Rows.Count;
for (int i = 0; i < rows; i++)
{
UpdatePanel updatePanel = new UpdatePanel();
updatePanel.ID = "up" + dtQuestionBlock.Rows[i][1].ToString();
Label lbl = new Label();
lbl.ID = "lbl" + dtQuestionBlock.Rows[i][1].ToString();
lbl.CssClass = "h4";
lbl.Attributes.Add("runat", "server");
lbl.Text = dtQuestionBlock.Rows[i][1].ToString();
pCustomize.Controls.Add(lbl);
pCustomize.Controls.Add(new Literal() { ID = "br" + i, Text = "<br /><br />" });
HtmlTable tbl = new HtmlTable();
tbl.Width = "100%";
tbl.Attributes.Add("class", "table");
tbl.ID = "htmltbl" + dtQuestionBlock.Rows[i][1].ToString();
HtmlTableRow htr = new HtmlTableRow();
HtmlTableCell hcella = new HtmlTableCell();
CheckBox acb = new CheckBox();
acb.ID = "cb" + dtQuestionBlock.Rows[i]["name"].ToString();
//acb.CheckedChanged += new EventHandler(cb_CheckedChanged);
hcella.Width = "30px";
hcella.Controls.Add(acb);
htr.Cells.Add(hcella);
HtmlTableCell hcellf = new HtmlTableCell();
hcellf.InnerText = "Frage";
hcellf.Style.Add("font-weight", "bold");
hcellf.Style.Add("font-size", "15px");
htr.Cells.Add(hcellf);
tbl.Rows.Add(htr);
string cont = dtQuestionBlock.Rows[i]["ID"].ToString();
dtQuestion = taQuestion.GetDataBy1(Convert.ToInt32(cont));
nCountTables = i;
for (int j = 0; j < dtQuestion.Rows.Count; j++)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
acb = new CheckBox();
acb.ID = "cb" + dtQuestion.Rows[j]["content"].ToString();
cell.Width = "30px";
cell.Controls.Add(acb);
tr.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = dtQuestion.Rows[j]["content"].ToString();
cell.ID = "cell" + j + "_" + dtQuestion.Rows[j]["content"].ToString();
tr.Cells.Add(cell);
tbl.Rows.Add(tr);
}
updatePanel.ContentTemplateContainer.Controls.Add(tbl);
//tbl.Visible = false;
pCustomize.Controls.Add(updatePanel);
pCustomize.Controls.Add(new Literal() { ID = "br" + i + rows, Text = "<br />" });
}
return true;
}
catch (Exception ex)
{
Type cstype = ex.GetType();
ClientScriptManager cs = Page.ClientScript;
String cstext = ex.ToString();
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
return false;
}
finally
{
taQuestionBlock.Dispose();
dtQuestionBlock.Dispose();
}
}
你可以试试这个代码
List<CheckBox> lstChckBox;
protected void Page_Load(object sender, EventArgs e)
{
// you can create controls programaticaly or html page, doesnt important
//only you should know controls ID and all controls share same checked event
CheckBox chc1 = new CheckBox();
chc1.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc2 = new CheckBox();
chc2.CheckedChanged += new EventHandler(chck_CheckedChanged);
CheckBox chc3 = new CheckBox();
chc3.CheckedChanged += new EventHandler(chck_CheckedChanged);
// Now, you can create a List so event is fired, you can catch which controls checked or not
lstChckBox = new List<CheckBox>();
lstChckBox.Add(chc1);
lstChckBox.Add(chc2);
lstChckBox.Add(chc3);
}
void chck_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (sender as CheckBox);
foreach (CheckBox item in lstChckBox)
{
if (item != checkBox)
{
item.CheckedChanged -= new EventHandler(chck_CheckedChanged);
item.Checked = !checkBox.Checked;
item.CheckedChanged += new EventHandler(chck_CheckedChanged);
}
}
}