[英]How to add a control with an event handler dynamically to an update panel and have the event handler post back via AJAX
[英]Can't access elements by Ajax event handler (using update panel)
aspx看起來像這樣:
<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" />
一切都放在更新面板中。 單擊EnableAddMedChangeBtn,然后運行以下命令:
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");
}
}
換句話說,當單擊enableAddMedChangeBtn時,我在表中添加了一行並顯示“取消”按鈕。
現在,單擊“取消”按鈕后,我無法訪問在“取消”按鈕的事件處理程序中第一次單擊時添加的行。 換句話說,如果該表有2行,而我添加了第3行,則該表在取消按鈕的事件處理程序中顯示為兩行。 實際上,如果我注釋掉整個“取消”按鈕事件處理程序(但仍然讓空方法執行),則會刪除其他行。
感謝所有的幫助,在此先感謝。
查看此鏈接: http : //www.dotnetbull.com/2013/09/maintain-dynamic-control-events-data-during-postback.html 。
動態創建的控件(即不在頁面標記中的控件)似乎需要一些額外的維護。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.