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

时间:2009-12-24 14:48:52

标签: c# asp.net radio-button event-handling

我正在尝试向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);
如果我选择其中一个Main_methodRadioButtons将无法启动。 有什么问题?


@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);
    }

如果我使用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_idstatic int变量,标准值=“ - 1”。

1 个答案:

答案 0 :(得分:1)

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