[英]Web User Control postback Issues
我是ASP.net的新手,我對回發有一些疑問。
我有一個這樣的Senario:
1)我在網上有一個網格,里面有一個面板。
2)通過調用此按鈕,使用Web用戶控件“插入”面板
Control ctlControl;
ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
pnlTransaction.Controls.Add(ctlControl);
3)Web用戶控件提供兩個按鈕。 一種是“更新”,一種是“重置”。
問題是這樣的:
我想要實現的是,當按下“更新”按鈕時,它將把某些內容更新回我的數據庫嗎? 但似乎在我按下按鈕“更新”或“重置”之后。 Web用戶控件不見了。 對我的客人來說是因為回發問題嗎? 那是對的嗎?
我嘗試了if(!postback)仍然無法正常工作。
我該如何克服呢? 我已經撓頭大約一天了?
非常感謝。
問候
LiangCk:
PS:對不起,我的英語水平,請不要猶豫,說出我的錯誤或錯誤。
好了,您可以將任何數據列轉換為模板列,然后將Web用戶控件拖放到模板列上
這將導致類似於以下代碼的檢查,其中代碼中的“ uc1:webUserControle1”位於
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
<uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
如果您使用的是AJAX,請嘗試在UCT設計頁面上添加updatepanel
ASP.NET將不會在回發之間保留動態添加的用戶控件。 這就是為什么它消失了。 每次創建頁面時,都需要添加控件。 但是,您將需要在初始化控件樹時添加它,並在希望觸發事件時恢復原始控件ID。 這些鏈接提供了完整的說明http://www.4guysfromrolla.com/articles/092904-1.aspx和http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/
您必須每次在Page_Init或Page_Load上重新加載用戶控件。 然后,您可以獲得按鈕單擊事件,並且之后用戶控件將不會丟失。
private void LoadUserControl(){
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath)) {
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
protected void Page_Load(object sender, EventArgs e) {
LoadUserControl();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.