Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

how to access the textbox of dynamically created table in user control


how to access the textbox of dynamically created table in user control

how to access the textbox of dynamically created table in user control

Actually i'm creating web template using and c#.
in my user control page i have to create the table dynamically. i just read the data from XML file then retrieve the name and number of columns and rows of each table. while i'm creating the table i assign the name and id to each cell. because i have one row including some textbox for adding the data to database. but after i have created the table i can not access to the textbox cells based on id to get their data and insert it to database. my dynamic table code is:


public void CreateAddDynamicTable()
    XmlDocument xDocRead = new XmlDocument();
    string xModuleName = hid_ChooseModule.Value;
    XmlNode xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
    int colCount = xColCounter.ChildNodes.Count;

    int nonPkCounter = 0;
    string[] nonPrimaryKey = new string[100];
    string[] nonPkNewDataTemp = new string[100];

    for (int i = 1; i <= colCount; i++)
        if (xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Add/TableColumn" + i).Attributes.GetNamedItem("IsPrimaryKey").Value == "N")
            nonPrimaryKey[nonPkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Add/TableColumn" + i).Attributes.GetNamedItem("Name").Value;


    // Fetch the number of Rows and Columns for the table
    // using the properties
    int tblRows = nonPkCounter;
    int tblCols = 2;

    // Create a Table and set its properties
    Table tbl = new Table();
    // Add the table to the placeholder control
    // Now iterate through the table and add your controls
    for (int i = 0; i < tblRows; i++)
        TableRow tr = new TableRow();
        for (int j = 0; j < tblCols; j++)
            TableCell tc = new TableCell();
            Label td_Add_Header = new Label();
            TextBox td_Add = new TextBox();

            if (j == 0)
                td_Add_Header.Text = nonPrimaryKey[i];
                td_Add_Header.ID = "lb" + (i + 1) + "_header_AddModule";

                // Add the control to the TableCell
                tc.CssClass = "td_Header_AddModule";
                td_Add.Text = "";
                td_Add.ID = "tb" + (i + 1) + "_AddModule";
                // Add the control to the TableCell
                tc.CssClass = "td_Tb_AddModule";

            // Add the TableCell to the TableRow
        // Add the TableRow to the Table

my user control page which is contain a PlaceHolder is as below:


<asp:Panel ID="pn_Uc_TModule" runat="server">

<asp:Table runat="server" ID="table_Uc_TModule" CssClass="table_Uc_TModule" Width="100%">
 <asp:TableRow runat="server" VerticalAlign="Top" Height="50px">
                <asp:TableCell runat="server">
                    <asp:Button runat="server" Text="" CssClass="btn_Add_Active" OnClick="btn_AddNew_Click" />
                    <asp:Button ID="btn_Cancel_NewItem" runat="server" Text="" CssClass="btn_Cancel_New" OnClick="btn_Cancel_AddNew" Visible="false" />

                    <asp:Table runat="server" ID="table_Uc_AddModule" Visible="false">
                        <asp:TableRow runat="server">
                            <asp:TableCell runat="server" >

                                <asp:PlaceHolder ID="ph_Uc_AddModule" runat="server">

                        <asp:TableRow ID="TableRow1" runat="server">
                            <asp:TableCell runat="server" CssClass="td_Tb_AddModule" HorizontalAlign="Center">
                                <asp:Button ID="btn_Insert" runat="server" OnClick="Uc_AddModule_ItemInsert" Text="" CssClass="btn_Add" />
                                <asp:Button ID="btn_Cancel" runat="server" OnClick="Uc_AddModule_Clear" Text="" CssClass="btn_Clear" />


i'm using below method in my insert function at the code behind to access the data inserted at the textbox:


for (int i = 1; i <= nonPkCounter; i++)
        TextBox textBox = (ph_Uc_AddModule.FindControl("tb" + i + "_AddModule")) as TextBox;
        nonPkNewDataTemp[i] = textBox.Text;

i have tried pn_Uc_TModule.FindControl("tb" + i + "_AddModule") and also This.ph_Uc_AddModule.FindControl("tb" + i + "_AddModule") but still con not get the texbox data to insert it to database. could you please guide me how to get it done.
appreciate your consideration.

RE: how to access the textbox of dynamically created table in user control

The code where you are trying to find the textboxes is where, on the page or usercontrol?

Also, I would first try finding the placeholder first, just to make sure you are actually coding it correctly and from the right place.

RE: how to access the textbox of dynamically created table in user control

thanks jbenson001 for reply,
i'm trying to find this control from usercontrol code behind.
and i have put it in my insert function.
i have used <Table> instead of <placeHolder> in my usercontor page as below but still does not work. and i can not access the textBox.


<asp:Table runat="server" ID="add_Table"></asp:Table>

try to access with below code at usercontrol code behind:


for (int i = 1; i <= nonPkCounter; i++)
        TextBox textBox = (add_Table.FindControl("tb" + i + "_AddModule")) as TextBox;
        nonPkNewDataTemp[i] = textBox.Text;

any suggestion?

RE: how to access the textbox of dynamically created table in user control

Like I said in my last post.  Try just finding the placeholder first or table first, what ever the containing object is.  Once you know you can find that object correctly, then write the code to find the textboxes.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close