【问题标题】:GridView and CheckBoxList with UpdatePanel in asp.net (C#)在 asp.net (C#) 中使用 UpdatePanel 的 GridView 和 CheckBoxList
【发布时间】:2014-08-08 08:25:15
【问题描述】:

我在 UpdatePanel 中有一个 CheckBoxList,它在此事件上有 onselectedindex 更改事件,我必须通过从数据库中获取数据来更新我的 gridview。事件执行良好但 GridView 未显示更新数据可能是因为 GridView 不在更新面板中。

实际上,我的问题是在触发 Checkboxlist 的 onselectedindexchanged 后,gridview 中的数据没有更新,我不能将 gridview 放在更新面板中(它对我来说是强制性的)。

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>

【问题讨论】:

    标签: c# asp.net gridview updatepanel checkboxlist


    【解决方案1】:

    很遗憾,您不能这样做。如果GridViewUpdatePanel 之外,则不会。您的选择是:

    1. 删除UpdatePanel 并使用完整的回发
    2. UpdatePanel 中添加PostbackTrigger 并将CheckboxList 指定为触发元素...这与上述类似...总是会导致完整的回发
    3. GridView 放入UpdatePanel 中,一切都将在异步回发中完成
    4. CheckboxList 移到UpdatePanel 之外,然后将GridView 放在UpdatePanel 内,并为CheckboxList 添加AsyncPostbackTrigger,这样UpdatePanel 在检查项目时会异步更新

    底线是,如果您想异步重新绑定 GridView,则必须将其移动到 UpdatePanel 内,否则您将不得不采用完整的回发方法

    硬核解决方案

    您仍然可以借助 jQuery 插件、UserControl 和您可以创建以提供必要的 html 的 http 处理程序来手动完成所有操作。也就是说,拦截来自客户端的html请求,向处理程序发送一些参数的异步请求,以便处理程序知道如何告诉UserControl如何绑定GridView...当然,@987654343 @ 将在 UserControl

    处理程序可以使用当前HttpContext 中可用的Execute method of the HttpServerUtility class 实例将UserControl 的输出写入响应。比如……

    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.net 成员功能确保使用一个通用的处理程序 (.ashx),它自己插入到 ASP.NET 管道中,而无需您的进一步努力

    【讨论】:

      猜你喜欢
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2017-07-28
      相关资源
      最近更新 更多