在动态表中的RadioButton CheckedChanges中添加新的eventHandler

WIS*_*MAN 5 c# asp.net event-handling radio-button

我想另一个添加eventHandlerRadioButton.这是示例代码(正在运行):

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" />
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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

MyRadioButton.Load += new EventHandler(Another_method);
Run Code Online (Sandbox Code Playgroud)

Another_method将启动,但在

MyRadioButton.CheckedChanged += new EventHandler(Main_method);
Run Code Online (Sandbox Code Playgroud)

Main_method,如果我选择的一个将无法启动RadioButtons.怎么了?


@KevinP这是我的代码:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

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

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

select_id是一个static int变量,标准值=" - 1".

Rya*_*ord 1

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