asp.net无法找到从code-背后的div添加的控件 [英] asp.net Can't find controls added from code-behind in a div

查看:169
本文介绍了asp.net无法找到从code-背后的div添加的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问DIV控制钮,但我无法找到他们。

I'm trying to access Controls in div but I can't find them.

我它得到了一些控制的div。

I got a div with some controls in it.

<div id="divChampsFiltrageProjetsTest" class="divChampsFiltrageProjetsTest" runat="server">

  <span runat="server" style="color:black;"> Filtre 1 : </span>
  <asp:DropDownList ID="ddlFiltreProjets1" runat="server" ForeColor="Black" >
    <asp:ListItem Value="no" Selected="True">No. Projet</asp:ListItem>
    <asp:ListItem Value="desi">Designation Projet</asp:ListItem>
    <asp:ListItem Value="chef">Chef de Projet</asp:ListItem>
  </asp:DropDownList>

  <asp:TextBox ID="txtValeurFiltreProjets1" runat="server" ForeColor="Black" />

  <asp:Button ID="btnAddFiltre1" runat="server" Text="Ok" OnClick="btnAppliquerFiltreProjets_Click"/>

</div>

我可以找到code后面这些控制。但是,当我从code后面添加一些控件问题就来了。

I can find these controls in the code behind. But the problem comes when I add some controls from code behind.

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

在code的这部分控件添加到div,当我得到了网页,控件在源$ C ​​$ C。

This part of the code add the controls to the div and when I got the page, controls are in the source code.

        <div id="MainContent_divChampsFiltrageProjets" class="divChampsFiltrageProjets" style="display:block;">
            <div id="MainContent_divChampsFiltrageProjetsTest" class="divChampsFiltrageProjetsTest">

                <span style="color:black;"> Filtre 1 : </span>
                <select name="ctl00$MainContent$ddlFiltreProjets1" id="MainContent_ddlFiltreProjets1" style="color:Black;">
<option value="no">No. Projet</option>
<option value="desi">Designation Projet</option>
<option selected="selected" value="chef">Chef de Projet</option>

</select>

                <input name="ctl00$MainContent$txtValeurFiltreProjets1" type="text" value="johan" id="MainContent_txtValeurFiltreProjets1" style="color:Black;" />

            <span style="color:Black;">Filtre 2 : </span><select name="ctl00$MainContent$ddlFiltreProjets2" id="MainContent_ddlFiltreProjets2">
<option value="no">No. Projet</option>
<option value="desi">D&#233;signation Projet</option>
<option value="chef">Chef de Projets</option>

</select><input name="ctl00$MainContent$txtValeurFiltreProjets2" type="text" id="MainContent_txtValeurFiltreProjets2" style="color:Black;" /><input type="submit" name="ctl00$MainContent$btnAddFiltre1" value="Ok" id="MainContent_btnAddFiltre1" /></div>

但是,当我试图让我从code添加控件背后说值为null。

But when I try to get the controls that I add from code behind it says that the value is null.

    for (int i = 1; i < Convert.ToInt32(Session["nbFiltres"].ToString()) + 1; i++)
    {

        DropDownList ddl = (DropDownList)divChampsFiltrageProjetsTest.FindControl("ddlFiltreProjets"+i.ToString());
        TextBox txt = (TextBox)divChampsFiltrageProjetsTest.FindControl("txtValeurFiltreProjets" +i.ToString());

        Session["typeFiltreProjets" + i.ToString()] = ddl.SelectedValue.ToString();
        Session["valeurFiltreProjets" + i.ToString()] = txt.Text;
    }

这部分是一个按钮单击事件fonction。

This part is in the click event fonction of a button.

所以我的问题是,我从code添加控件背后是不是真的在DIV。

So my problem is that the controls that I add from code behind are not really in the div.

有没有人知道为什么吗?

Is anyone know why?

推荐答案

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

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.

这篇关于asp.net无法找到从code-背后的div添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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