asp gridview滚动不断跳到顶部

时间:2015-04-13 15:57:14

标签: c# asp.net

我尝试在互联网上发布数以千计的代码。没有错误,但它仍然不起作用。当我在gridview中编辑或更新时,它只是一直跳到顶部。 (垂直滚动条自动滚动到第一条记录。)请帮助。谢谢!

下面是我的代码:

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StaceyCost.aspx.cs" Inherits="WebApplication1.StaceyCost"  MaintainScrollPositionOnPostback="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
   div.scroll {
         width: 1850px;
         height: 630px;
         overflow: scroll;

        }


    </style>
</head>
<body style="background-image:url(/images/bg4.jpg)">
    <form id="form1" runat="server">
          <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <div>
      <h1>&nbsp;</h1>
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h1>
        <h1>&nbsp;Project Cost</h1>
        <p>&nbsp;</p>
        <p>
            &nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Font-Size="Medium" Text="Client:"></asp:Label>
&nbsp;&nbsp;
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                <asp:ListItem>AAH</asp:ListItem>
                <asp:ListItem>ALLY</asp:ListItem>
                <asp:ListItem>AMREF</asp:ListItem>
                <asp:ListItem>CAP</asp:ListItem>
                <asp:ListItem>CSS</asp:ListItem>
                <asp:ListItem>ICSF</asp:ListItem>
                <asp:ListItem>IPPF</asp:ListItem>
                <asp:ListItem>JTS</asp:ListItem>
                <asp:ListItem>NUL</asp:ListItem>
                <asp:ListItem>NYRP</asp:ListItem>
                <asp:ListItem>PFTH</asp:ListItem>
                <asp:ListItem>QLF</asp:ListItem>
                <asp:ListItem>SJS</asp:ListItem>
                <asp:ListItem>TCS</asp:ListItem>
                <asp:ListItem>WPHC</asp:ListItem>
                <asp:ListItem>VAC</asp:ListItem>
            </asp:DropDownList>
        </p>
         <table width="1850" border="0">
             <tr>
             <td style="vertical-align:top;text-align:center">

     <input type="hidden" id="hdnScrollTop" runat="server" value="0" />
                  <div class="scroll">
                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Project_ID" DataSourceID="SqlDataSource1" ShowHeaderWhenEmpty="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                     <Columns>
                         <asp:TemplateField ShowHeader="False" HeaderStyle-Width ="80px" ItemStyle-Wrap="false">
                             <EditItemTemplate>
                                 <asp:Linkbutton ID="Linkbutton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Linkbutton>
                                 &nbsp;<asp:Linkbutton ID="Linkbutton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Linkbutton>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:Button>
                             </ItemTemplate>

<HeaderStyle Width="80px"></HeaderStyle>

<ItemStyle Wrap="False"></ItemStyle>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Project Number" InsertVisible="False" SortExpression="Project_ID">
                             <EditItemTemplate>
                                 <asp:Label ID="Label1" runat="server" Text='<%# Eval("Project_ID") %>' Width ="80px" ></asp:Label>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label2" runat="server" Text='<%# Bind("Project_ID") %>' Width ="80px" ></asp:Label>
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Campaign Name" SortExpression="Campaign_Name">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Campaign_Name") %>' Width ="400px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label3" runat="server" Text='<%# Bind("Campaign_Name") %>' Width ="400px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertCampaign_Name" runat="server" Width ="400px"  ></asp:TextBox>
                             </FooterTemplate>
                             <ItemStyle HorizontalAlign="Left" />
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Drop Date" SortExpression="Drop_Date">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Drop_Date", "{0:yyyy-MM-dd}") %>' TextMode="Date" Width ="130px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label1" runat="server" Text='<%# Bind("Drop_Date","{0:d}") %>' Width ="130px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertDropDate" runat="server" Width ="130px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="House Qty" SortExpression="House_Qty">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("House_Qty", "{0:#,0}") %>' Width ="80px" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label4" runat="server" Text='<%# Bind("House_Qty", "{0:#,0}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertHouse_Qty" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="House Cost" SortExpression="House_Cost">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("House_Cost" )%>' Width ="80px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label5" runat="server" Text='<%# Bind("House_Cost", "{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertHouse_Cost" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Acq Qty" SortExpression="Acq_Qty">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Acq_Qty", "{0:#,0}") %>' Width ="80px" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label6" runat="server" Text='<%# Bind("Acq_Qty", "{0:#,0}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertAcq_Qty" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Acq Cost" SortExpression="Acq_Cost">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Acq_Cost") %>' Width ="80px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label7" runat="server" Text='<%# Bind("Acq_Cost", "{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertAcq_Cost" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="List Cost" SortExpression="List_Cost">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("List_Cost") %>' Width ="80px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label8" runat="server" Text='<%# Bind("List_Cost", "{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertList_Cost" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Data Cost" SortExpression="Data_Cost">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Data_Cost") %>' Width ="80px" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label9" runat="server" Text='<%# Bind("Data_Cost", "{0:c2}") %>' Width ="80px" ></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertData_Cost" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Total Qty" SortExpression="Total_Qty">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Total_Qty", "{0:#,0}") %>' Width ="80px" onkeyup = "javascript:this.value=Comma(this.value);"  ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label10" runat="server" Text='<%# Bind("Total_Qty", "{0:#,0}") %>' Width ="80px" ></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertTotal_Qty" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Total Cost" SortExpression="Total_Cost">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("Total_Cost") %>' Width ="80px" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label11" runat="server" Text='<%# Bind("Total_Cost", "{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertTotal_Cost" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="House CPP" SortExpression="House_CPP">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("House_CPP") %>' Width ="80px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label12" runat="server" Text='<%# Bind("House_CPP","{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertHouse_CPP" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Acq CPP" SortExpression="Acq_CPP">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("Acq_CPP") %>' Width ="80px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label13" runat="server" Text='<%# Bind("Acq_CPP","{0:c2}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertAcq_CPP" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Data Team Qty" SortExpression="Datateam_Qty">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("Datateam_Qty", "{0:#,0}") %>' Width ="80px" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label14" runat="server" Text='<%# Bind("Datateam_Qty", "{0:#,0}") %>' Width ="80px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertDatateam_Qty" runat="server" Width ="80px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Qty Difference" SortExpression="Qty_Diff">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox14" runat="server" Text='<%# Bind("Qty_Diff", "{0:#,0}") %>' Width ="60px" onkeyup = "javascript:this.value=Comma(this.value);" ></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label15" runat="server" Text='<%# Bind("Qty_Diff", "{0:#,0}") %>' Width ="60px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertQty_Diff" runat="server" Width ="60px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Note" SortExpression="Note1">
                             <EditItemTemplate>
                                 <asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("Note1") %>' Width ="120px"></asp:TextBox>
                             </EditItemTemplate>
                             <ItemTemplate>
                                 <asp:Label ID="Label16" runat="server" Text='<%# Bind("Note1") %>' Width ="120px"></asp:Label>
                             </ItemTemplate>
                             <FooterTemplate>
                                 <asp:TextBox ID="txtInsertNote1" runat="server" Width ="120px" ></asp:TextBox>
                             </FooterTemplate>
                         </asp:TemplateField>
                     </Columns>
                     <HeaderStyle Font-Size="Medium" />
                 </asp:GridView>
              </div>
             </td>
             </tr>
         </table>
    </div>

<script language="javascript" type="text/javascript">
    function Comma(Num) { //function to add commas to textboxes
        Num += '';
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
        x = Num.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1))
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        return x1 + x2;
    }
</script>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:productionConnectionString %>" DeleteCommand="DELETE FROM [project] WHERE [Project_ID] = @Project_ID" InsertCommand="INSERT INTO [project] ([Campaign_Name], [Drop_Date], [House_Qty], [House_Cost], [Acq_Qty], [Acq_Cost], [List_Cost], [Data_Cost], [Total_Qty], [Total_Cost], [House_CPP], [Acq_CPP], [Datateam_Qty], [Qty_Diff], [Note1]) VALUES (@Campaign_Name, @Drop_Date, @House_Qty, @House_Cost, @Acq_Qty, @Acq_Cost, @List_Cost, @Data_Cost, @Total_Qty, @Total_Cost, @House_CPP, @Acq_CPP, @Datateam_Qty, @Qty_Diff, @Note1)" SelectCommand="SELECT [Project_ID], [Campaign_Name], [Drop_Date], [House_Qty], [House_Cost], [Acq_Qty], [Acq_Cost], [List_Cost], [Data_Cost], [Total_Qty], [Total_Cost], [House_CPP], [Acq_CPP], [Datateam_Qty], [Qty_Diff], [Note1] FROM [project] WHERE (([Client] = @Client) AND ([Complete] = @Complete)) ORDER BY [Project_ID] DESC" UpdateCommand="UPDATE [project] SET [Campaign_Name] = @Campaign_Name, [Drop_Date] = @Drop_Date, [House_Qty] = @House_Qty, [House_Cost] = @House_Cost, [Acq_Qty] = @Acq_Qty, [Acq_Cost] = @Acq_Cost, [List_Cost] = @List_Cost, [Data_Cost] = @Data_Cost, [Total_Qty] = @Total_Qty, [Total_Cost] = @Total_Cost, [House_CPP] = @House_CPP, [Acq_CPP] = @Acq_CPP, [Datateam_Qty] = @Datateam_Qty, [Qty_Diff] = @Qty_Diff, [Note1] = @Note1 WHERE [Project_ID] = @Project_ID">
            <DeleteParameters>
                <asp:Parameter Name="Project_ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Campaign_Name" Type="String" />
                <asp:Parameter DbType="Date" Name="Drop_Date" />
                <asp:Parameter Name="House_Qty" Type="Double" />
                <asp:Parameter Name="House_Cost" Type="Decimal" />
                <asp:Parameter Name="Acq_Qty" Type="Double" />
                <asp:Parameter Name="Acq_Cost" Type="Decimal" />
                <asp:Parameter Name="List_Cost" Type="Decimal" />
                <asp:Parameter Name="Data_Cost" Type="Decimal" />
                <asp:Parameter Name="Total_Qty" Type="Double" />
                <asp:Parameter Name="Total_Cost" Type="Decimal" />
                <asp:Parameter Name="House_CPP" Type="Decimal" />
                <asp:Parameter Name="Acq_CPP" Type="Decimal" />
                <asp:Parameter Name="Datateam_Qty" Type="Double" />
                <asp:Parameter Name="Qty_Diff" Type="Double" />
                <asp:Parameter Name="Note1" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Client" PropertyName="SelectedValue" Type="String" />
                <asp:Parameter DefaultValue="true" Name="Complete" Type="Boolean" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Campaign_Name" Type="String" />
                <asp:Parameter DbType="Date" Name="Drop_Date" />
                <asp:Parameter Name="House_Qty" Type="Double" />
                <asp:Parameter Name="House_Cost" Type="Decimal" />
                <asp:Parameter Name="Acq_Qty" Type="Double" />
                <asp:Parameter Name="Acq_Cost" Type="Decimal" />
                <asp:Parameter Name="List_Cost" Type="Decimal" />
                <asp:Parameter Name="Data_Cost" Type="Decimal" />
                <asp:Parameter Name="Total_Qty" Type="Double" />
                <asp:Parameter Name="Total_Cost" Type="Decimal" />
                <asp:Parameter Name="House_CPP" Type="Decimal" />
                <asp:Parameter Name="Acq_CPP" Type="Decimal" />
                <asp:Parameter Name="Datateam_Qty" Type="Double" />
                <asp:Parameter Name="Qty_Diff" Type="Double" />
                <asp:Parameter Name="Note1" Type="String" />
                <asp:Parameter Name="Project_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

    </form>
</body>
</html>

我尝试在互联网上发布数以千计的代码。但它仍然无法正常工作。当我在gridview中编辑或更新时,它只是一直跳到顶部。 (垂直滚动条自动滚动到第一条记录。)请帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为该页面正在进行完整的回发。如果是这样,请在@Page指令中设置MaintainScrollPositionOnPostBack="true"。这将使页面在回发后保持在该位置。