1

Hi I am really stuck up in one of my development tasks since couple of days and I am unable to find out the exactly whats going on. Please help.

I am trying to add rows dynamically to the webpage as follows. I want to use server control. But I am unable to add more than one row.

No luck even though I used session variable. Kindly help please :)

-----------------aspx file---------------

<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:Button ID="btn1" runat="server" Text="Add Row" OnClick="addRow" />
</div>

---------------------C# code behind------------------------------

protected void addRow(object sender, EventArgs e)
    {

        int num_row = new int(); //checkpoint
        num_row = (tbl.Rows).Count;

        if (Session["tables"] != null)
        {
            tbl =  (Table)Session["tables"];
        }


        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(tb);
        cell2.Controls.Add(cb);

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

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


    }
7
  • You can merge your declaration and assignment for your num_row variable. Commented Oct 24, 2013 at 9:33
  • you can also use ViewState, but from the table you can know the number of rows without saving it on session. Commented Oct 24, 2013 at 9:36
  • Thnaks a lot guys for quick replies. Commented Oct 24, 2013 at 9:39
  • Nzall, I am not very clear . Could you please express it by code.. :) Commented Oct 24, 2013 at 9:39
  • Try using ListView control and keep the last row Commented Oct 24, 2013 at 9:43

2 Answers 2

1

You are able to see only one row added everytime because, dynamically created controls will not be available across postbacks.

When you click on add row first time, a post back happens, a third row is added. when you click in add row second time, the already added row would not be available and a new row gets added again. Finally you will be able to see only one row everytime.

Bottom line is you have to recreate the dynamically added controls everytime on postback in the page_load event, the reason being dynamically added server sontrols donot persist across postbacks

refer Why dynamically created user controls disappear when controls are not doing full postbacks? also

To maintain the viewstate of dynamically added controls you have to generate ids for the controls. When you recreate the controls during postback, recreate them with the same ids so that view state is maintained.

Use some standard logic to generate the ids for the controls. hope this helps.

Update:

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;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Saranya. But I am not clear about creating id logic. I have created id for each controld in the code above and also I have stored the whole in session variable. but still no luck :(. Could you please share the suggesting code changes with me.
Hi Alwyn , I have edited my answer to reflect the code change required. I have assumed that by default you have 2 rows in your table. you need not store the whole table in session variable, only the number of rows would suffice. Note that the viewstate that is the values you provide in the text box is also persisted across postback!!
0

After entering the value in the tale column how to save all the values in the database table?

1 Comment

I hope you are trying how to access the dynamically created controls Check out this ... read me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.