[英]GridView and CheckBoxList with UpdatePanel in asp.net (C#)
我在UpdatePanel中有一個CheckBoxList,該事件具有onselectedindex更改事件,我必須通過從數據庫中獲取數據來更新我的gridview。 事件執行得很好,但是GridView不顯示更新的數據可能是因為GridView不在updatepanel中。
實際上,我的問題是觸發Checkboxlist的onselectedindexchanged后數據未在gridview中更新,並且我無法將gridview放入updatepanel(對我來說是強制性的)內。
onselectedindexchanged事件工作正常,我已經將其檢出。 那么我該如何更新我的gridview數據。
這是我的代碼
<asp:UpdatePanel ID="MonthUpdatePanel" runat="server">
<ContentTemplate>
<asp:CheckBoxList ID="MonthCheckBoxList" runat="server" AutoPostBack="True"
onselectedindexchanged="MonthCheckBoxList_SelectedIndexChanged"
Width="195px" onclick="updateviewmore()" >
<asp:ListItem Value="1">1 Month</asp:ListItem>
<asp:ListItem Value="2">2 Month</asp:ListItem>
<asp:ListItem Value="6">6 Month</asp:ListItem>
<asp:ListItem Value="12">1 Year</asp:ListItem>
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
<div>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Width="100%"
GridLines="None" onrowdatabound="GridView2_RowDataBound"
ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="pad5">
<asp:Label ID="BookImageName" Text='<% #"../BookImages/"+ Eval("BookImage") %>' CssClass="BookImage" runat="server" Height="180px" Width="135px" />
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
不幸的是,你不能那樣做。 如果GridView
在UpdatePanel
之外,則不是這樣。 您的選擇是:
UpdatePanel
並使用完整的回發 UpdatePanel
添加PostbackTrigger
並將CheckboxList
指定為triger元素...這與上面類似...它將始終導致完整的回發 GridView
放在UpdatePanel
,一切將在異步回發中完成 CheckboxList
UpdatePanel
之外,然后將GridView
放在UpdatePanel
內,並為CheckboxList
添加一個AsyncPostbackTrigger
,以便在選中和項目時UpdatePanel
可以異步更新。 最重要的是,如果要異步重新綁定GridView
則必須將其移到UpdatePanel
內,否則必須使用完整的回發方法
您仍然可以借助jQuery
插件, UserControl
和可以創建的http處理程序手動創建所有內容,以提供必要的html。 也就是說,攔截來自客戶端的html請求,通過一些參數向處理程序發出異步請求,以使處理程序知道如何告訴UserControl
如何綁定GridView
... ...當然, GridView
將位於UserControl
內部
處理程序可以使用HttpServerUtility
類實例的Execute
方法將UserControl
的輸出寫入響應,該方法在當前HttpContext
可用。 例如...
public class YourHandler: IHttpHandler {
public void ProcessRequest(HttpContext ctx){
StringWriter writer = new StringWriter();
ctx.Server.Execute("~/UserControl.ascx", output, false);
ctx.Response.Write(writer);
}
}
這種方法有效地工作,就好像UserControl
在正常請求期間正在通過所有ASP.NET管道周期一樣,並且它的上下文將與http處理程序的上下文完全相同...因此,如果您使用的是內置asp。網絡成員資格功能確保使用通用處理程序(.ashx),該處理程序本身即可插入ASP.NET管道,而無需您付出額外的努力
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.