在C#中,保持表单的值通常可以通过以下几种方式实现:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 初始化表单控件的值
txtName.Text = Session["Name"] != null ? Session["Name"].ToString() : "";
txtEmail.Text = Session["Email"] != null ? Session["Email"].ToString() : "";
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
// 保存表单的值到Session对象
Session["Name"] = txtName.Text;
Session["Email"] = txtEmail.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 初始化表单控件的值
if (hfName.Value != "")
{
txtName.Text = hfName.Value;
}
if (hfEmail.Value != "")
{
txtEmail.Text = hfEmail.Value;
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
// 保存表单的值到隐藏字段
hfName.Value = txtName.Text;
hfEmail.Value = txtEmail.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 初始化表单控件的值
if (ViewState["Name"] != null)
{
txtName.Text = ViewState["Name"].ToString();
}
if (ViewState["Email"] != null)
{
txtEmail.Text = ViewState["Email"].ToString();
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
// 保存表单的值到ViewState对象
ViewState["Name"] = txtName.Text;
ViewState["Email"] = txtEmail.Text;
}
以上是几种常见的保持C#中表单的值的解决方法,可以根据具体的需求选择适合的方式来实现。