简体   繁体   中英

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? 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. In your case only the 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. The timer will trigger every 30 seconds.

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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