»
S
I
D
E
B
A
R
«
+
More
Recent News and Links
  • No bookmarks avaliable.
Classic ASP header redirect (302 Object Moved)
Aug 19th, 2008 by webpaws

ASP makes it easy to redirect a user to another page. This is a
useful trick for redirecting users after form submissions, protecting
content on your site by redirecting users to a login page, and pointing
an old page address to a new one.

Classic ASP header redirect (302 Object Moved)

At the top of your ASP document (before any HTML code), use the following code:

<% Response.Redirect("default.asp") %>

This example shows how to redirect a user to a page on your own
site, in the same directory as your redirect page. If you wanted to
redirect a user to a completely different website, simply use the full
URL:

<% Response.Redirect("http://www.yourdomain.com") %>

The Response.Redirect declarative gives an “Object Moved,” or Status
Code 302 header response. If you want to permanently point an old page
address to a new one using ASP, see below.

Serving a permanent redirect (301) header status code

There may be an instance where you want to point an old page to a
new location. This can be accomplished in ASP by including specific
header information in your code snippet. The following example serves a
301 Redirect (“301 Moved Permanently”) to anyone who accesses the page.

<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.thenewdomain.com”
%>

VN:F [1.8.4_1055]
Rating: 8.8/10 (5 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)
 
Take a piece of the Web from WebPaws.com: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • MisterWong
  • Y!GG
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • De.lirio.us
  • eKudos
  • email
  • Facebook
  • Fleck
  • Google Bookmarks
  • Hype
  • LinkedIn
  • MySpace
  • Print
  • Technorati
  • YahooMyWeb
PostBack in ASP.net – passing variables on postback
Aug 19th, 2008 by webpaws

Be aware that if you set a variable before postback and then check it after postback it will be reset, use Session or ViewState to keep variable values regardless of postback state.

ViewState is a collection bag which holds key value pairs of changed control attributes. You can also utilize it to store your own values which will persist though a Page’s postbacks. ViewState is a member of Page, which means it is globably accessible throughout your Page. The ViewState is loaded early on in the Page creation (life cycle), and towards the end it is encrypted and outputed into the HTML. Therefore, when you add some data to the ViewState on the first page load, that data is being stored (encrypted) in the __ViewState hidden field you’ll find in the HTML source.  When the page is loaded again (after you click the button), that value is decrypted and loaded into the ViewState bag, which means you’ll be able to access it.

Remember to always check if the value you are getting (either from ViewState or Session) is null or not. If not, you’re users will get the infamous “Object reference not sent to an instance of an object.” exception.

ViewState is the mechanism that allows state values to be preserved across page postbacks.

Because of the stateless nature of web pages, regular page member variables will not maintain their values across postbacks.  When we need a page variable to maintain its value across page post backs, we can use ViewState to store that value.  Values stored in ViewState will be serialized and sent to the client browser as the value of a hidden form input.  When you view the page source (in your browser) of a page the uses ViewState, you may see this hidden viewstate input which will look something like this:

<input type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wQPDwAKMTn1ODM5Rj…….” />

This single hidden field contains all the viewstate values for all the page controls. This is an important aspect of viewstate that you need to consider.

Example

One simple way to store small values in viewstate is to use a property instead of a member variable.  This property can use viewstate to store its value rather than a member variable that would lose the value over a postback. For example, storing an Integer in viewstate can be accomplished like this:

VB

Public Property SomeInteger() As Integer

Get

Dim o As Object = ViewState(“SomeInteger”)

If Not o Is Nothing Then Return DirectCast(o, Integer)

Return 0 ‘a default

End Get

Set(ByVal value As Integer)

ViewState(“SomeInteger”) = value

End Set

End Property

VN:F [1.8.4_1055]
Rating: 6.8/10 (9 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 1 vote)
 
Take a piece of the Web from WebPaws.com: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • MisterWong
  • Y!GG
  • Webnews
  • Digg
  • del.icio.us
  • StumbleUpon
  • De.lirio.us
  • eKudos
  • email
  • Facebook
  • Fleck
  • Google Bookmarks
  • Hype
  • LinkedIn
  • MySpace
  • Print
  • Technorati
  • YahooMyWeb
»  Substance: WordPress   »  Style: Ahren Ahimsa
© Copyright by WebPaws.com 2009. All Rights Reserved.

WebPaws.com is Digg proof thanks to caching by WP Super Cache