gpt4 book ai didi

c# - 在动态表的 RadioButton CheckedChanges 中添加新的事件处理程序

转载 作者:太空狗 更新时间:2023-10-29 23:48:48 25 4
gpt4 key购买 nike

我正在尝试将另一个 eventHandler 添加到 RadioButton。这是示例代码(正在运行):

ASP.NET:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />

C#:

protected void Page_Load(object sender, EventArgs e)
{
RadioButton RB1 = new RadioButton();
RB1.ID = "1";
RB1.GroupName = "bla";
RB1.CheckedChanged += new EventHandler(CheckedChanged);
RadioButton RB2 = new RadioButton();
RB2.ID = "2";
RB2.GroupName = "bla";
RB2.CheckedChanged += new EventHandler(CheckedChanged);
PlaceHolder1.Controls.Add(RB1);
PlaceHolder1.Controls.Add(RB2);

}
protected void CheckedChanged(object sender, EventArgs e)
{
Label1.Text = ((RadioButton)sender).ID;
}

在我的项目中,我动态创建了 RadioButtons(我从数据库中获取的行数)。同样添加 eventHandler 不起作用,但是如果我写

MyRadioButton.Load += new EventHandler(Another_method);

Another_method 将开始,但在

MyRadioButton.CheckedChanged += new EventHandler(Main_method);

如果我选择其中一个 RadioButtonsMain_method 将不会启动。怎么了?


@凯文P这是我的代码:

    Table tb1 = new Table();
PlaceHolder1.Controls.Add(tb1);
TableRow tr = new TableRow();
TableCell tc = new TableCell();
//adding the first row with title"
tr.Cells.Add(tc);
for (int i = 0; i < ((ArrayList)(result[0])).Count; i++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((columnsResultFromSqlClients)(i)).ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
//filling the table
for (int i = 0; i < result.Count; i++)
{
tr = new TableRow();
tc = new TableCell();
//adding radio button
RadioButton RB = new RadioButton();
RB.Attributes.Add("value", ((ArrayList)(result[i]))[0].ToString());
RB.GroupName = "for_selecting";
RB.ID = ((ArrayList)(result[i]))[0].ToString();
RB.CheckedChanged += new EventHandler(RB_CheckedChanged2);
//RB.AutoPostBack = true;

RB.Attributes.Add("AutoPostBack", "True");
tc.Controls.Add(RB);
tr.Cells.Add(tc);
//adding content
for (int j = 0; j < ((ArrayList)(result[i])).Count; j++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((ArrayList)(result[i]))[j].ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
}

如果我使用 RB.AutoPostBack = true;,我没有时间按下按钮来提交我的选择,因为当我单击其中一个单选按钮时页面将重新加载。还有 RB_CheckedChanged2 代码:

protected void RB_CheckedChanged2(object sender, EventArgs e)
{
RadioButton tempRB = (RadioButton)sender;
if (tempRB.Checked)
{
selected_id = tempRB.ID;
}
}

select_id 是一个static int 变量,标准值为“-1”。

最佳答案

如果我没记错的话,在 ASP.Net 中使用动态控件,您需要在回发时“重新连接”它们的事件。请记住,在回传中,动态控件不再存在。您必须重新创建它们。

关于c# - 在动态表的 RadioButton CheckedChanges 中添加新的事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1958575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com