如何阻止下拉列表在刷新后恢复为默认值



所以我有一个 Web 应用程序,它有一个绑定到网格视图的下拉列表,当我启动我的应用程序并单击 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 是页面打开时我的默认选择,这会更新网格视图。但是我需要一个解决方案来不刷新它并继续刷新我选择的产品(即我目前只有两个选择绑定到我的下拉菜单,即 214 和 225(有人知道吗?谢谢。

在您的 刷新数据方法 ,请在GridView1.DataBind()后插入下面给定的代码,让我知道它是否适合您。

Dropdownlist1.SelectedValue=Convert.ToString(selectedProduct);

在刷新代码中,将 DropdownList 所选值作为参数包括在内,并在 BindDropDownList(( 方法中选择相同的值。

最新更新