为什么页面会回滚选定的索引更改事件?

时间:2014-11-06 11:02:07

标签: c# asp.net .net forms aspxcombobox

此表单工作正常,但每当我选择下拉框时它会滚动,它会滚动到顶部。

<asp:UpdatePanel ID="updGridViewSMS" runat="server" UpdateMode="Conditional">
          <ContentTemplate>
            <label><b>Search By Date Range</b></label>
            <asp:Label ID="lblDateFrom" runat="server" Text="From"></asp:Label>
            <asp:TextBox ID="txtFromDate" runat="server" ></asp:TextBox>                      
            <asp:CalendarExtender ID="CalendarExtenderFromDate" Format="dd/MMM/yyyy" TargetControlID="txtFromDate" runat="server">
            </asp:CalendarExtender>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                                ControlToValidate="txtFromDate" Display="None" ErrorMessage="" 
                                ForeColor="Red" >
            </asp:RequiredFieldValidator>
            <asp:Label ID="lblDateTo" runat="server" Text="To"></asp:Label>
            <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
            <asp:CalendarExtender ID="CalendarExtenderToDate" Format="dd/MMM/yyyy" TargetControlID="txtToDate" runat="server">
            </asp:CalendarExtender>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                                ControlToValidate="txtToDate" Display="None" ErrorMessage="" 
                                ForeColor="Red" >
            </asp:RequiredFieldValidator>
            <asp:Button ID="btnSearchByDate"  CssClass="btn btn-success" runat="server" Text="Search" 
                        ClientIDMode="Static"  OnClick="btnSearchByDate_Click" />
            &nbsp; 
            <%--<asp:Button ID="btnEdit"  CssClass="btn btn-success" runat="server" Text="Edit" 
                        ClientIDMode="Static" OnClick="btnEdit_Click"  />--%>

            <asp:ValidationSummary
             HeaderText="Requires Date Range:"
             DisplayMode="SingleParagraph"
             EnableClientScript="true"
             ForeColor="Red"
             runat="server" ID="ValidationSummary1"/>
           <label runat="server" id="lblSelectionMessage" style="color:navy; font-size:12px; font-style:italic" visible="false">

           </label>
           <asp:GridView ID="GridViewSmsComplaints" AllowPaging="True" PageSize="5" runat="server" AutoGenerateColumns="False"   Width="1085px" OnRowDataBound="GridViewSmsComplaints_RowDataBound" OnPageIndexChanging="GridViewSmsComplaints_PageIndexChanging" CellPadding="4" Height="417px" ForeColor="#333333" GridLines="None" >
               <FooterStyle BackColor="#990000" ForeColor="White" Font-Bold="True" />
               <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
               <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <PagerTemplate>
            <table>
                <tr>
                    <td>
                        <asp:LinkButton ID="lnkFirstPage" CommandName="Page" CommandArgument="First" runat="server">First</asp:LinkButton>
                    </td>
                    <td>
                        <asp:LinkButton ID="lnkNextPage" CommandName="Page" CommandArgument="Next" runat="server">Next</asp:LinkButton>
                    </td>
                    <td>
                        <asp:LinkButton ID="lnkPrevPage" CommandName="Page" CommandArgument="Prev" runat="server">Previous</asp:LinkButton>
                    </td>
                    <td>
                        <asp:LinkButton ID="lnkLastPage" CommandName="Page" CommandArgument="Last" runat="server">Last</asp:LinkButton>
                    </td>
                </tr>
            </table>
        </PagerTemplate>
               <AlternatingRowStyle BackColor="White" />
               <Columns>
                    <asp:BoundField HeaderText="Sms ID" DataField="ID" /> 
                    <asp:BoundField HeaderText="Recieving Date" DataField="RecievingDate" />  
                    <%--<asp:BoundField HeaderText="ToMobileNo" DataField="ToMobileNo" />  --%>
                    <asp:BoundField HeaderText="Complainant Mob No." DataField="FromMobileNo" /> 
                    <asp:BoundField HeaderText="Complaint" DataField="Message" >   
                     <ItemStyle Wrap="True" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Complete/InComplete">
                     <ItemTemplate>
                       <%--<asp:CheckBox ID="ckboxIsComplaint"  runat="server" />--%>
                       <asp:DropDownList ID="ddlIsComplaint" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlIsComplaint_SelectedIndexChanged">
                        <asp:ListItem Text="-Select-" Value="-1"></asp:ListItem>
                        <asp:ListItem Text="InComplete" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Complete" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Inappropriate" Value="-3"></asp:ListItem>
                       </asp:DropDownList>
                     </ItemTemplate>
                    </asp:TemplateField>
               </Columns>
               <RowStyle ForeColor="#333333" BackColor="#FFFBD6" />
               <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
               <SortedAscendingCellStyle BackColor="#FDF5AC" />
               <SortedAscendingHeaderStyle BackColor="#4D0000" />
               <SortedDescendingCellStyle BackColor="#FCF6C0" />
               <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>    

        <br />
      <asp:Panel ID="pnlBoxesDropDowns" runat="server">
        <label>Complainant</label>
        <asp:TextBox ID="txtComplainant" runat="server" ></asp:TextBox>
        <asp:RequiredFieldValidator ID="ReqFieldValdiatorComplainant" runat="server" ControlToValidate="txtComplainant" ErrorMessage="Complainant is Required" ForeColor="Red" SetFocusOnError="True" ValidationGroup="Complaints">Complainant is Required
        </asp:RequiredFieldValidator>
        <label>Relevant Region</label>
        <asp:DropDownList ID="ddlRegions" runat="server" CssClass="DropDown_Width" Width="147px" OnSelectedIndexChanged="ddlRegions_SelectedIndexChanged" AppendDataBoundItems="True"  AutoPostBack="true" >
          <asp:ListItem Value="-1" Selected="True">-Select-</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="ReqFieldValidatorRegions" runat="server" 
         ControlToValidate="ddlRegions" ErrorMessage="Region is Required" InitialValue="-1"
         ForeColor="Red" ValidationGroup="Complaints">Region is Required</asp:RequiredFieldValidator>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        <asp:Label ID="lblRelevantDistrict" runat="server">District</asp:Label> 
        <asp:DropDownList ID="ddlDistricts" runat="server" CssClass="DropDown_Width" Width="127px" OnSelectedIndexChanged="ddlDistricts_SelectedIndexChanged" AutoPostBack="true">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="ReqFieldValidatorDistricts" runat="server" 
         ControlToValidate="ddlDistricts" ErrorMessage="Region is Required" InitialValue="-1"
         ForeColor="Red" ValidationGroup="Complaints">District is Required</asp:RequiredFieldValidator>
       <label>Relevant P.Station</label> 
        <asp:DropDownList ID="ddlPoliceStations" AutoPostBack="true" runat="server" Width="147px" CssClass="DropDown_Width" OnSelectedIndexChanged="ddlPoliceStations_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="ReqFieldValidatorPoliceStations" runat="server" 
         ControlToValidate="ddlPoliceStations" ErrorMessage="Police Station is Required" InitialValue="-1"
         ForeColor="Red" ValidationGroup="Complaints">Police Station is Required</asp:RequiredFieldValidator>
          <asp:label ID="lblPriority" runat="server">Priority</asp:label>
          <asp:DropDownList ID="ddlPriority" runat="server" Width="127px">
              <asp:ListItem Text="Top" Value="1"></asp:ListItem>
              <asp:ListItem Text="Normal" Value="2"></asp:ListItem>
          </asp:DropDownList>
          &nbsp;
        </asp:Panel>
        <br />
        <br />
            <asp:Timer runat="server" Enabled="false" Interval="240000" ID="RefreshSmsComplaints" OnTick="RefreshSmsComplaints_Tick" />
          </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="RefreshSmsComplaints" EventName="Tick" />
            </Triggers>
         </asp:UpdatePanel>
        <asp:Button ID="btnDone" ValidationGroup="Complaints"  runat="server" CssClass="btn btn-success"  
            OnClick="btnDone_Click" Text="Done" />

代码隐藏:

    protected void ddlRegions_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Loads Districts Drop Down List When Relevant Region is Clicked
        ddlDistricts.Visible = true;
        lblRelevantDistrict.Visible = true;
        GridViewSmsComplaints.Visible = true;

        try
        {
            if (ddlDistricts.SelectedValue == "0")
            {
                ddlDistricts.SelectedValue = "-1";
            }
            else
            {
                ddlDistricts.DataTextField = "Name";
                ddlDistricts.DataValueField = "DistrictID";
                ddlDistricts.DataSource = mngRecievedMsg.FillDropDown_Districts(Convert.ToInt16(ddlRegions.SelectedValue));
                ddlDistricts.DataBind();
                ListItem list = new ListItem("-Select-", "-1");
                ddlDistricts.Items.Insert(0, list);
            }
        }
        catch (Exception) 
        {

        }

    }
    protected void ddlDistricts_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Loads police Stations Drop Down List When Relevant Region is Clicked

        updGridViewSMS.Visible = true; 
        try
        {
            if (ddlPoliceStations.SelectedValue == "0")
            {
                ddlPoliceStations.SelectedValue = "-1";
            }
            else
            {
                ddlPoliceStations.DataTextField = "Name";
                ddlPoliceStations.DataValueField = "ID";
                ddlPoliceStations.DataSource = mngRecievedMsg.GetHomePoliceStation(Convert.ToInt16(ddlDistricts.SelectedValue));
                ddlPoliceStations.DataBind();
                ListItem list_2 = new ListItem("-Select-", "-1");
                ddlPoliceStations.Items.Insert(0, list_2);
            }
        }
        catch (Exception) 
        {

        }


    }

    protected void ddlPoliceStations_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblPriority.Visible = true;
        ddlPriority.Visible = true;
        ddlDistricts.Visible = true;
    }

3 个答案:

答案 0 :(得分:0)

我建议你使用ajax级联下拉列表

通过使用它,您可以在没有任何回发启动的情况下实现绑定

跟进此链接

http://www.ajaxcontroltoolkit.com/CascadingDropDown/CascadingDropDown.aspx

希望这可以帮助你

答案 1 :(得分:0)

简单地将以下属性添加到&lt;%@ Page&gt;标签

  

MaintainScrollPositionOnPostback =&#34;真&#34;

<%@ Page Title="" MaintainScrollPositionOnPostback="true" Language="C#">

答案 2 :(得分:-2)

确定。

如果您仅在IE中测试此功能,它应该没有任何问题。但对于Chrome,您需要执行以下步骤:

this.MaintainScrollPositionOnPostBack = true;

另外,请参阅步骤: -

右键点击该项目 点击“添加” - &gt; “添加新项目”。在“添加新项”窗口中,选择“浏览器文件”,然后单击“添加”。 应用程序会要求您将此文件放在“App_Browsers”文件夹中,单击“是” 现在添加维护滚动位置的功能,如下所示:

<browsers>
    <browser refID="Safari1Plus">
            <capabilities>
                <capability name="supportsMaintainScrollPositionOnPostback" 
                    value="true" />
            </capabilities>
    </browser>