C# 在代码隐藏中引用非asp复选框
我试图在C#代码隐藏中引用一个非asp复选框。复选框不是asp元素的原因是它是动态自动生成的,而不是作为网站的一部分。到目前为止,我有以下相关的aspx:C# 在代码隐藏中引用非asp复选框,c#,asp.net,webforms,C#,Asp.net,Webforms,我试图在C#代码隐藏中引用一个非asp复选框。复选框不是asp元素的原因是它是动态自动生成的,而不是作为网站的一部分。到目前为止,我有以下相关的aspx: <asp:Table ID="myTable" runat="server" Width="100%"> <asp:TableRow> <asp:TableCell>A</asp:TableCell> <asp:TableCell>B<
<asp:Table ID="myTable" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell>A</asp:TableCell>
<asp:TableCell>B</asp:TableCell>
<asp:TableCell>C</asp:TableCell>
<asp:TableCell>D</asp:TableCell>
<asp:TableCell>E</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:LinkButton runat="server" ID="TEST" CssClass="btn btn-default pull-right" OnClick="TEST_Click">
TEST <i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>
A.
B
C
D
E
试验
而背后的C#代码是:
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
if (check)
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" >";
}
else
{
tempCell.Text = "<input type=\"checkbox\" id=\"chk" + i + "\" checked>";
}
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
HtmlInputCheckBox chkbox = (HtmlInputCheckBox)FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
MessageBox.Show("NOTHING :(");
}
public void GenerateTable()
{
int i=0;
bool[]box={true,false,true,false,true};
List tRows=新列表();
TableRow newRow=新TableRow();
添加(newRow);
foreach(var复选框)
{
TableCell tempCell=新的TableCell();
如果(检查)
{
tempCell.Text=“”;
}
其他的
{
tempCell.Text=“”;
}
tRows[0].Cells.Add(tempCell);
i++;
}
foreach(表行在tRows中)
{
myTable.Rows.Add(row);
}
}
公共无效测试\u单击(对象发送者,事件参数e)
{
HtmlInputCheckBox chkbox=(HtmlInputCheckBox)FindControl(“chk1”);
if(chkbox!=null)
{
如果(!chkbox.Checked)
{
MessageBox.Show(“选中”);
}
其他的
{
MessageBox.Show(“未选中”);
}
}
其他的
MessageBox.Show(“无:(”);
}
chkbox总是空的:(.您需要更改两件事 要通过
FindControl
查找复选框,它必须是页面控件集合的一部分,这意味着您必须添加复选框
控件
CheckBox c = new CheckBox { ID = "chk" + i };
tempCell.Controls.Add(c);
动态添加的复选框
控件是表
控件集合的一部分,因此您必须在那里而不是在页面上搜索它
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
下面是代码的完整更新
protected void Page_Load(object sender, EventArgs e)
{
GenerateTable();
}
public void GenerateTable()
{
int i = 0;
bool[] box = {true, false, true, false, true};
List<TableRow> tRows = new List<TableRow>();
TableRow newRow = new TableRow();
tRows.Add(newRow);
foreach (var check in box)
{
TableCell tempCell = new TableCell();
CheckBox c = new CheckBox { ID = "chk" + i };
c.Checked = check;
tempCell.Controls.Add(c);
tRows[0].Cells.Add(tempCell);
i++;
}
foreach (TableRow row in tRows)
{
myTable.Rows.Add(row);
}
}
public void TEST_Click(object sender, EventArgs e)
{
CheckBox chkbox = (CheckBox)this.myTable.FindControl("chk1");
if (chkbox != null)
{
if (!chkbox.Checked)
{
MessageBox.Show("Checked");
}
else
{
MessageBox.Show("NOT Checked");
}
}
else
{
MessageBox.Show("NOTHING :(");
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
可生成();
}
public void GenerateTable()
{
int i=0;
bool[]box={true,false,true,false,true};
List tRows=新列表();
TableRow newRow=新TableRow();
添加(newRow);
foreach(var复选框)
{
TableCell tempCell=新的TableCell();
复选框c=新复选框{ID=“chk”+i};
c、 检查=检查;
tempCell.Controls.Add(c);
tRows[0].Cells.Add(tempCell);
i++;
}
foreach(表行在tRows中)
{
myTable.Rows.Add(row);
}
}
公共无效测试\u单击(对象发送者,事件参数e)
{
复选框chkbox=(复选框)this.myTable.FindControl(“chk1”);
if(chkbox!=null)
{
如果(!chkbox.Checked)
{
MessageBox.Show(“选中”);
}
其他的
{
MessageBox.Show(“未选中”);
}
}
其他的
{
MessageBox.Show(“无:(”);
}
}
请像第一个一样将其包装起来,并使用Pnl.FindControl(“chk1”);这是一个独立的.aspx页面,还是有母版页?母版页有更改生成ID的趋势。您可以编辑呈现输入的示例吗?我尝试过Robs建议,但它只能在aspx中手动添加复选框。它有母版页,我更新了复选框的添加方式。谢谢。