如何访问在asp.net中动态创建的服务器控件

问题描述:

我创建了一个网页,我用服务器端控件动态添加到表中。

I created a web page and I used to add server side controls to table dynamically.

我赋予他们各自的ID的..
但我无法访问这些动态创建的服务器控件。

I assigned them individually the id's.. But I am unable to access these dynamically created server controls.

C#code:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            int num_row = (int)Session["No_of_Rows"];
            for (int i = 2; i < num_row; i++)
            {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();
                TextBox tb = new TextBox();
                CheckBox cb = new CheckBox();

                row.ID = "rw" + i;

                cell1.ID = "c" + i + "1";
                cell2.ID = "c" + i + "2";

                tb.ID = "txt" + i;
                tb.EnableViewState = true;
                cb.ID = "chk" + i;

                cell1.Controls.Add(cb);
                cell2.Controls.Add(tb);

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

                tbl.Rows.Add(row);
            }
        }
        else
        {
            Session["No_of_Rows"] = 2;
        }




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

        int num_row = (int)Session["No_of_Rows"] + 1;
        TableRow row = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TextBox tb = new TextBox();
        CheckBox cb = new CheckBox();

        row.ID = "rw" + num_row;

        cell1.ID = "c" + num_row + "1";
        cell2.ID = "c" + num_row + "2";

        tb.ID = "txt" + num_row;
        //tb.EnableViewState = true;
        cb.ID = "chk" + num_row;

        cell1.Controls.Add(cb);
        cell2.Controls.Add(tb);

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

        tbl.Rows.Add(row);
        Session["No_of_Rows"] = tbl.Rows.Count;



    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();

        int num_row = Convert.ToInt16(tbl.Rows.Count);
        for (int i = 1; i < num_row; i++)
        {
            string tstr = "txt" + i;
            tb = (TextBox)this.FindControl(tstr);

        }
        Label1.Text = tb.Text;
    }


ASP code:


asp code:

    <form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" Text="textbox value is"></asp:Label>
    <asp:Table ID="tbl" runat="server">
        <asp:TableRow ID="rw0">
            <asp:TableCell ID="c01" Width="100px">
                <asp:CheckBox runat="server" ID="chk0" />
            </asp:TableCell>
            <asp:TableCell ID="c02" Width="100px">
                <asp:TextBox runat="server" ID="txt0" />
            </asp:TableCell></asp:TableRow>
        <asp:TableRow ID="rw1">
            <asp:TableCell ID="c11" Width="100px">
                <asp:CheckBox ID="chk1" runat="server" />
            </asp:TableCell>
            <asp:TableCell ID="c12" Width="100px">
                <asp:TextBox runat="server" ID="txt1" />
            </asp:TableCell></asp:TableRow>
    </asp:Table>
    <asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
</div>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
</form>

请帮我在这。

要测试你的情况下,我增加2控制为txtOutput和的GetValues​​ button.Everything似乎是很好,除了你的控件ID从0开始的地方作为你的循环开始1。

To test your scenario, i added 2 controls txtOutput and Getvalues button.Everything seems to be fine, except your control ids starts from 0 where as your for loop starts from 1.

不过,这不应该的FindControl返回null。

However this should not return null in FindControl.

我怀疑你的表ID不正确,因为在其他职位你提到的表ID是TBL。

I suspect your table id is incorrect because in the other post you referred the table id is "tbl".

其他明智的一切似乎是正确的。无论如何,我已经粘贴您的code,我在这里试过,和它的作品。

Other wise everything seems to be correct. Anyway, i have pasted your code which i tried here, and it works.

另一种可能,你会得到错误是,如果ü尝试之前,他们甚至在Page_Load中创建访问控制。但是因为你有2排在表中的静态,你的code应该工作无关的动态控制创造。请尝试以下code和更新

Another possibility where you will get the error is if u try to access the controls before even they are created in Page_Load. However because you have 2 rows static in your table, your code should work irrespective of the dynamic control creation. Pls try the below code and update

<form id="form1" runat="server">
        <div>
        <div>
        <asp:Table ID="tbl" runat="server">
            <asp:TableRow ID="rw0">
                <asp:TableCell ID="c01" Width="100px">
                    <asp:CheckBox runat="server" ID="chk0" />
                </asp:TableCell>
                <asp:TableCell ID="c02" Width="100px">
                    <asp:TextBox runat="server" ID="txt0" />
                </asp:TableCell></asp:TableRow>
            <asp:TableRow ID="rw1">
                <asp:TableCell ID="c11" Width="100px">
                    <asp:CheckBox ID="chk1" runat="server" />
                </asp:TableCell><asp:TableCell ID="c12" Width="100px">
                    <asp:TextBox runat="server" ID="txt1" />
                </asp:TableCell></asp:TableRow>
        </asp:Table>
        <asp:textbox runat="server" id="checkIn" ClientIDMode="Static" name="checkIn" ></asp:textbox>
        <asp:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
        <br />
        <asp:Button ID="GetValues" runat="server" Text="GetValues" OnClick="GetValuesfromDynamicControls" />
        <asp:TextBox ID="txtOutput" runat="server" />

// code后面从这里开始。

//code behind starts here

 public partial class DynamicControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack)
            {
                int num_row = (int)Session["No_of_Rows"];
                for (int i = 2; i < num_row; i++)
                {
                    TableRow row = new TableRow();
                    TableCell cell1 = new TableCell();
                    TableCell cell2 = new TableCell();
                    TextBox tb = new TextBox();
                    CheckBox cb = new CheckBox();

                    row.ID = "rw" + i;

                    cell1.ID = "c" + i + "1";
                    cell2.ID = "c" + i + "2";

                    tb.ID = "txt" + i;
                    tb.EnableViewState = true;
                    cb.ID = "chk" + i;

                    cell1.Controls.Add(cb);
                    cell2.Controls.Add(tb);

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

                    tbl.Rows.Add(row);
                }
            }
            else
            {
                Session["No_of_Rows"] = 2;
            }
        }

        protected void addRow(object sender, EventArgs e)
        {
            int num_row = (int)Session["No_of_Rows"];
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TextBox tb = new TextBox();
            CheckBox cb = new CheckBox();

            row.ID = "rw" + num_row;

            cell1.ID = "c" + num_row + "1";
            cell2.ID = "c" + num_row + "2";

            tb.ID = "txt" + num_row;
            tb.EnableViewState = true;
            cb.ID = "chk" + num_row;

            cell1.Controls.Add(cb);
            cell2.Controls.Add(tb);

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

            tbl.Rows.Add(row);
            Session["No_of_Rows"] = tbl.Rows.Count;
        }


        protected void GetValuesfromDynamicControls(object sender, EventArgs e)
        {
            //int rows =Convert.ToInt32( Session["No_of_Rows"]);
            int rows = Convert.ToInt32(tbl.Rows.Count);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < rows; i++)
            {               
                    TextBox tb =(TextBox) Page.FindControl("txt" + i);
                    sb.Append(tb.Text + ";");                
            }

            txtOutput.Text = sb.ToString();
        }

    }