Wednesday, May 7, 2008

Changing the Page Title from server side in ASP.NET

Some times we need to change the page title of the page from the server side code. For this, there are two simple methods to set the the page title.

We can directly assign the page title form server side as Page.Title="MyPageTitle". But one main thing is that we must set the <head> tag as runat="server".

<head runat="server">

And remove the <title></title> tag from the <head> tag.

The second method is that,
put the runat="server" and id in the Title tag.

<title id="MyPageTitle" runat="server"> My Page Title </title>

After making the title tag as server control we can easily access the title from server by Id specified in the title tag. If you are using files from VS2003, then we need to declare title as protected.

protected System.Web.UI.HtmlControls.HtmlGenericControl MyPageTitle;


(Later versions of visual studio does not need this over head. The VS automatically add this declaration in designer file.)

The HtmlGenericControl type objects have a property named "InnerText" and we can set the new page title there.

MyPageTitle.InnerText="MyNewPageTitle";

Wednesday, January 23, 2008

Modifying Query String in .net



Sometimes we need to modify the query string values of URL.

For Example :

Consider our Current URL is : http://www.mysite.com/MyHomePage.aspx?Value=x
and, we want to change it as : http://www.mysite.com/MyHomePage.aspx?Value=y

We can access all the Query Strings from Request.QueryString["query_string_name"], but we can't make any modification to QueryString["query_string_name"] because it is read only. What we can do is, copy that Query String collection to an instance of System.Collections.Specialized.NameValueCollection and replace the current query string from URL.

The following code will change the query string value of "Value" to y.
System.Collections.Specialized.NameValueCollection queryStrings = System.Web.HttpUtility.ParseQueryString( Request.QueryString.ToString());
queryStrings.Set("Value", "y");
lnkNew.NavigateUrl = Request.Url.AbsolutePath + "?" + queryStrings.ToString();


This code will set the navigation URL of lnkNew(hyper link) as "http://www.mysite.com/MyHomePage.aspx?Value=y".

We need to specify the "?" between path and query string parameters.

We can add add query string value using NameValueCollection.Add("","") method. Eg: In above example, we are going to add new query string value using following code:
System.Collections.Specialized.NameValueCollection queryStrings = System.Web.HttpUtility.ParseQueryString( Request.QueryString.ToString());
queryStrings.Set("Value", "y");
queryStrings.Add("Value2", "MyName");
lnkNew.NavigateUrl = Request.Url.AbsolutePath + "?" + queryStrings.ToString();


Above code segment will set the navigation URL to.
http://www.mysite.com/MyHomePage.aspx?Value=y&Value2=MyName

"&" will appear default between two parameters.

We can also redirect to another page with modified query string parameters.

System.Collections.Specialized.NameValueCollection queryStrings = System.Web.HttpUtility.ParseQueryString( Request.QueryString.ToString());
queryStrings.Set("Value", "y");
queryStrings.Add("Value2", "MyName");
string page="Contact.aspx"
Response.Redirect(page + "?" + queryStrings.ToString());


This code will redirect our page to
http://www.mysite.com/Contact.aspx?Value=y&Value2=MyName


We can also remove the query string value.
For example
queryStrings.Remove("Value");

The "Value" parameters from the query string.