简体   繁体   English

无法访问动态创建的控件

[英]Unable to access the dynamically created controls

In my website I am able to create controls ( textbox and dropdown ) dynamically using UPDATE PANEL control. 在我的网站上,我能够使用UPDATE PANEL控件动态创建控件( textboxdropdown )。 But I am unable to access these controls. 但是我无法访问这些控件。 Even while viewing in source code in the browser during debug, the dynamically created controls are absent! 即使在调试过程中在浏览器中查看源代码时,也不存在动态创建的控件! I am wondering how come we are able to see the dynamicallly created controls while the definition for them are absent in the source view of the o/p. 我想知道为什么我们能看到动态创建的控件,而在o / p的源视图中却没有它们的定义。 Could you please clear my this doubt. 你能解决我的这个疑问吗? Thanks a lot. 非常感谢。

Code: 码:

      <div id="divAssociatedInc">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel11" runat="server">
        <ContentTemplate>
            <%--<asp:UpdatePanel ID="updPnlAssociatedInc" runat="server" UpdateMode="Conditional">
                                                <ContentTemplate>--%>
            <asp:Label ID="lblcount" runat="server" Text="1" Visible="false"></asp:Label>
            <asp:Table ID="someTBL" runat="server" Style="width: 100%">
                <asp:TableRow>
                    <asp:TableCell HorizontalAlign="right" Style="width: 200px">
                        <asp:Label ID="lblAssociatedIncNo" runat="server" Text="Associated Incident(s)# :"></asp:Label><font
                            face="verdana" color="red" size="1"></font>

                        <div>
                            <asp:Button runat="server" ID="BtnAddAssoInc" OnClick="btnAddNewRowAssocInc_Click"
                                Text="Add Associated Incidents" />

                        </div>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="left" Style="width: 320px;">
                        <asp:TextBox ID="txtAssocIncRec0" name = "txtAssoc0" runat="server" MaxLength="12"></asp:TextBox>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="right" Style="width: 200px">
                        <asp:Label ID="lblAssociatedSeverity" runat="server" Text="Severity :"></asp:Label><font
                            face="verdana" color="red" size="1"></font>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="left" Style="width: 320px;">
                        <asp:DropDownList ID="ddlbAssocIncRec0" name="ddlbAssoc0" runat="server">
                        </asp:DropDownList>
                    </asp:TableCell>
                </asp:TableRow>

            </asp:Table>
        </ContentTemplate>
    </asp:UpdatePanel>

</div>

and the corresponding code behind is 而后面的相应代码是

        protected void Page_Load(object sender, EventArgs e)
    {
        int intRowCount = Convert.ToInt32(ViewState["No_of_Rows"]);
        for (int i = 1; i <= intRowCount; i++)
        {
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            TableCell cell4 = new TableCell();
            TextBox tb = new TextBox();
            DropDownList ddl = new DropDownList();
            Label lbl1 = new Label();
            Label lbl2 = new Label();

            row.ID = "rwAssocIncRec" + i;

            cell1.ID = "cAssocIncRec" + i + "1";
            cell2.ID = "cAssocIncRec" + i + "2";
            cell3.ID = "cAssocIncRec" + i + "3";
            cell4.ID = "cAssocIncRec" + i + "4";
            cell2.Attributes.Add("align", "left");
            cell4.Attributes.Add("align", "left");


            tb.ID = "txtAssocIncRec" + i;
            tb.MaxLength = 12;
            tb.EnableViewState = true;

            lbl1.ID = "lblAssocIncRec" + i + "01";
            lbl2.ID = "lblAssocIncRec" + i + "02";

            ddl.ID = "ddlbAssocIncRec" + i;

            cell1.Controls.Add(lbl1);
            cell2.Controls.Add(tb);
            cell3.Controls.Add(lbl2);
            cell4.Controls.Add(ddl);
            ddl.Items.Add(new ListItem("--Select--", "0"));
            ddl.Items.Add(new ListItem("1", "1"));
            ddl.Items.Add(new ListItem("2", "2"));


            row.Cells.Add(cell1);
            row.Cells.Add(cell2);
            row.Cells.Add(cell3);
            row.Cells.Add(cell4);

            someTBL.Rows.Add(row);
        }

    }
    protected void btnAddNewRowAssocInc_Click(object sender, EventArgs e)
    {

        TableRow newRow = new TableRow();
        newRow.ID = "trAssocIncRec" + lblcount.Text;

        TableCell newCell1 = new TableCell();
        newCell1.ID = "tdAssocIncRec1" + lblcount.Text;
        Label lbl = new Label();
        lbl.Text = "";
        lbl.ID = "lblAssocIncRec" + lblcount.Text;
        newCell1.Controls.Add(lbl);
        newRow.Cells.Add(newCell1);

        TableCell newCell2 = new TableCell();
        newCell2.ID = "tdAssocIncRec2" + lblcount.Text;
        TextBox txtBox = new TextBox();
        txtBox.ID = "txtAssocIncRec" + lblcount.Text;
        txtBox.MaxLength = 12;
        newCell2.Attributes.Add("align", "left");
        newCell2.Controls.Add(txtBox);
        newRow.Cells.Add(newCell2);

        TableCell newCell3 = new TableCell();
        newCell3.ID = "tdAssocIncRec3" + lblcount.Text;
        Label lbl2 = new Label();
        lbl2.Text = "";
        lbl2.ID = "lblAssocIncRec2" + lblcount.Text;
        newCell3.Controls.Add(lbl2);
        newRow.Cells.Add(newCell3);

        TableCell newCell4 = new TableCell();
        newCell4.ID = "tdAssocIncRec4" + lblcount.Text;
        DropDownList ddlb = new DropDownList();
        ddlb.ID = "ddlbAssocIncRec" + lblcount.Text;
        newCell4.Attributes.Add("align", "left");
        newCell4.Controls.Add(ddlb);
        ddlb.Items.Add(new ListItem("--Select--", "0"));
        ddlb.Items.Add(new ListItem("1", "1"));
        ddlb.Items.Add(new ListItem("2", "2"));
        newRow.Cells.Add(newCell4);

        someTBL.Rows.Add(newRow);
        ViewState["No_of_Rows"] = Convert.ToInt32(lblcount.Text);

        lblcount.Text = Convert.ToString(Convert.ToInt32(lblcount.Text) + 1);


    }

What you need to do is recreate these in either the Load event, or the Init event of the page every time. 您需要做的是每次都在页面的Load事件或Init事件中重新创建它们。 This is so that the values can be retrieved. 这样可以检索值。

I would then find the controls you have created and add them to a list in a viewstate(so it gets destroyed when the user leaves the page) 然后,我将找到您创建的控件,并将其添加到viewstate的列表中(这样,当用户离开页面时,该控件将被销毁)

ViewState["DynamicControls"] = list;

Then you have the values stored which you can come back to. 然后,您存储了可以返回的值。

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

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