ViewState problem...
-
In one form I save a variable in a view state like so: ViewState["Color"] = "red"; I then call Response.Redirect(nextPage.aspx) In the page load function of nextPage.aspx I try and read the variable using this: string sColor=(string) ViewState["Color"]; but there is nothing there! What am I doing wrong? thanks Brian
-
In one form I save a variable in a view state like so: ViewState["Color"] = "red"; I then call Response.Redirect(nextPage.aspx) In the page load function of nextPage.aspx I try and read the variable using this: string sColor=(string) ViewState["Color"]; but there is nothing there! What am I doing wrong? thanks Brian
Hi Brian. ViewState is maintained for one page and its postbacks. When you redirect to another page, a new ViewState is generated. In your context, you may want to set a cookie rather than use ViewState, or use a Session variable:
Session["Color"] = "red";
-
Hi Brian. ViewState is maintained for one page and its postbacks. When you redirect to another page, a new ViewState is generated. In your context, you may want to set a cookie rather than use ViewState, or use a Session variable:
Session["Color"] = "red";
Thanks Mike. I did just that and things are working. Perhaps you can answer this question for me If I defne an aspx page with html controls, can I use code behined file to handle the code for those controls? This would mean that the controls are handled client side... Brian
-
Thanks Mike. I did just that and things are working. Perhaps you can answer this question for me If I defne an aspx page with html controls, can I use code behined file to handle the code for those controls? This would mean that the controls are handled client side... Brian
Hi Brian. The code-behind file (and the .aspx file for that matter) is compiled and executed server-side only. You can declare standard <html> controls with the
runat="server"
attribute to access them server-side. Here's a simple example:<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object o, EventArgs e)
{
myHeading.InnerHtml = "This is the Heading";
myText.Value = "Default Value";
}</script>
<html>
<head>
<title></title>
</head><body>
<form runat="server"><h3 id="myHeading" runat="server" /> Here is a standard HTML text box: <input type="text" id="myText" runat="server" /> </form>
</body>
</html>
If you want to manipulate html controls client-side, you'll need to look at javascript for that.