简体   繁体   English

访问动态创建的控件(c#)

[英]Accessing controls created dynamically (c#)

In my code behind (c#) I dynamically created some RadioButtonLists with more RadioButtons in each of them. 在我的代码(c#)后面,我动态地创建了一些RadioButtonLists,每个RadioButtonLlist中都有更多的RadioButton。 I put all controls to a specific Panel. 我将所有控件都放到了特定的面板上。 What I need to know is how to access those controls later as they are not created in .aspx file (with drag and drop from toolbox)? 我需要知道的是如何在以后访问这些控件,因为它们不是在.aspx文件中创建的(通过工具箱拖放)?

I tried this: 我试过这个:

    foreach (Control child in panel.Controls)
    {
        Response.Write("test1");
        if (child.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButtonList"))
        {
            RadioButtonList r = (RadioButtonList)child;
            Response.Write("test2");
        }   
    }

"test1" and "test2" dont show up in my page. “test1”和“test2”不会显示在我的页面中。 That means something is wrong with this logic. 这意味着这个逻辑出了问题。 Any suggestions what could I do? 有什么建议我该怎么办?

You must recreate your controls after each postback. 您必须在每次回发后重新创建控件。

ASP.NET is stateless, that is, when you postback a page to the server, your dynamically created controls won't be part of the page anymore. ASP.NET是无状态的,也就是说,当您将页面回发到服务器时,动态创建的控件将不再是页面的一部分。

Last week I had to overcome this situation once more. 上周我不得不再次克服这种情况。

What did I do? 我做了什么? I saved the data that I used to create the controls inside Session object. 我保存了用于在Session对象中创建控件的数据。 On PageLoad method I passed that same data to recreate the dynamic controls. 在PageLoad方法上,我传递了相同的数据来重新创建动态控件。

What I suggest is: Write a method to create the dynamic controls. 我的建议是:编写一个方法来创建动态控件。

On PageLoad method check to see if it's a postback... 在PageLoad方法上检查它是否是回发...

if(Page.IsPostBack)
{
   // Recreate your controls here.
}

A really important thing: assign unique IDs to your dynamically created controls so that ASP.NET can recreate the controls binding their existing event handlers, restoring their ViewState, etc. 非常重要的事情:为动态创建的控件分配唯一ID,以便ASP.NET可以重新创建绑定其现有事件处理程序的控件,还原其ViewState等。

myControl.ID = "myId";

I had a hard time to learn how this thing works. 我很难学习这件事是如何运作的。 Once you learn you have power in your hands. 一旦你了解到掌握了你的力量。 Dynamically created controls open up a new world of possibilities. 动态创建的控件开辟了一个新的可能性世界。

As Frank mentioned: you can use the "is" keyword this way to facilitate your life... 正如弗兰克所说:你可以用这种方式使用“is”关键词来促进你的生活......

if(child is RadioButtonList)


Note: it's worth to mention the ASP.NET Page Life Cycle Overview page on MSDN for further reference. 注意:值得一提的是MSDN上的ASP.NET页面生命周期概述页面,以供进一步参考。

When are you doing this in your code? 你什么时候在你的代码中这样做? Be sure you do this at the right time in the ASP life cycle or your controls don't exist yet: http://msdn.microsoft.com/en-us/library/ms178472.aspx 请确保您在ASP生命周期中的正确时间执行此操作,或者您的控件尚不存在: http//msdn.microsoft.com/en-us/library/ms178472.aspx

I don't think creating controls in the PageLoad is the right away of doing, first the asp.net life cycle goes from Initialization;Load ViewState Data;Load PostData; 我不认为在PageLoad中创建控件是正确的,首先asp.net生命周期来自Initialization;加载ViewState数据;加载PostData; Object Load etc. 对象加载等

if you create controls at the Page_Load you'll lose the ViewState, events etc. 如果你在Page_Load上创建控件,你将丢失ViewState,事件等。

The right away is doing at PageInit, or if is a control (OnInit). 马上就是在PageInit,或者如果是控件(OnInit)。

The next difficult is that at PageInit, you don't have the ViewState Available, if you need to reconstruct the number of objects you need to store some context/info in a hidden field ant then retrieve that information at PageInit, Create the objects and voila! 接下来的困难是,在PageInit,你没有ViewState可用,如果你需要重建你需要在隐藏字段中存储一些上下文/信息的对象数量,然后在PageInit中检索该信息,创建对象和瞧!

Example: 例:

imagine that you need to create 1..N TextBoxes, you create html hidden field (not with runat=server) eg NumberOfTextBoxes. 想象你需要创建1..N TextBoxes,你创建html隐藏字段(不是用runat = server),例如NumberOfTextBoxes。

When you are executing PageInit Code: you retrieve the value eg numberOfTextBoxes = Request.Form["NumberOfTextBoxes"], then you create the TextBoxes. 当您执行PageInit代码时:检索值,例如numberOfTextBoxes = Request.Form [“NumberOfTextBoxes”],然后创建TextBoxes。

Remember the most important thing is to match the number and the order of existent Controls stored the ViewState. 请记住,最重要的是匹配存储在ViewState中的现有控件的数量和顺序。

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

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