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

时间:2018-05-30 07:38:46

标签: c# asp.net dropdown

我有一个从sql表填充的下拉列表...我有30秒后刷新的页面,问题是在30秒之后页面刷新回默认页面而不是停留在所选页面上。如何更改此选项以保留所选页面?我也把一个默认的页码放到我的页面加载中,我怎么能改变这个,所以我不必把默认选择... ...希望这是有道理的。

 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);
        }

1 个答案:

答案 0 :(得分:1)

您不必刷新整个页面。您可以使用updatepanel更新页面的部分内容。在你的情况下只有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>

您使用javascript计时器在updatepanel上调用update。计时器每30秒触发一次。

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

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

最后在页面加载中添加了一个

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));   
      }
   }
}

这样您就可以刷新gridview而无需重新加载页面并在下拉列表中丢失初始选择。

相关问题