test
test

Reputation: 2618

Dynamically adding row asp.net (on button click) - using dynamic HTML - row not shown

I have at table and a button. The first time the page loads, this method is called and it works perfectly. When I press the button ' New Slot' and this method is called, I am able to see during debugging that row.count increases for this table, yet in the browser, a new is not rendered (so it isn't the case that maybe one row pops on another). I am using an update panel in my UI, but with or without it I have the same effects. I also tried re-adding the table everytime in this method, but only 1 row is SHOWN/RENDERED, yet if I type something in the row, and then add another one, the stuff in the first row is lost, may be the row is being added and the previous one deleted? Maybe I have to use the viewstate? Can someone guide me to the right solution please?

private void AddSlot()
        {
            DropDownList ddlProfiles = new DropDownList();
            ddlProfiles.DataSource = DLProfiles.Instance.GetAllProfilesByGid(Int32.Parse(Session["gid"].ToString()));
            ddlProfiles.DataTextField = "pName";
            ddlProfiles.DataBind();

            TableRow row = new TableRow();

            TableCell cellProfile = new TableCell();
            cellProfile.Controls.Add(ddlProfiles);

            TableCell cellStart = new TableCell();
            TextBox startTime = new TextBox();
            cellStart.Controls.Add(startTime);

            TableCell cellEnd = new TableCell();
            TextBox endTime = new TextBox();
            cellEnd.Controls.Add(endTime);

            row.Cells.Add(cellProfile);
            row.Cells.Add(cellStart);
            row.Cells.Add(cellEnd);

            scheduleTable.Rows.Add(row);

        }

<asp:UpdatePanel ID="pnlSchedTable" runat="server">
                <ContentTemplate>
        <div id="divCurrentSchedule" runat="server">
            <h3 id="scheduleTitle" runat="server">
            </h3>

                    <asp:Table ID="scheduleTable" runat="server">
                        <asp:TableHeaderRow ID="rowHeaders" runat="server">
                            <asp:TableHeaderCell Text="Profile Name" />
                            <asp:TableHeaderCell Text="Start time" />
                            <asp:TableHeaderCell Text="End time" />
                        </asp:TableHeaderRow>
                    </asp:Table>
                    <asp:Button ID="btnNewSlot" runat="server" OnClick="btnNewSlot_Click" Text="New Slot" />

        </div></ContentTemplate>
            </asp:UpdatePanel>

Upvotes: 1

Views: 1696

Answers (1)

beauXjames
beauXjames

Reputation: 8418

When are you calling AddSlot? Chances are, if you want it to run after any page request, whether it be an initial load or a button click, you should move it to 'Pre-Render'...then your timing issues will be adjusted.

Upvotes: 1

Related Questions