I have this webform:
public class web1{
private string target = string.Empty;
protected void Page_Load(object sender, EventsArgs e){
target = "something";
}
protected void btnSubmit_Click(object sender, EventArgs e){
//use target variable here
}
}
When I click the button which triggers btnSubmit_Click()
the target
variable gets reset to string.Empty
because of private string target = string.Empty
.
Currently I'm assigning the new value to a Session
and clearing it after the button click, but was wondering if there was a way of avoiding Session
.
CodePudding user response:
This typcial would work:
private string target = string.Empty;
protected void Page_Load(object sender, EventsArgs e){
if (!IsPostBack)
{
target = "something";
ViewState["target"] = target;
}
else
target = (string)ViewState["target"];
}
Now any button click, event code, or whatever is free to use target.