在更新面板内的回发上维护可滚动的gridview位置

时间:2013-01-15 15:28:00

标签: c# asp.net

我有一个带有overflow-y:grid的gridview。 gridview位于更新面板内,该面板包含在用户控件中。使用ajax模式弹出扩展器显示此用户控件。现在,这个gridview可能有数百个项目,如果用户必须选择一个项目,那么异步回发会再次重新加载弹出窗口并且滚动位置会丢失。我尝试使用链接http://weblogs.asp.net/andrewfrederick/archive/2008/03/04/maintain-scroll-position-after-asynchronous-postback.aspx上发布的解决方案,但没有运气。不确定我是否错放了javascript代码。

这是我有模态弹出扩展器的地方。

<asp:Panel ID="pnlShowDirectMailMedium" runat="server" Visible="false" HorizontalAlign="Center">
        <uc2:DirectMailMedium ID="ucDirectMailMedium" runat="server" OnCloseEvent="ucDirectMailMedium_CloseEvent" />
        <asp:HiddenField ID="hfCloseDMMedium" runat="server" />
    </asp:Panel>
    <cc1:ModalPopupExtender ID="mpeDirectMailMedium" runat="server" BackgroundCssClass="Modal"
        TargetControlID="hfCloseDMMedium" PopupControlID="pnlShowDirectMailMedium" OkControlID="hfCloseDMMedium"
        CancelControlID="hfCloseDMMedium" Drag="true" RepositionMode="None" />

这是我拥有gridview的实际用户控件。

<asp:Panel ID="pnlDirectMail" runat="server">
<asp:UpdatePanel ID="upDirectMail" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
<div class="ZipCodeGrid" id="divGrid">
                            <asp:GridView ID="gvZips" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvZips_RowDataBound"
                                GridLines="None" Width="870px">
                                <Columns>
                                    <asp:TemplateField HeaderText="Zip">
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:HiddenField ID="hfSelectedId" runat="server" Value='<%# Bind("SelectedId") %>' />
                                            <asp:HiddenField ID="hfID" runat="server" Value='<%# Bind("ID") %>' />
                                            <asp:Label ID="lblZip" runat="server" Text='<%# Bind("Zip") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lblDescription" runat="server" Text="City" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lblSegmentDescHeader" runat="server" Text="Segment" Visible="false" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblSegment" runat="server" Text='<%# Bind("SegmentDesc") %>' Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl1Acre50KHeader" runat="server" Text="1 Acre (50K)" Visible="false" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl1Acre50KQty" runat="server" Text='<%# Bind("One50K") %>' Width="50px"
                                                Visible="false" />
                                            <asp:CheckBox ID="chk1Acre50K" runat="server" Checked='<%# Bind("One50KSelected") %>'
                                                OnCheckedChanged="chk1Acre50K_CheckChanged" AutoPostBack="true" Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl1Acre50KPriceHeader" runat="server" Text="Cost" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl1Acre50KCost" runat="server" Text='<%# Bind("One50KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl1Acre100KHeader" runat="server" Text="1 Acre (100K)" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl1Acre100KQty" runat="server" Text='<%# Bind("One100K") %>' Width="50px"
                                                Visible="false" />
                                            <asp:CheckBox ID="chk1Acre100K" runat="server" Checked='<%# Bind("One100KSelected") %>'
                                                OnCheckedChanged="chk1Acre100K_CheckChanged" AutoPostBack="true" Visible="false" />
                                            <asp:HiddenField ID="hf1Acre100KCost" runat="server" Value='<%# Bind("One100KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl2Acre50KHeader" runat="server" Text="Prospect 2Acre+" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl2Acre50KQty" runat="server" Text='<%# Bind("Two50K") %>' Width="50px" />
                                            <asp:CheckBox ID="chk2Acre50K" runat="server" Checked='<%# Bind("Two50KSelected") %>'
                                                OnCheckedChanged="chk2Acre50K_CheckChanged" AutoPostBack="true" />
                                            <asp:HiddenField ID="hf2Acre50KPrice" runat="server" Value='<%# Bind("Two50KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl2Acre100KHeader" runat="server" Text="Prospect 3Acre+" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl2Acre100KQty" runat="server" Text='<%# Bind("Two100K") %>' Width="50px" />
                                            <asp:CheckBox ID="chk2Acre100K" runat="server" Checked='<%# Bind("Two100KSelected") %>'
                                                OnCheckedChanged="chk2Acre100K_CheckChanged" AutoPostBack="true" />
                                            <asp:HiddenField ID="hf2Acre100KPrice" runat="server" Value='<%# Bind("Two100KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl3Acre50KHeader" runat="server" Text="3 Acre (50K)" Visible="false" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl3Acre50KQty" runat="server" Text='<%# Bind("Three50K") %>' Visible="false"
                                                Width="50px" />
                                            <asp:CheckBox ID="chk3Acre50K" runat="server" Checked='<%# Bind("Three50KSelected") %>'
                                                OnCheckedChanged="chk3Acre50K_CheckChanged" AutoPostBack="true" Visible="false" />
                                            <asp:HiddenField ID="hf3Acre50KPrice" runat="server" Value='<%# Bind("Three50KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lbl3Acre100KHeader" runat="server" Text="3 Acre (100K)" Visible="false" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lbl3Acre100KQty" runat="server" Text='<%# Bind("Three100K") %>' Width="50px"
                                                Visible="false" />
                                            <asp:CheckBox ID="chk3Acre100K" runat="server" Checked='<%# Bind("Three100KSelected") %>'
                                                OnCheckedChanged="chk3Acre100K_CheckChanged" AutoPostBack="true" Visible="false" />
                                            <asp:HiddenField ID="hf3Acre100KPrice" runat="server" Value='<%# Bind("Three100KCost", "{0:$###,###,##0.00}") %>' />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:LinkButton ID="lblOccupiedHousingUnit" runat="server" Text="Housing Unit" Visible="false"
                                                ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtOccupiedHousingUnit" runat="server" Text='<%# Bind("OccupiedHousingUnit") %>'
                                                Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:LinkButton ID="lblHouseholdIncome" runat="server" Text="Household Income" Visible="false"
                                                ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtHouseholdIncome" runat="server" Text='<%# Bind("HouseholdIncome", "{0:$###,###,##0.00}") %>'
                                                Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:LinkButton ID="lblDistancetoSite" runat="server" Text="Distance" Visible="false"
                                                ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtDistancetoSite" runat="server" Text='<%# Bind("DistancetoSite") %>'
                                                Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:LinkButton ID="lblAverageIncome" runat="server" Text="Home Value" Visible="false"
                                                ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtAverageIncome" runat="server" Text='<%# Bind("AverageIncome", "{0:$###,###,##0.00}") %>'
                                                Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lblRuralPct" runat="server" Text="% Rural" Visible="false" ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtRuralPct" runat="server" Text='<%# Bind("RuralPct") %>' Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lblLawnGarden" runat="server" Text="Lawn & Garden" Visible="false"
                                                ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtLawnGarden" runat="server" Text='<%# Bind("LawnGarden") %>' Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <HeaderStyle HorizontalAlign="Center" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderTemplate>
                                            <asp:Label ID="lblLawnGardenEquipment" runat="server" Text="Lawn & Garden Equipment"
                                                Visible="false" ForeColor="White" />
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="txtLawnGardenEquipment" runat="server" Text='<%# Bind("LawnGardenEquipment") %>'
                                                Visible="false" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <HeaderStyle CssClass="HeaderFreez" />
                                <AlternatingRowStyle BackColor="#E4E4E4" />
                                <RowStyle BackColor="#F7F7F7" />
                            </asp:GridView>
                        </div>
</ContentTemplate>
</asp:UpdatePanel>

有人可以让我了解该怎么做。仅供参考 - 剧本管理员在主页面上。

2 个答案:

答案 0 :(得分:2)

你需要这样做,

完整详情:Maintain Scrollbar Position Inside UpdatePanel After Partial PostBack

您需要记下javascript,这是维护滚动位置的图像的一部分

enter image description here

答案 1 :(得分:1)

由于您的gridview位于用户控件内部,因此它是添加用于维护滚动位置的代码的适当位置。

值得一提的一件重要事情是,DOM完全加载后需要执行javascript,这可以通过以下方式完成:

  1. 使用@Pranay Rana的方法并将脚本放在ScriptManager控件
  2. 之后
  3. 添加对jQuery的引用并使用$(document).ready()...
  4. 以下是一个如何完成此操作的简单示例,我相信您可以相应地修改它以适用于您的方案: - )

    <强> ASPX:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs"
        Inherits="WebApplication16.UserControl1" %>
    <script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var xPos, yPos;
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_beginRequest(BeginRequestHandler);
            prm.add_endRequest(EndRequestHandler);
    
            function BeginRequestHandler(sender, args) {
                xPos = $get('scrollDiv').scrollLeft;
                yPos = $get('scrollDiv').scrollTop;
            }
    
            function EndRequestHandler(sender, args) {
                $get('scrollDiv').scrollLeft = xPos;
                $get('scrollDiv').scrollTop = yPos;
            }
        });
    </script>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div id="scrollDiv" style="overflow:auto;height: 100px;" />
            <asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:CommandField ShowSelectButton="true" />
                    <asp:BoundField DataField="Name" />
                </Columns>
            </asp:GridView>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    代码背后:

    public partial class UserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                gvEmployees.DataSource = new List<Employee>
                {
                    new Employee{Name="Employee 1"},
                    new Employee{Name="Employee 2"},
                    new Employee{Name="Employee 3"},
                    new Employee{Name="Employee 4"},
                    new Employee{Name="Employee 5"},
                    new Employee{Name="Employee 6"},
                    new Employee{Name="Employee 7"},
                };
                gvEmployees.DataBind();
            }
        }
    }
    
    public class Employee
    {
        public string Name { get; set; }
    }