Introduction
In this article, I am trying to focus on View States. We use View States to persist state across postbacks. I want to show how we can retrieve an href
value through postback and the language I am using is C#. To be able to set the URL that a user is taken to programmatically, you need to be able to access the anchor tag that is defined in the control’s markup. To do this, you need to give it an id (I set the id
to VerticalLink
) and a runat=server
attribute. Alternatively, you could change the simple <a>
tag to its server-side counterpart: the <asp:HyperLink>
.
In this sample that I am trying to explain, I used a user control (named banner1
) that contains an image which has an anchor tag and its href property is set to www.negardata.com. I used this user control in a webpage like default.aspx but in the code behind of this page in the load_page
event, I set the NavigateUrl
property of the banner1
to another value like http://www.codeproject.com.
The code in the code behind of default.aspx is this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
banner1.NavigateUrl = "http://www.codeproject.com";
}
The first time a user loads default.aspx, the url for the banner1
would be http://www.codeproject.com but imagine the user clicks a button that causes a postback, what will happen?
The Url that I set in page_load
event in the default.aspx will be lost and it will show the url that has been set in the href attribute of the user control page.
Now comes the view state.
To be able to programmatically set the NavigateUrl
property that I add to the banner1
and to ensure this property survives post backs, I need to implement a View State property.
On the code behind of my user control which is banner1
, I have this code that I will explain in more detail later on.
public string NavigateUrl
{
get
{
object _navigateUrl = ViewState["NavigateUrl"];
if (_navigateUrl != null)
{
return (string)_navigateUrl;
}
else
{
return "http://negardata.com";
}
}
set
{
ViewState["NavigateUrl"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
VerticalLink.HRef = NavigateUrl;
}
Now let's see what's happening in the code.
In this code:
set
{
ViewState["NavigateUrl"] = value;
}
When I assign a value to the NavigateUrl
property, its value is stored in the ViewState
collection.When I assign a value to a View State property, it is stored in the page, in the hidden __VIEWSTATE
field. It means it gets sent to the browser when the page loads and it is sent back to the server when the page is posted back again.
When the postback occurs, the code in page_load
in the user control fires again.
protected void Page_Load(object sender, EventArgs e)
{
VerticalLink.HRef = NavigateUrl;
}
The value for NavigateUrl
is returned by the getter of the property:
get
{
object _navigateUrl = ViewState["NavigateUrl"];
if (_navigateUrl != null)
{
return (string)_navigateUrl;
}
else
{
return "http://negardata.com";
}
}
This code first tries to get the value from View State using ViewState["NavigateUrl"]
, If the value that is returned is Nothing or null
, the getter returns the default value for the property: http://www.negardata.com.
However, if the value is not Nothing, it is cast to a string
and eventually returned to the calling code. At the end, the NavigateUrl
returned from the View State property is assigned to the HRef
property of the anchor tag again, which is then used as the URL users are taken to when they click the image.
History
- 18th September, 2011: Initial version
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.