C# 将文本框中的字符串存储在按钮上单击可在页面重新加载时使用该变量
如何在buttonclick上存储文本框输入,以便页面重新加载可以使用这些值重新创建其部分内容? 我尝试过使用viewState,但在使用断点时,页面加载时总是显示null 按钮点击:C# 将文本框中的字符串存储在按钮上单击可在页面重新加载时使用该变量,c#,asp.net,viewstate,C#,Asp.net,Viewstate,如何在buttonclick上存储文本框输入,以便页面重新加载可以使用这些值重新创建其部分内容? 我尝试过使用viewState,但在使用断点时,页面加载时总是显示null 按钮点击: protected void LoadLifeLineBtn_Click(object sender, EventArgs e) { ViewState["lifelineID"] = Convert.ToInt32(TextBox1.Text); ViewState["phaseid"] = C
protected void LoadLifeLineBtn_Click(object sender, EventArgs e)
{
ViewState["lifelineID"] = Convert.ToInt32(TextBox1.Text);
ViewState["phaseid"] = Convert.ToInt32(TextBox2.Text);
this.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";
}
}
假设您的按钮已连接到此事件
<asp:Button ID="LoadLifeLineBtn" runat="server" OnClick="LoadLifeLineBtn_Click"></asp:Button>
现在重新组织Page\u Load
事件,以便在页面最初加载时进行处理
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";
}
}
原因是当
LoadLifeLineBtn\u Click
触发时,您可以在该点执行所需操作,而不直接调用Page\u Load
事件。假设您的按钮已连接到此事件
<asp:Button ID="LoadLifeLineBtn" runat="server" OnClick="LoadLifeLineBtn_Click"></asp:Button>
现在重新组织Page\u Load
事件,以便在页面最初加载时进行处理
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";
}
}
原因是当
LoadLifeLineBtn\u Click
启动时,您可以在该点执行所需操作,而不必直接调用Page\u Load
事件。我添加了第二个答案,以帮助解释当您单击LoadLifeLineBtn
按钮时代码中发生的一些事情
当您单击LoadLifeLineBtn
时,它将触发LoadLifeLineBtn\u click
事件,该事件将创建回发,并且输入TextBox1
和TextBox2
的值将已经是ViewState
的一部分
protected void Page_Load(object sender, EventArgs e)
{
int lifelineid;
int phaseid;
hideAndShow();
if (!IsPostBack)
{
lifelineid = 22222;
phaseid = 1;
}
else
{
lifelineID = Convert.ToInt32(TextBox1.Text);
phaseid = Convert.ToInt32(TextBox2.Text);
}
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
在您的代码中,您可以将其更改为该状态,并且它应该按照您的意愿继续运行,而无需手动设置视图状态
protected void Page_Load(object sender, EventArgs e)
{
int lifelineid;
int phaseid;
hideAndShow();
if (!IsPostBack)
{
lifelineid = 22222;
phaseid = 1;
}
else
{
lifelineID = Convert.ToInt32(TextBox1.Text);
phaseid = Convert.ToInt32(TextBox2.Text);
}
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
我添加了第二个答案,以帮助解释当您单击
LoadLifeLineBtn
按钮时代码中发生的一些事情
当您单击LoadLifeLineBtn
时,它将触发LoadLifeLineBtn\u click
事件,该事件将创建回发,并且输入TextBox1
和TextBox2
的值将已经是ViewState
的一部分
protected void Page_Load(object sender, EventArgs e)
{
int lifelineid;
int phaseid;
hideAndShow();
if (!IsPostBack)
{
lifelineid = 22222;
phaseid = 1;
}
else
{
lifelineID = Convert.ToInt32(TextBox1.Text);
phaseid = Convert.ToInt32(TextBox2.Text);
}
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
在您的代码中,您可以将其更改为该状态,并且它应该按照您的意愿继续运行,而无需手动设置视图状态
protected void Page_Load(object sender, EventArgs e)
{
int lifelineid;
int phaseid;
hideAndShow();
if (!IsPostBack)
{
lifelineid = 22222;
phaseid = 1;
}
else
{
lifelineID = Convert.ToInt32(TextBox1.Text);
phaseid = Convert.ToInt32(TextBox2.Text);
}
FillFaseTable(PhaseMainTable, phaseid, lifelineid);
PhasePanel.CssClass = "panel col-lg-2 col-md-2 col-xs-2 Phase1BackgroundColor";
}
以前加载了这个.Page_(null,null),因为它需要一些东西。我在LoadLifeLineBtn中使用此.Page_load的原因是我需要设置viewstate的值,但此时Page_load已经完成了它的运行(因为它在单击按钮之前完成了),将
中的代码重构为一个方法,如果(IsPostBack)
块,则调用该方法,而不是调用this.Page_load()
。实际上,你只需要最后两行。是的!成功了!谢谢:)以前有这个。页面加载(空,空),因为它需要一些东西。我在LoadLifeLineBtn中使用此.Page_load的原因是我需要设置viewstate的值,但此时Page_load已经完成了它的运行(因为它在单击按钮之前完成了),将中的代码重构为一个方法,如果(IsPostBack)
块,则调用该方法,而不是调用this.Page_load()
。实际上,你只需要最后两行。是的!成功了!谢谢:)