user7422837
user7422837

Reputation:

How to stop the dropdownlist from reverting back to default after refresh

So i have a web app that has a dropdown that is binded to a gridview, all is working perfect when i start my app and click on a different selection on the DDL.. but i have my page to refresh every 10 seconds (as needed) but for some reason once i click on a new DDL selection, after the 10 seconds the page refreshes back to the default selection. I think my issue is that my default value is in the page load so its causing the default to be refreshed each time a refresh happens, is there a way around this?

Code so far...

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

The 214 is my default selection when the page opens, this updates the gridview.. But i need a solution for it to not refresh this and to keep refreshing the product i have chosen (i.e i only have two selections at the moment binded to my dropdown which is 214 and 225) Anybody have any idea? Thanks.

Upvotes: 0

Views: 259

Answers (2)

Pratap P
Pratap P

Reputation: 36

In your Refreshdata method , please insert below given code after GridView1.DataBind() and let me know if it works for you.

Dropdownlist1.SelectedValue=Convert.ToString(selectedProduct);

Upvotes: 1

Amit Shishodia
Amit Shishodia

Reputation: 97

In your refresh code, include DropdownList selected value as parameter and select the same value in BindDropDownList() method.

Upvotes: 0

Related Questions