【发布时间】:2019-01-15 14:04:25
【问题描述】:
我有一个下拉列表,其中包含在后面的代码中创建的列表项。 ddl填充()。非常简单并且可以完成工作。它填充了当前月份和未来几个月。我使用此下拉菜单作为填充 Gridview 的选择。当此下拉索引更改时,它会更改隐藏字段值和相应的查询以填充网格视图。所有这些都正常工作。在gridview 中,我有另一个下拉列表和一个按钮。两者都将选定的行提交到数据库。这也很好用。问题是,每次这些行之一提交到数据库时,都会导致回发并重置整个页面。它将下拉列表重置为第一个列表项。 IE。例如,我将下拉列表更改为索引 4。在这种情况下,这将是四月。如果我向数据库提交一行,页面会刷新并返回到索引 0 .. 在这种情况下是一月。如何防止它以这种方式重置并保持我提交行时的位置?
我尝试了几种不同的选择。我已经尝试过会话状态。隐藏字段值更改。似乎没有任何效果。它要么不执行回发,因此从不提交到数据库,要么回发,正确提交,然后重置整个页面。包括将 hiddenfield 值重置回 0
/* This is up in Page load. */
if (Session["pageStatus"] != null)
{
if (Session["pageStatus"].ToString() == "Loaded")
{
hf2.Value = "Loaded";
}
}
else
{
hf2.Value = "New";
}
if (Session["selectedMonth"] != null)
{
hf1.Value = Session["selectedMonth"].ToString();
}
if (ViewState["button_was_clicked"] != null)
{
ddlFill();
StyleDDL();
}
lblTestlabel.Text = hf2.Value;
AddAttributes();
ShowMonth();
if (!Page.IsPostBack)
{
btnReviewCurrentMonth_OnClick(sender, e);
ddlFill();
StyleDDL();
}
private void ddlFill()
{
string a, b, c, d, e, f;
a = "0";
b = "1";
c = "2";
d = "3";
e = "4";
f = "5";
DropDownList1.Items.Insert(0, new ListItem(ReturnMonth(a))); // A blank object call and the ReturnMonth Method fill the list items.
DropDownList1.Items.Insert(1, new ListItem(ReturnMonth(b)));
DropDownList1.Items.Insert(2, new ListItem(ReturnMonth(c)));
DropDownList1.Items.Insert(3, new ListItem(ReturnMonth(d)));
DropDownList1.Items.Insert(4, new ListItem(ReturnMonth(e)));
DropDownList1.Items.Insert(5, new ListItem(ReturnMonth(f)));
/* These were for various testing options to get it to maintain the
state */
hf2.Value = "Loaded";
Session["pageStatus"] = "Loaded";
DropDownList1.SelectedIndex = Int32.Parse(hf1.Value);
}
我的目标是在提交到数据库后维护页面的状态。
【问题讨论】:
-
我认为这个问题是因为
ddlFill方法在回发时被调用。默认情况下,ASP.NET 控件在回发时保留其状态。