如何在buttonclick上存儲文本框輸入,以便頁面重新加載可以使用這些值重新創建它的某些內容? 我已經嘗試使用viewState,但它總是隻在page_load上使用斷點時表示null。將文本框中的字符串存儲在buttonclick上以便在頁面重新加載時使用該變量
按鈕點擊:
protected void LoadLifeLineBtn_Click(object sender, EventArgs e)
{
ViewState["lifelineID"] = Convert.ToInt32(TextBox1.Text);
ViewState["phaseid"] = Convert.ToInt32(TextBox2.Text);
this.Page_Load();
}
而且我的繼承人的Page_Load
int lifelineid;
int phaseid;
protected void Page_Load(object sender, EventArgs e)
{
hideAndShow();
if (!IsPostBack)
{
lifelineid = 22222;
phaseid = 1;
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
else if (IsPostBack)
{
if (ViewState["lifelineid"] != null)
{
lifelineid = (int)ViewState["lifelineid"];
}
if (ViewState["phaseid"] != null)
{
phaseid = (int)ViewState["phaseid"];
}
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
}
以前有this.Page_load(null,null),因爲它需要一些東西。 我在LoadLifeLineBtn中使用this.Page_load的原因是因爲我需要爲viewstate設置值,但是比page_load已經完成了它的運行(因爲它在buttonclick之前完成) – svennand
重構'else if(IsPostBack )'阻塞到一個方法中,然後調用該方法而不是調用'this.Page_Load()'。其實你只需要最後兩行。 – mshsayem
是的!工作!謝謝 :) – svennand