且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

asp.net无法找到从code-背后的div添加的控件

更新时间:2023-09-26 11:49:16

在添加控件动态,您将需要确保这些被重新创建之后每次回来后。

When adding controls dynamically you will need to make sure that these are re-created after each post back.

有是在.aspx文件中声明的控制,并从code编程加入他们身后之间的差异:

There is a difference between declaring controls in the .aspx file and adding them programmatically from code behind:

当控件添加声明,然后所有的控件都没有对未来的回发,因为在每次回发请求ASP.NET Web表单重新创建使用的声明在设计和数据页的状态从视图状态,并从提交的表单。

When the controls are added declaratively then all the controls are there on future postbacks, because on every postback request ASP.NET Webforms recreates the state of the page using the declarations in the designer and the data from the Viewstate and from the posted form.

当控件编程addded ,然后ASP.NET会尝试做相同的:重新使用了这些声明在.aspx文件+ + ViewState的形式发布在服务器上的页面的状态。它没有知道你已经修改了控制的层次结构和您添加或删除一些控制,因为这些改变也不会在回发之间任何方式跟踪的方法。这就是为什么你需要重新创建每个回发预期的层次结构。

When the controls are addded programmatically then ASP.NET will try to do the same: recreate the state of the page on the server using the declarations in the .aspx file + ViewState + posted form. It does not have a way of knowing that you've modified the Controls hierarchy and that you've added or removed some controls, because these changes are not tracked in any way between postbacks. That is why you will need to recreate the expected hierarchy on every postback.

您将需要重新运行code,增加的 Page_Init 活动的控制。这一点很重要,因为这件事之后ASP.NET将尝试状态信息从ViewState中,并从发布的数据,以控制层次,现在将包含动态控件绑定。如果你不这样做,你仍然有你的控制,但你将无法取回张贴在那些控制用户的数据。

You will need to re-run your code that adds controls on the Page_Init event. This is important, because after this event ASP.NET will try to bind state information from the ViewState and from the posted data to the control hierarchy, that now will contain your dynamic controls. If you don't do this, you will still have your controls but you won't be able to retrieve the data posted by the user on those controls.

下面是如何在code可能看起来像:

Here is how your code could look like:

public void Page_Init(object sender, EventArgs e){
    CreateDynamicControls();
}

public void CreateDynamicControls(){
    HtmlGenericControl span = new HtmlGenericControl("span");
    DropDownList ddlList = new DropDownList();
    TextBox txtValeur = new TextBox();
    Button btnAdd = new Button();
    ListItem item1 = new ListItem("No. Projet", "no");
    ListItem item2 = new ListItem("Désignation Projet", "desi");
    ListItem item3 = new ListItem("Chef de Projets", "chef");

    span.Style.Add(HtmlTextWriterStyle.Color, "Black");
    span.InnerText = "Filtre " + (i + 1).ToString() + " : ";

    ddlList.ID = "ddlFiltreProjets" + (i + 1).ToString();
    ddlList.Items.Add(item1);
    ddlList.Items.Add(item2);
    ddlList.Items.Add(item3);

    txtValeur.ID = "txtValeurFiltreProjets" + (i + 1).ToString();
    txtValeur.ForeColor = System.Drawing.Color.Black;

    btnAdd = btnAddFiltre1;

    divChampsFiltrageProjetsTest.Controls.Add(span);
    divChampsFiltrageProjetsTest.Controls.Add(ddlList);
    divChampsFiltrageProjetsTest.Controls.Add(txtValeur);
    divChampsFiltrageProjetsTest.Controls.Add(btnAdd);
}

您可以找到在MSDN上的详细信息,在 ASP.NET页生命周期文章。

You can find more info on MSDN, on the ASP.NET Page Life Cycle article.