简体   繁体   English

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

[英]Accessing value of dynamically created controls c# asp.net

I'm trying to access the value of a selected radioButton. 我正在尝试访问所选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. 以下代码位于for循环中并输出无线电选项。 All the options are for one required user selection. 所有选项均适用于一个必需的用户选择。

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

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. 这样做的一种简单方法是循环使用radioArea的子控件并检查eack控件以查看它是否是单选按钮以及是否已选中。 Another options is to give each ID a prefix ie ID="RAD_"+singleType.appTypeName and loop over the Request object and match each one with the correct suffix. 另一个选项是为每个ID提供一个前缀,即ID =“RAD _”+ singleType.appTypeName并循环访问Request对象,并将每个ID与正确的后缀匹配。

The FindControl method you are using, expects you to pass it the ID that was assigned to control when you created it. 您正在使用的FindControl方法希望您将创建它时分配给控件的ID传递给它。

So for your example the RadioButton.ID should be equal to singleType.appTypeID.ToString() 因此,对于您的示例, RadioButton.ID应该等于singleType.appTypeID.ToString()

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM