【问题标题】:Click on ASP.net Gridview's Header causing post back. How to stop this?单击导致回发的 ASP.net Gridview Header。如何阻止这种情况?
【发布时间】:2016-02-25 07:40:36
【问题描述】:

我正在使用 ASP.net 建立一个网站。那里有一个gridview。 问题是当我单击 gridview 的标题时,它会导致页面返回。不知道如何阻止这个。这是代码

   <asp:GridView ID="gridEmployee" Width="100%" RowStyle-Height="15" HeaderStyle-BackColor="#3AC0F2"
                HeaderStyle-ForeColor="White" RowStyle-BackColor="Black" RowStyle-ForeColor="#3A3A3A"
                AutoGenerateColumns="False" BorderWidth="1px" OnRowCreated="gridEmployee_RowCreated" OnSelectedIndexChanging="gridEmployee_SelectedIndexChanging"
                ClientIDMode="Static" ShowHeaderWhenEmpty="True" DataKeyNames="employee_master_id_pk"
                AllowPaging="True" PageSize="20" runat="server" EnableViewState="false" OnPageIndexChanging="gridEmployee_PageIndexChanging">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Image runat="server" ImageUrl='<%#string.Format("/images/profile_full/{0}",Eval("image_url")) %>' Width="50px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="NAME" ReadOnly="true" DataField="call_name">
                        <HeaderStyle Width="70%" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="MOBILE" ReadOnly="true" DataField="mobile_no">
                        <HeaderStyle Width="30%" />
                    </asp:BoundField>
                    <asp:CommandField ShowSelectButton="true" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" />
                </Columns>
                <SelectedRowStyle BackColor="#0AA6FF" Font-Bold="True" Font-Italic="true" ForeColor="White" />
                <HeaderStyle BackColor="#199e38" ForeColor="White" Height="20px" Font-Size="Medium"></HeaderStyle>
                <RowStyle BackColor="#f6f7f9" ForeColor="Black" Height="20px" HorizontalAlign="Left" />
            </asp:GridView>

下面是显示的错误信息

System.Web.HttpUnhandledException (0x80004005): 类型异常 'System.Web.HttpUnhandledException' 被抛出。 ---> System.ArgumentException:回发或回调参数无效。 使用配置或 在页面中。出于安全目的, 此功能验证回发或回调事件的参数 源自最初呈现它们的服务器控件。如果 数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法,以便 注册回发或回调数据以进行验证。在 System.Web.UI.ClientScriptManager.ValidateEvent(字符串唯一 ID, 字符串参数)在 System.Web.UI.Control.ValidateEvent(字符串 唯一ID,字符串事件参数)在 System.Web.UI.WebControls.GridView.RaisePostBackEvent(字符串 事件参数)在 System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 事件参数)在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.HandleError(异常 e)处 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest() 在 System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext 上下文)
在 System.Web.UI.Page.ProcessRequest(HttpContext 上下文) 在 ASP.private_task_aspx.ProcessRequest(HttpContext context) 在 c:\Users\testl\AppData\Local\Temp\Temporary ASP.NET 文件\root\cc51fe2f\b39cd5d2\App_Web_wtae1pov.4.cs:第 0 行 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, Boolean & completedSynchronously)

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    问题是关于排序与否。

    如果你想让 GridView 不是可排序的,你必须设置

    <asp:GridView ID="gridEmployee"  AllowSorting="false"
    

    如果你想允许排序但没有完整的回发,你必须在 GridView 周围添加一个 UpdatePanel

    <asp:UpdatePanel>
        <ContentTemplate>
           <asp:GridView ID="gridEmployee" 
    

    【讨论】:

    • 定义“不工作”你的听众仍然可以点击吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多