当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - How to access dynamically created server controls in asp.net

问题描述:

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

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:

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

Please assist me in this.

网友答案:

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.

However this should not return null in FindControl.

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

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

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

    }
网友答案:

On Each Post Back, Dynamically Created controls may not visible. So You need to recreate dynamic Controls on each post back.Then you can access these controls.

网友答案:

you can do that like this :

TextBox txt= (TextBox)FindControl("txt"+i.Tostring());
string txtValue=txt.Text;

and to access all of the textboxes in a for loop :

int num_row = Convert.ToInt16(sometable.Rows.Count);
for (int i = 1; i < num_row; i++)
{
    string txtID="txt" + i.Tostring();
    TextBox txt= (TextBox)FindControl(txtID);
    string txtValue=txt.Text;
}
网友答案:

TRY:

            tb = (TextBox)sometable.FindControl("txt" + i);
分享给朋友:
您可能感兴趣的文章:
随机阅读: