使用 C# 从动态控件中获取值?

发布于 2024-12-19 12:37:31 字数 753 浏览 0 评论 0原文

我在我的项目中创建了一些单选按钮列表控件,每次加载页面时都会创建它们,我想获取用户选择的单选按钮的值,但由于我的单选按钮是动态创建的,我不知道如何访问它们的值以及如何创建它们的事件处理程序。有没有办法在创建控件时为其分配名称或 ID? 我希望你能帮助我。

我在 page_load 事件上创建了一系列 radiobuttlist,其中文本及其值已从数据库中提取。现在,用户必须从该单选按钮列表中选择选项之一,我想获取用户检查的单选按钮的值。如果我不知道 radiobuttlist 的名称和 id,因为它们是动态创建的,我该怎么做。 这就是我所得到的:

for (int i = 3; i < numfields; i++) {
            if (dr[i].ToString() != "" && dr[i] != null){
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString()));
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex);
            }
        }

所以基本上我使用我的数据读取器来循环数据库中的数据,如果字段中的值不为空或为空,那么我将一个项目添加到名为“r”的 radiobuttlist 中 我也尝试为此创建一个事件处理程序,但由于我从未与他们合作过,所以我真的不知道该怎么做。 :( 如果我看起来太可怜了,我很抱歉。

I created a few radiobuttonlist controls on my project, they're created every time the page is loaded, i want to get the value of the radiobutton that the user has selected, but since my radiobuttons were created dynamically, i don't know how to acces to their values nor how to create their event handlers. Is there a way to assign a name or id to the control when i create it?
i hope you can help me.

I create a seires of radiobuttlist on the page_load event, with the text and their values been pulled out of a database. now, the user has to choose one of the options from that radiobuttlist and i want to get the value of the radiobutton the user checked. how do i do that if i don't know the name nor the id of the radiobuttlist since they're created dynamically.
this is what i've got:

for (int i = 3; i < numfields; i++) {
            if (dr[i].ToString() != "" && dr[i] != null){
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString()));
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex);
            }
        }

so basically i use my datareader to loop through the data in the database, if the value from the field isn't empty or null, then i add an item to the radiobuttlist called "r"
i tried to create an eventhandler for that too, but since i have never worked with them i really don't know what to do. :(
I'm so sorry if i seem way too pathetic.

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

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

发布评论

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

评论(4

橘寄 2024-12-26 12:37:31

快速浏览一下您的代码:

for (int i = 3; i < numfields; i++) { 
            if (dr[i].ToString() != "" && dr[i] != null){ 
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString())); 
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex); 
            } 
        } 

最明显的是您的 if 语句。您应该首先检查 null:

if (dr[i] != null && dr[i].ToString() != ""){

就像 dr[i] 为 null 一样,您将得到一个异常(因为您将尝试在 null 对象上调用 ToString() 方法。

如果 dr 的内容始终会是字符串,你可能会考虑写:

if(!String.IsNullOrEmpty(dr[i]){

我还注意到你从 3 开始索引 - 这是因为你想跳过前 3 个字段吗?

无论你在哪里创建变量,'r',你都可以设置您可以使用 ID 属性来实现名称和 ID 属性。查找 PostBack 上的控件 因此,如果您像这样创建了无线电列表:

RadioButtonList r = new RadioButtonList();
r.Id = "MyRadioButtonList";
r.SelectedIndexChanged += MyRadioButton_SelectedIndexChanged;

它将指向以下事件处理程序:

private void MyRadioButton_SelectedIndexChanged(Object sender, EventArgs e) {

    ... Do Stuff ...    

}

当您回发时,可以通过多种方式查找控件;您可以在 Request.Forms 集合中查找控件。匹配您提交的控件的名称,或者更合适的是,您可以使用 FindControl 方法和您为控件提供的 ID。请参阅 C#, FindControl 的帖子,其方法(作者为 Jeff Atwood!)将在整个控件层次结构中搜索您的控件。

添加动态控件的时间也很重要。如果您在页面生命周期中添加得太晚,那么它将在回发中不可用。有关的详细信息,请参阅http://support.microsoft.com/kb/317515 >恰逢添加控件。动态 ASP.Net 控件也有大量资源。

Taking a quick look at your code:

for (int i = 3; i < numfields; i++) { 
            if (dr[i].ToString() != "" && dr[i] != null){ 
                r.Items.Add(new ListItem(dr[i].ToString(), dr[i].ToString())); 
                //r.SelectedIndexChanged += new EventHandler(rowSelectedIndex); 
            } 
        } 

The most obvious thing that jumps out is your if statement. You should first check for null:

if (dr[i] != null && dr[i].ToString() != ""){

As if dr[i] is null, you'll get an exception (as you'll be trying to call the ToString() method on a null object.

If the contents of dr are always going to be strings, you might consider writing:

if(!String.IsNullOrEmpty(dr[i]){

I also note you start your indexing at 3 - is this because you want to skip the first 3 fields?

Wherever you create your variable, 'r', you can set the name and ID properties. You can use the ID property to look for the control on PostBack. So if you created your radiolist like so:

RadioButtonList r = new RadioButtonList();
r.Id = "MyRadioButtonList";
r.SelectedIndexChanged += MyRadioButton_SelectedIndexChanged;

Which would point at the following event handler:

private void MyRadioButton_SelectedIndexChanged(Object sender, EventArgs e) {

    ... Do Stuff ...    

}

There are several ways of finding your control when you post back; you can look in the Request.Forms collection for a control matching the name of the control you submitted, or, more appropriately, you can use the FindControl method with the ID you gave the control. See C#, FindControl for a post with a method (by Jeff Atwood!) that will search the entire hierarchy of controls for your control.

When you add a dynamic control is important, too. If you add it too late in the page lifecycle then it will not be available on PostBack. See http://support.microsoft.com/kb/317515 for more details on just when to add a control. There are plenty of resources for Dynamic ASP.Net controls around too.

-柠檬树下少年和吉他 2024-12-26 12:37:31

您可以在创建单选按钮时将其放入列表中。这也是您想要添加处理程序的时候。

RadioButton rb;
for (int i = 1; i < 5; i++)
{
    rb = new RadioButton();

    rb.AutoSize = true;
    rb.Location = new System.Drawing.Point(25, (i*25) + 25);
    rb.Name = "radioButton" + i.ToString();
    rb.Text = "radioButton" + i.ToString();
    //Add some event handler?
    this.Controls.Add(rb);
    lstRadioButton.Add(rb);
}

每当您想知道选择了哪一个时,您可以对列表进行 foreach 循环,并查看 RadioButton 是否被选中。

foreach (RadioButton rButton in lstRadioButton)
{
    if (rButton.Checked == true)
    {
        //Do something
    }
}

You could put your RadioButton into a list as you create them. This is also when you want to add your handlers.

RadioButton rb;
for (int i = 1; i < 5; i++)
{
    rb = new RadioButton();

    rb.AutoSize = true;
    rb.Location = new System.Drawing.Point(25, (i*25) + 25);
    rb.Name = "radioButton" + i.ToString();
    rb.Text = "radioButton" + i.ToString();
    //Add some event handler?
    this.Controls.Add(rb);
    lstRadioButton.Add(rb);
}

Whenever you want to know which one is selected you can do a foreach loop of your list and look if your RadioButton is checked.

foreach (RadioButton rButton in lstRadioButton)
{
    if (rButton.Checked == true)
    {
        //Do something
    }
}
溺ぐ爱和你が 2024-12-26 12:37:31

您可能正在搜索 TagName 属性(如果编程名称对您来说不够)。

You are maybe searching for TagName property if the programmatic name isn't enough for you.

人疚 2024-12-26 12:37:31

问题是您正在 page_load 中创建控件。为了将它们的值正确地发送回控件中,您必须将此创建移至 page_init 方法中并每次重新创建它们。

然后,在page_load中,您可以正确访问控件中的值。如果您使用一致的命名约定为它们提供 ID,您将能够使用 FindControl 方法找到它们,或者在 page_init 中,您可以将它们存储在页面或用户控件级别的集合中。

The problem is that you are creating the controls in page_load. In order for their values to be posted back into the controls correctly, you must move this creation into the page_init method and recreate them every time.

Then, in page_load, you can access the values in the controls correctly. If you give them IDs using a consistent naming convention, you will be able to find them using the FindControl method or, in page_init, you can store them in a collection at the page or user control level.

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