我有一个字符串变量,在页面加载之前初始化。字符串变量接受函数中的值,我想在Submit事件中使用此变量。但是当我提交表格时,字符串vale丢失了。字符串变量在页面加载时变为空白。如何保存价值直到提交事件?

这是示例/示例代码:

public partial class Send_Enquiry : System.Web.UI.Page
{

    String tempID2=string.Empty;

    ....

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void submit()
    {
        //When I am calling the "tempID2" value over here, the value come blank.
    }

    protected void getdata()
    {
        tempID2= "data";
    }

}

最佳答案

网络是无状态的。您需要自己maintain state of your web page。有许多方法可以做到这一点,例如ViewStateSession。您可以将字符串值存储在ViewState(在页面级别维护)或Session中,该值在服务器上按每个用户维护。

您可以像这样存储它:

ViewState["yourKey"] = "data";


然后,为了进行检索,您可以执行以下操作:

string str = ViewState["yourKey"] as string;

10-06 04:43
查看更多