Answered by:
MaintainScrollPositionOnPostback is not working?

Question
-
User-381059448 posted
i'm using an asp:GridView within an asp:Panel within an asp:Content page. The GridView has over 100 rows, so I'm using the Panel to provide a vertical scrollbar. I set MaintainScrollPositionOnPostBack="True" in both the <%@ Page %> directive but the GridView jumps right back to the top when the user selects one of the rows. What am I doing wrong?
Thanks,
this is my code for grid
<asp:Panel runat="server" class="tab-pane fade " ID="company_car_admin_panel_layout" role="tabpanel" aria-labelledby="company_car_admin_panel_layout_tab"> <div style="max-height:400px; overflow-y:scroll ; max-width:700px; overflow-x:scroll"> <asp:GridView ID="GridView1" runat="server" CssClass="table table-bordered" AutoGenerateColumns="false" ShowFooter="true" ShowHeaderWhenEmpty="true" CellPadding="4" ForeColor="#333333" GridLines="None" Width="700px" DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:TemplateField HeaderText="Company Car Value"> <ItemTemplate> <asp:Label Text='<%# Eval("Company_Car_Value") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="text_company_car_value" Text='<%# Eval("Company_Car_Value") %>' runat="server" /> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="text_company_car_value_footer" runat="server" /> <asp:RequiredFieldValidator ValidationGroup="car" ID="RequiredFieldValidator1" SetFocusOnError="true" ControlToValidate="text_company_car_value_footer" runat="server" Text="*Company value is Required" ForeColor="Red"></asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Company Car Code"> <ItemTemplate> <asp:Label Text='<%# Eval("Company_Car_Code") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="text_company_car_code" Text='<%# Eval("Company_Car_Code") %>' runat="server" /> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="text_company_car_code_footer" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ValidationGroup="car" SetFocusOnError="true" runat="server" ControlToValidate="text_company_car_code_footer" Text="*Company Code is Required" ForeColor="Red"></asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Company Car Alias"> <ItemTemplate> <asp:Label Text='<%# Eval("Company_Car_Alias") %>' runat="server" /> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="text_company_car_alias" Text='<%# Eval("Company_Car_Alias") %>' runat="server" /> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="text_company_car_alias_footer" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator3" SetFocusOnError="true" ValidationGroup="car" runat="server" ControlToValidate="text_company_car_alias_footer" Text="*Company Alias is Required" ForeColor="Red"></asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Action" HeaderStyle-Width="100px"> <ItemTemplate> <asp:LinkButton runat="server" CommandName="edit" ToolTip="edit" Text="<span class='table-icons'><i class='fas fa-edit'></i></span>" style="width:20px;margin-right:7px;"></asp:LinkButton> <asp:LinkButton runat="server" CommandName="delete" ToolTip="delete" Text="<span class='table-icons'><i class='fas fa-times'></i></span>" style="width:20px;margin-right:7px;" OnClientClick="return confirm('Are you sure you want to delete this record?');"></asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton runat="server" CommandName="Update" ToolTip="Update" Text="<span class='table-icons'><i class='far fa-save'></i></span>" style="width:20px;margin-right:7px;"></asp:LinkButton> <asp:LinkButton runat="server" CommandName="Cancel" ToolTip="cancel" Text="<span class='table-icons'><i class='fas fa-times'></i></span>" style="width:20px;margin-right:7px;"></asp:LinkButton> </EditItemTemplate> <FooterTemplate> <asp:LinkButton runat="server" ValidationGroup="car" CausesValidation="true" CommandName="addnew" ToolTip="addnew" Text="<span class='table-icons' style='font-size:22pt;'><i class='far fa-save'></i></span>"></asp:LinkButton> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:PlaceHolder runat="server" ID="company_car_admin_PlaceHolder"></asp:PlaceHolder> </div> </asp:Panel>
and this is my code for master page
<%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeBehind="WEB_SERVICE.aspx.cs" Inherits="SIRH.Layouts.SIRH.WEB_SERVICE" DynamicMasterPageFile="~masterurl/default.master" %>
Saturday, September 15, 2018 10:39 AM
Answers
-
User839733648 posted
Hi ahmedshpt,
You could just add the attribute AllowPaging="true" to your GridView.
And you may set PageSize="number" to define the number of rows to display.
Besides, you have to add the function OnPageIndexChanging to let the pagination work well.
I’ve made a sample on my side, and for more information, you could refer to the code below.
.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Student.aspx.cs" Inherits="StudentTest.Student" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentID" CssClass="table table-striped table-bordered table-condensed" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" > <Columns> <asp:BoundField DataField="StudentID" HeaderText="StudentID" InsertVisible="False" ReadOnly="True"> </asp:BoundField> <asp:TemplateField HeaderText="LastName"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FirstName"> <EditItemTemplate> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Year"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Year") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Year") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EnrollmentDate" > <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Operation" ShowDeleteButton="True" ShowEditButton="True" /> </Columns> </asp:GridView> </asp:Content>
code behind.
using System.Data; using System.Data.SqlClient; using System.Configuration; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView (); } } public void BindGridView () { string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strCon); con.Open(); SqlCommand cmd = new SqlCommand("select * from [Students]", con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { BindGridView(); GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); }
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 17, 2018 7:44 AM
All replies
-
User475983607 posted
The MaintainScrollPositionOnPostBack maintains the page scroll not a panel. You'll need to write JavaScript code that persists the panel's scroll position. A panel is just a div. So you could capture the scroll position and when the user clicks edit and place the values in a hidden field.
OI think it would be easier to use pagination rather than relying on the vertical scroll of a div.
Saturday, September 15, 2018 5:39 PM -
User-381059448 posted
thanks mgebhard
can you help me please to use pagination , sorry i'm a beginner in asp.net
Saturday, September 15, 2018 10:05 PM -
User839733648 posted
Hi ahmedshpt,
You could just add the attribute AllowPaging="true" to your GridView.
And you may set PageSize="number" to define the number of rows to display.
Besides, you have to add the function OnPageIndexChanging to let the pagination work well.
I’ve made a sample on my side, and for more information, you could refer to the code below.
.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Student.aspx.cs" Inherits="StudentTest.Student" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="StudentID" CssClass="table table-striped table-bordered table-condensed" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging" > <Columns> <asp:BoundField DataField="StudentID" HeaderText="StudentID" InsertVisible="False" ReadOnly="True"> </asp:BoundField> <asp:TemplateField HeaderText="LastName"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="FirstName"> <EditItemTemplate> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Year"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Year") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("Year") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EnrollmentDate" > <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Operation" ShowDeleteButton="True" ShowEditButton="True" /> </Columns> </asp:GridView> </asp:Content>
code behind.
using System.Data; using System.Data.SqlClient; using System.Configuration; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGridView (); } } public void BindGridView () { string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strCon); con.Open(); SqlCommand cmd = new SqlCommand("select * from [Students]", con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { BindGridView(); GridView1.PageIndex = e.NewPageIndex; GridView1.DataBind(); }
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 17, 2018 7:44 AM -
Thursday, September 20, 2018 9:55 PM