访问动态创建的控件的值c#asp.net [英] Accessing value of dynamically created controls c# asp.net

查看:118
本文介绍了访问动态创建的控件的值c#asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我不知道如何访问所选值,因为我是不知道它是什么名字,它是动态的。

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

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

The following code is in a for loop and outputs radio options. 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);

这是我试图访问它:

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

我意识到这应该很简单,但仍然在一个非常PHP的心态,欣赏任何帮助。

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

谢谢

推荐答案

混淆了单选按钮控件的各种属性以及如何正确生成单选按钮。

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.

此处的ID属性是关键。

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.

GroupName只是一个别名对于单选按钮的标准名称属性,所以当用户点击组中的单选按钮时,可以选择一个单选按钮。

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.

例如:

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

在上面的例子中,我分配了singleType.appTypeName作为ID,假设这是唯一的。

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

然后在回发上检索该值,这样做,昵称singleType.appTypeName =mySuperApp:

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

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

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

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

您将需要循环播放单选按钮,找出哪一个被检查。一个简单的方法是循环播放RadioArea的子控件,并检查eack控件,看它是否是一个单选按钮,如果它被检查。另一个选择是给每个ID一个前缀,即ID =RAD _+ singleType.appTypeName,并循环遍历Request对象,并将每个ID与正确的后缀进行匹配。

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.

这篇关于访问动态创建的控件的值c#asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆