简体   繁体   English

回发后如何保持下拉选择相同

[英]How to keep dropdown selection the same after postback

I have a dropdown list that is populated from sql table... I have the page to refresh after 30 seconds, the problem is after the 30 seconds the page refreshes back to the default page instead of staying on the selected page.. how can i change this to stay on the selected page? 我有一个从sql表填充的下拉列表...我要在30秒后刷新页面,问题是30秒后页面刷新回到默认页面,而不是停留在所选页面上。我将其更改为保留在所选页面上吗? I also have put a default page number into my page load, how can i change this so i do not have to put a default selection in ... hope this makes sense. 我也已经在页面加载中添加了默认页码,我该如何更改它,这样我就不必在...中添加默认选择了……希望这有意义。

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Refreshdata(214, DateTime.Today, DateTime.Today.AddDays(1).AddMinutes(-1));
                BindDropDownList();


            }
        }

214 is one of the stored procedure selections, but i would like this to be a default selection .. 

 public void Refreshdata(int selectedProduct, DateTime shiftStart, DateTime shiftEnd)
        {
            BizManager biz = new BizManager();

            GridView1.DataSource = biz.GetPacktstatisticsForShift(
                shiftStart
                , shiftEnd
                , selectedProduct).DefaultView;
            GridView1.DataBind();
        }

  public void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime shiftStart = DateTime.Today;
            DateTime shiftEnd = DateTime.Today.AddDays(1).AddMinutes(-1);
            int productId;
            if (int.TryParse(Dropdownlist1.SelectedValue, out productId))
                Refreshdata(productId, shiftStart, shiftEnd);
        }

You don't have to refresh the whole page. 您不必刷新整个页面。 You can use updatepanel to update partial part of your page. 您可以使用updatepanel更新页面的部分内容。 In your case only the gridview. 在你的情况下只有gridview。

<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
      <asp:GridView ID="GridView1" runat="server">
      </asp:GridView>                                  
   </ContentTemplate>
 </asp:UpdatePanel>

You call update on the updatepanel with a javascript timer. 您可以使用JavaScript计时器在updatepanel上调用update。 The timer will trigger every 30 seconds. 计时器每30秒触发一次。

<script type="text/javascript">
setInterval(function () { RefreshItems();}, 30000);

function RefreshItems()
{
  var UpdatePanel1 = '<%=UpdatePanel1.ClientID%>';
  if (UpdatePanel1 != null) 
  {
     __doPostBack(UpdatePanel1, '');
  }
}       
</script>

And as last add a else to your page load 最后添加另一个到您的页面加载

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    Refreshdata(214, DateTime.Today, DateTime.Today.AddDays(1).AddMinutes(-1));
    BindDropDownList();
   }else
   {
      if(int.TryParse(DropDownList1.SelectedValue, out int selectedProduct)) 
      {
         Refreshdata(selectedProduct, DateTime.Today, DateTime.Today.AddDays(1).AddMinutes(-1));   
      }
   }
}

This way you can refresh the gridview without reload the page and loosing your initial selection on the dropdownlist. 这样,您可以刷新gridview而不重新加载页面,也不必在下拉列表中失去初始选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM