简体   繁体   中英

Can't access elements by Ajax event handler (using update panel)

aspx looks like this:

            <asp:Panel ID="MedChangeScroll" CssClass="table-responsive scrollBlock" runat="server">
                <asp:Table ID="MedChangeTable" CssClass="table table-striped table-hover table-bordered" runat="server"></asp:Table>
            </asp:Panel>
            <asp:Button ID="EnableAddMedChangeBtn" runat="server" CssClass="btn btn-success" Text="Add Med Change" OnClick="enableAddMedChange" />
            <asp:Button ID="CancelAddMedChangeBtn" runat="server" Style="display: none;" CssClass="btn" Text="Cancel" OnClick="cancelAddMedChange" />

Everything is housed in an update panel. On the click of the EnableAddMedChangeBtn I run the following:

public void enableAddMedChange(object sender, EventArgs e)
        {

            TableRow tableRow = new TableRow();
            MedChangeTable.Rows.Add(tableRow);
            tableRow.TableSection = TableRowSection.TableBody;

            CancelAddMedChangeBtn.Style.Add("display", "inline-block");
            EnableAddMedChangeBtn.Style.Add("display", "none");
        }
}

In other words when the enableAddMedChangeBtn is clicked I add a row to the table and display the cancel button.

Now, when the cancel button is clicked, I cannot access the row that I added on the first click in the cancel button's event handler. In other words if the table had 2 rows and I added a 3rd, the table shows up as having two rows in the cancel button's event handler. In fact, if I comment out the entire cancel button event handler (but still let the empty method execute), the additional row gets deleted.

All help is appreciated, thanks in advance.

Check out this link: http://www.dotnetbull.com/2013/09/maintain-dynamic-control-events-data-during-postback.html .

It seems that dynamically created controls (ie controls which are not in the markup for the page) require a little additional plumbing to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM