簡體   English   中英

刷新后如何阻止下拉列表恢復為默認值

[英]How to stop the dropdownlist from reverting back to default after refresh

因此,我有一個Web應用程序,該應用程序的下拉列表已綁定到gridview,當我啟動我的應用程序並單擊DDL上的其他選擇時,一切運行正常。.但是我每10秒刷新一次頁面(根據需要) ),但由於某種原因,一旦我單擊新的DDL選擇,則10秒鍾后頁面將刷新回到默認選擇。 我認為我的問題是我的默認值在頁面加載中,因此每次刷新時都會導致默認值被刷新,是否可以解決此問題?

到目前為止的代碼...

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



            }
        }

        private void BindDropDownList()
        {
            BizManager mgr = new BizManager();

            DataView dv = mgr.GetItemSeriesMaster().DefaultView; //how to filter data
            dv.RowFilter = ProductQueryFilter;
            Dropdownlist1.DataSource = dv;
            Dropdownlist1.DataTextField = "Description"; // the items to be displayed in the list items
            Dropdownlist1.DataValueField = "Id"; // the id of the items displayed
            Dropdownlist1.DataBind();

        }

        private string ProductQueryFilter
        {
            get { return ConfigurationManager.AppSettings["ProductQueryFilter"]; } //returns itemseriesmaster 214 or 225
        }


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

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //if (e.Row.Cells[2].Text.Trim() == "0")
                //    e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
                //if (e.Row.Cells[4].Text.Trim() == "0")
                //    e.Row.Cells[4].ForeColor = System.Drawing.Color.Red; ~~~these find a specific number to change i.e change all 0 to green.
                //if (e.Row.Cells[6].Text.Trim() == "0")
                //    e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[1].ForeColor = Color.Red;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[3].ForeColor = Color.Red;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[5].ForeColor = Color.Red;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[7].ForeColor = Color.Red;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[2].ForeColor = Color.Black;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[4].ForeColor = Color.Black;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[6].ForeColor = Color.Black;
                    }

                    if (e.Row.Cells[i].Text.ToLower().IndexOf("0") > -1)
                    {
                        e.Row.Cells[8].ForeColor = Color.Black;
                    }
                }
            }
        }

214是我在打開頁面時的默認選擇,這將更新gridview。。但是我需要一個解決方案,以便不刷新它並保持刷新我選擇的產品(即,目前綁定到我的產品只有兩個選擇)下拉菜單分別是214和225)有人有什么主意嗎? 謝謝。

在您的Refreshdata方法中,請在GridView1.DataBind()之后插入以下給定的代碼,讓我知道它是否對您有用。

Dropdownlist1.SelectedValue=Convert.ToString(selectedProduct);

在刷新代碼中,將DropdownList選定的值包括在內作為參數,並在BindDropDownList()方法中選擇相同的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM