Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使动态创建的单选按钮执行公共事件_C#_Events_Dynamic_Radio Button - Fatal编程技术网

C# 使动态创建的单选按钮执行公共事件

C# 使动态创建的单选按钮执行公共事件,c#,events,dynamic,radio-button,C#,Events,Dynamic,Radio Button,我的表单中有一个名为Button1的按钮和一个名为TexBox1的文本框 我编写了这样的代码,当我单击按钮时,将创建一个单选按钮,并使用它自己的名称: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls;

我的表单中有一个名为Button1的按钮和一个名为TexBox1的文本框

我编写了这样的代码,当我单击按钮时,将创建一个单选按钮,并使用它自己的名称:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Counter"] = 0;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            HtmlGenericControl div = new HtmlGenericControl("div");
            for (int i = 0; i < Convert.ToInt32(ViewState["Counter"]); i++)
            {
                RadioButton rb = new RadioButton();
                rb.GroupName = "GN1";
                rb.ID = i.ToString();
                rb.Text = "Button" + i.ToString(); 
                div.Controls.Add(rb);
                Panel1.Controls.Add(div);

            }
            ViewState["Counter"] = Convert.ToInt32(ViewState["Counter"]) + 1;


        }
    }
}

如何使其应用于任何单选按钮?

在生成
单选按钮
控件时,请尝试订阅
CheckedChanged
事件:

for (int i = 0; i < Convert.ToInt32(ViewState["Counter"]); i++)
{
    RadioButton rb = new RadioButton();
    rb.CheckedChanged += (s, a) => TextBox1.Text = ((RadioButton)s).Text;
    ...
    ...
}
for(int i=0;iTextBox1.Text=((RadioButton)s).Text;
...
...
}

您可以使用以下方法:

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton btn = sender as RadioButton;
    textBox1.Text = btn.Text;
}
创建RadioBUtton时,需要将其附加到
Checked Changed
事件

rb.CheckedChanged += radioButton_CheckedChanged;
在你有了你的方法之后:

private void SomeMethod_Click(object sender, RoutedEventArgs e)
{
    // Here your logic
}

您好…谢谢您的回复…我没有函数“private void radioButton_CheckedChanged(object sender,EventArgs e)”,当它被动态创建时…我如何让按钮自动调用此函数?在类中编写该函数,当你创建你的按钮时,使用rb.CheckedChanged+=radioButton\u CheckedChanged我做了你说的,但它没有做任何事情,我认为你没有做。@selman 22我已经把rb.CheckedChanged+=radioButton\u CheckedChanged放在这里了;在我的
受保护的无效按钮1内单击(对象发送者,事件参数e)
函数
rb.Text=“Button”+i.ToString()
并使函数
private void radioButton\u checked更改(对象发送方,事件参数e)
如您所说。。。是吗?您好…谢谢您的回复…当我单击它们时,不会发生任何事情…文本框中不会出现任何内容,我是否需要刷新任何内容、页面或其他内容?Hello@user3215839只需键入:“rb.”然后点击“ctrl+Space”以显示visual studio intellisense,并且会出现类似“OnMouseDown”的事件。我使用此事件编辑我的答案。问候语!
RadioButton rb = new RadioButton();
rb.OnMouseDown+= SomeMethod_Click;
private void SomeMethod_Click(object sender, RoutedEventArgs e)
{
    // Here your logic
}