访问动态创建的控件的值 c# asp.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:00:31  来源:igfitidea点击:

Accessing value of dynamically created controls c# asp.net

c#asp.netcontrolsdynamic

提问by Andrew

I'm trying to access the value of a selected radioButton. I have a list of radio button's seperated by sub headers (hence why i havent used a radioList)

我正在尝试访问所选单选按钮的值。我有一个由子标题分隔的单选按钮列表(因此我没有使用 radioList)

I don't know how to access the selected value as i'm not sure what name it's given with it being dynamic.

我不知道如何访问选定的值,因为我不确定它是动态的。

The following code is in a for loop and outputs radio options. All the options are for one required user selection.

以下代码在 for 循环中并输出单选选项。所有选项都用于一个必需的用户选择。

//add list to radio button list
RadioButton button1 = new RadioButton { 
GroupName = "myGroup", 
Text = singleType.appTypeName, 
ID = singleType.appTypeID.ToString() 
};
radioArea.Controls.Add(button1);
myLabel = new Label();
myLabel.Text = "<br />";
radioArea.Controls.Add(myLabel);

This is how im trying to access it:

这是我尝试访问它的方式:

RadioButton myButton = (RadioButton) radioArea.FindControl("myGroup");

I realise this should be very simple but im still in a very much PHP mindset and appreciate any help.

我意识到这应该非常简单,但我仍然处于非常 PHP 的心态,并感谢任何帮助。

Thanks

谢谢

采纳答案by Tim Saunders

Hi it sounds to me like you are bit confused over what the various properties of the radio button control do and how to generate the radio button correctly.

嗨,听起来您对单选按钮控件的各种属性的作用以及如何正确生成单选按钮感到有些困惑。

The ID property is key here.

ID 属性是这里的关键。

You should set the ID property to a unique string that you then use to access the control on postback.

您应该将 ID 属性设置为一个唯一的字符串,然后您可以使用该字符串在回发时访问控件。

The GroupName is just an alias for the standard name property of a radio button and is used so when a user clicks on a radio button in a group only one radio button can be selected.

GroupName 只是单选按钮的标准名称属性的别名,因此当用户单击组中的单选按钮时,只能选择一个单选按钮。

For example:

例如:

//add list to radio button list
RadioButton radioButton = new RadioButton();
radioButton.GroupName = "radioGroup";
radioButton.Text = singleType.appTypeID.ToString();
radioButton.ID = singleType.appTypeName;

radioArea.Controls.Add(radioButton);

Label label = new Label();
label.Text = "<br />";
radioArea.Controls.Add(label);

In the above example I've assigned singleType.appTypeName as the ID, assuming that this is unique.

在上面的示例中,我将 singleType.appTypeName 指定为 ID,假设这是唯一的。

Then to retrive the value on postback do this, assumnig that singleType.appTypeName = "mySuperApp":

然后要在回发时检索值,请假设 singleType.appTypeName = "mySuperApp":

RadioButton radioButton = (RadioButton) radioArea.FindControl("mySuperApp");

Now you can access the radioButton variable to check which GroupName it has, get it's value and check that it is checked.

现在您可以访问 radioButton 变量来检查它有哪个 GroupName,获取它的值并检查它是否被选中。

You will need to loop over the radio buttons to find out which one is checked. An easy way of doing this is looping over the child controls of the radioArea and checking eack control to see if it is a radio button and if it is checked. Another options is to give each ID a prefix i.e. ID="RAD_"+singleType.appTypeName and loop over the Request object and match each one with the correct suffix.

您将需要遍历单选按钮以找出选中的那个。一个简单的方法是遍历 radioArea 的子控件并检查 eack 控件以查看它是否是一个单选按钮以及它是否被选中。另一种选择是给每个 ID 一个前缀,即 ID="RAD_"+singleType.appTypeName 并遍历 Request 对象,并将每个 ID 与正确的后缀匹配。

回答by rocka

The FindControlmethod you are using, expects you to pass it the IDthat was assigned to control when you created it.

FindControl您正在使用的方法希望您将ID创建它时分配给控制的方法传递给它。

So for your example the RadioButton.IDshould be equal to singleType.appTypeID.ToString()

所以对于你的例子RadioButton.ID应该等于singleType.appTypeID.ToString()

Hope this helps.

希望这可以帮助。