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

发布于 2024-08-16 17:59:49 字数 3213 浏览 2 评论 0原文

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

ASP.NET:C#

<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" />

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_method 中如果我选择其中之一则不会启动RadioButtons。 怎么了?


@凯文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”。

I'm trying to add another eventHandler to RadioButton. This is the sample code (which is working):

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

In my project I have dynamic creating of RadioButtons (the number of rows I get from database). The same adding eventHandler does not work, but if I write

MyRadioButton.Load += new EventHandler(Another_method);

The Another_method will start, but in

MyRadioButton.CheckedChanged += new EventHandler(Main_method);

the Main_method will not start if I choose one of the RadioButtons.
What is wrong?


@KevinP
This is my code:

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

If I use RB.AutoPostBack = true;, I have no time to press the button to submit my choice, cause the page will reload when i click the one of the Radio Buttons.
Also the RB_CheckedChanged2 code:

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

The select_id is a static int varible with standart value = "-1".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城孤痞 2024-08-23 17:59:49

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

If I am not mistaken, with dynamic controls in ASP.Net, you need to "rewire" their events on a postback. Remember that on a postback, dynamic controls are no longer there. You have to recreate them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文