0

我有一个大型数据集绑定到一个 300 像素高度限制的 DIV 标记内的网格视图。当我选择一行并触发编辑功能时,页面将数据(根据需要)重新加载到 gridview 中,无论我向下滚动多远,都会将我带回顶部。

有没有一种理想的方式只使用 asp.net c# 来关注正在编辑的行。如果需要,我不介意这是否涉及在编辑模式下将行放置在网格顶部。

下面是 CountryGrid.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPageSimple.master" AutoEventWireup="true"
    CodeFile="CountryGrid.aspx.cs" EnableEventValidation="false" Inherits="CountryGrid"
    Title="JFA Admin Portal - OP49 Country Update" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>
            <center>
            <div class="headlanding">
                           <asp:Label ID="lb_Welcome" runat="server" CssClass="labelLargeFont" Text="Label">OP49 - Country Update</asp:Label>
                        <br />
                        <br />
                        <br />
                            <asp:Label ID="Label4" runat="server" CssClass="labelLargeFont" >You are currently modifying: </asp:Label> 
                            <asp:Label ID="Country" runat="server" CssClass="labelLargeFont" />
                        <br />
                            <asp:Label ID="Label1" runat="server" CssClass="labelLargeFont" >For </asp:Label> 
                            <asp:Label ID="Season" runat="server" CssClass="labelLargeFont" />
                        <br />
                        <br />
                    </div>                                 
                    <br />
                    <div style="text-align: center">
                        <asp:Label ID="Error_Dashboard" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>
                        <asp:Label ID="ErrorAccess" runat="server" Font-Size="Small" ForeColor="#FF3300"></asp:Label>

                </center>
                </div>    

                <asp:Panel ID="Panel1" runat="server" Font-Size="Large" CssClass="Grid_Panel" GroupingText="OP49 - Update Grid" Font-Bold="true">
                    <center>
                        <div style="max-height: 550px; overflow: auto;">
                        <asp:GridView ID="CountryGridView" DataKeyNames="i_SK_Accom" runat="server" Postback="False"
                            AutoGenerateColumns="False" CssClass="mGrid" RowStyle-CssClass="normal" Width="95%" 
                            OnRowEditing="CountryGridView_RowEditing" 
                            OnRowCancelingEdit="CountryGridView_RowCancelingEdit" 
                            OnRowUpdating="CountryGridView_RowUpdating" >
                            <Columns>
                                <asp:TemplateField HeaderText="Accom Code">
                                    <ItemTemplate>
                                        <asp:Label ID="lb_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="lbl_Accom_Code" runat="server" Text='<%# Bind("Accom_Code") %>' />
                                    </EditItemTemplate>
                                    <HeaderStyle Width="80px" />
                                    <ItemStyle Width="80px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Accom Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lb_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:Label ID="lbl_Accom_Name" runat="server" Text='<%# Bind("Accom_Name") %>' />
                                    </EditItemTemplate>
                                    <HeaderStyle Width="200px" />
                                    <ItemStyle Width="200px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="OP49 Required?">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="txt_OP49" runat="server" SelectedValue='<%# Bind("OP49_Required") %>' CssClass="DropBoxYN">
                                        <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                        <asp:ListItem Text="No" Value="N"></asp:ListItem>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lb_OP49" runat="server" Text='<%# Bind("OP49_Required") %>' />
                                </ItemTemplate>
                                <HeaderStyle Width="100px" />
                                <ItemStyle Width="100px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Weekly">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="txt_Weekly" runat="server" SelectedValue='<%# Bind("Weekly") %>'  CssClass="DropBoxYN">
                                        <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                        <asp:ListItem Text="No" Value="N"></asp:ListItem>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lb_Weekly" runat="server" Text='<%# Bind("Weekly") %>' />
                                </ItemTemplate>
                                <HeaderStyle Width="60px" />
                                <ItemStyle Width="60px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Daily">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="txt_Daily" runat="server" SelectedValue='<%# Bind("Daily") %>' CssClass="DropBoxYN">
                                        <asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
                                        <asp:ListItem Text="No" Value="N"></asp:ListItem>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lb_Daily" runat="server" Text='<%# Bind("Daily") %>' />
                                </ItemTemplate>
                                <HeaderStyle Width="60px" />
                                <ItemStyle Width="60px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Actions" >
                                <ItemTemplate>
                                    <asp:ImageButton ID="bt_Edit" runat="server" CommandName="Edit" AlternateText="Edit" ImageUrl="~/Images/Edit.gif" CssClass="ImageButton" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                        <asp:ImageButton ID="bt_Update" runat="server" CommandName="Update" AlternateText="Update" ImageUrl="~/Images/save.png" CssClass="ImageButton" />
                                        <asp:ImageButton ID="bt_Cancel" runat="server" CommandName="Cancel" AlternateText="Cancel" ImageUrl="~/Images/cancel.png" CssClass="ImageButton" />
                                </EditItemTemplate>
                                <HeaderStyle Width="60px" />
                                <ItemStyle Width="60px" Wrap="False" />
                            </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        </div>
                        <br />
                            <asp:ImageButton ID="Home" runat="server" ImageUrl="~/Images/home.png" CssClass="ImageButtonLarge" AlternateText="Home" ToolTip="Home" OnClick="Click_Home" />
                    </center>
                </asp:Panel>        
        </ContentTemplate>
        </asp:UpdatePanel>
</asp:Content>

页面背后的代码:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class CountryGrid : System.Web.UI.Page
{
    SnowballInterface conn = new SnowballInterface();
    SnowballInterface CurSeason = new SnowballInterface();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Request["i_FK_CountryID"] == null)
        {
            Response.Redirect("Default.aspx");
        }

        #region Get Accom Data
        SqlCommand EditCOComm = new SqlCommand("IFACE_JFA_COUNTRY", conn.sbConn);
        EditCOComm.CommandType = CommandType.StoredProcedure;

        EditCOComm.Parameters.Add("@Statement", SqlDbType.Char).Value = "CountryDetails";
        EditCOComm.Parameters.Add("@i_FK_CountryID", SqlDbType.Int).Value = Request["i_FK_CountryID"].Trim().ToString();

        try
        {
            conn.sbConn.Open();
            SqlDataReader EditCOReader;
            EditCOReader = EditCOComm.ExecuteReader();
            // Download default values
            while (EditCOReader.Read())
            {
                Country.Text = Convert.ToString(EditCOReader["Country_Name"]).Trim();
                Season.Text = Convert.ToString(EditCOReader["Season_Name"]).Trim();
            }

            EditCOReader.Close();
        }
        catch (Exception)
        {
        }
        finally
        {
            conn.sbConn.Close();
        }
        #endregion

        if (!IsPostBack)
        {
            BindCountryGrid();
        }
    }

    protected void CountryGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        CountryGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindCountryGrid();            
    }

    protected void CountryGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        CountryGridView.EditIndex = -1;
        //Bind data to the GridView control.
        BindCountryGrid();
    }

    protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label Textbox_Accom = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
        Label Textbox_Accom_Name = (Label)CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
        DropDownList Textbox_OP49 = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_OP49");
        DropDownList Textbox_Weekly = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Weekly");
        DropDownList Textbox_Daily = (DropDownList)CountryGridView.Rows[e.RowIndex].FindControl("txt_Daily");

        SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
        commEditConsultant.CommandType = CommandType.StoredProcedure;

            commEditConsultant.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
            commEditConsultant.Parameters.Add("@Page", SqlDbType.VarChar).Value = "OP49";
            commEditConsultant.Parameters.Add("@PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
            commEditConsultant.Parameters.Add("@Season_Name", SqlDbType.VarChar).Value = Season.Text;
            commEditConsultant.Parameters.Add("@Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
            commEditConsultant.Parameters.Add("@i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
            commEditConsultant.Parameters.Add("@OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
            commEditConsultant.Parameters.Add("@Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
            commEditConsultant.Parameters.Add("@Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;

            conn.sbConn.Open();
                commEditConsultant.ExecuteNonQuery();
                CountryGridView.EditIndex = -1;
                Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
            conn.sbConn.Close();    
                BindCountryGrid();


    }

    private void BindCountryGrid()
    {
        SqlCommand CountryGridSelect = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
        CountryGridSelect.CommandType = CommandType.StoredProcedure;
        CountryGridSelect.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "CountryGridSelect";
        CountryGridSelect.Parameters.Add("@i_FK_CountryID", SqlDbType.VarChar).Value = Request["i_FK_CountryID"].Trim().ToString();

        try
        {
            conn.sbConn.Open();
            SqlDataReader CountryGridSelectReader;
            CountryGridSelectReader = CountryGridSelect.ExecuteReader();
            CountryGridView.DataSource = CountryGridSelectReader;
            CountryGridView.DataBind();
            CountryGridSelectReader.Close();
        }
        finally
        {
            conn.sbConn.Close();
        }
    }

    protected void Click_Home(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?i_FK_SeasonID=" + Request["i_FK_SeasonID"].Trim().ToString());
    }    

}
4

1 回答 1

3

您可以 在您的 aspx 页面上的页面指令中将MaintanScrollPositionOnPostback设置为 true。

<%@ Page Language="C#" AutoEventWireup="true" MaintainScrollPositionOnPostback="true" CodeBehind="Gridview.aspx.cs" Inherits="TestWebsite.Gridview" %>
于 2013-04-23T07:03:41.843 回答