点击gridview中的选择按钮会导致页面刷新

3
每当我尝试点击Gridview内的搜索按钮或选择按钮时,整个页面会重新加载并向上滚动。我希望它保持在原地。怎么做?
代码:
<asp:TextBox ID="txtComplaintSubject" runat="server" CssClass="textField_width"></asp:TextBox>&nbsp
            <asp:Button ID="btnSearch"  CssClass="btn btn-success" runat="server" Text="Search" 
             ValidationGroup="AdminRole" ClientIDMode="Static" OnClick="btnSearch_Click" />

             <asp:GridView ID="gridViewComplaints" AutoGenerateSelectButton="true" runat="server" CssClass="mGrid" OnSelectedIndexChanged="gridViewComplaints_SelectedIndexChanged">
              <EmptyDataRowStyle BorderStyle="None" ForeColor="Red" BorderWidth="0px" />
               <EmptyDataTemplate>
                  No Data Found for this Input. Try Again.
               </EmptyDataTemplate> 
             <SelectedRowStyle CssClass="selected-row" BackColor="YellowGreen" ForeColor="white" />
             </asp:GridView>



protected void btnSearch_Click(object sender, EventArgs e)
    {
        ManageComposedLetter mngCompLetter = new ManageComposedLetter();
        DataTable dt = mngCompLetter.FillGridView_Complaints(txtComplaintSubject.Text);
        if (dt.Rows.Count > 0)
        {
            gridViewComplaints.DataSource = dt;
            gridViewComplaints.DataBind();
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            gridViewComplaints.DataSource = dt;
            gridViewComplaints.DataBind();
            int totalcolums = gridViewComplaints.Rows[0].Cells.Count;
            gridViewComplaints.Rows[0].Cells.Clear();
            gridViewComplaints.Rows[0].Cells.Add(new TableCell());
            gridViewComplaints.Rows[0].Cells[0].ColumnSpan = totalcolums;
            gridViewComplaints.Rows[0].Cells[0].Text = "No Data Found";
        }

}


是的,这就是正确的方法。请查看此链接 - Dumisani
3个回答

4
如果您希望页面刷新后仍保留当前位置,只需添加以下代码:
MaintainScrollPositionOnPostback="true"

在您的aspx文件顶部添加以下内容,使其看起来像这样:
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs"

0

我认为在页面加载时也可以使用if(!Page.IsPostBack)。这对于解决这个问题可能更容易。 (但我建议大家当然要学习更新面板)


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接