Saturday, March 27, 2010

.NET 4.0- A new method StringBuilder.Clear() to clear stringbuilder

With Microsoft.NET 4.0 we have a convenient method called clear method which will clear string builder. This method is very use full where we need to use string builder for multiple string manipulation. So we don’t need to create a separate string builder for it. Let’s take a simple example which will print string builder string before,after clear so we can see how it works. Following is simple console application for this.

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
System.Text.StringBuilder myStringBuilder =
new System.Text.StringBuilder(string.Empty);
myStringBuilder.Append("This is my string");
Console.WriteLine("String Builder Output Before Clear:{0}",
myStringBuilder.ToString());
myStringBuilder.Clear();
Console.WriteLine("String Builder Output After Clear:{0}",
myStringBuilder.ToString());
myStringBuilder.Append("This is my another string");
Console.WriteLine("String Builder Output After new string:{0}",
myStringBuilder.ToString());
}
}
}
Here is a output as expected.Strinbuilder

Technorati Tags: ,
Shout it
kick it on DotNetKicks.com
Share:

Dynamically creating Meta Tag from ASP.NET 2.0/1.1

Search Engine optimization is very important to any web site today you can’t get more visitors except search engine optimization.In asp.net site we can also create dynamic meta tags as per our requirement for each page very easily. I will going to show you the two ways of adding meta tags dynamically one for asp.net 2.0 and other for any asp.net version. Lets look first that way and then we will look another way. First you have to add runat=”Server” in your head tag like following.

<head id="Header" runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
Then you need to write following code to create metatags dynamically.

protected  void Page_Load(object sender, EventArgs e)
{

HtmlMeta keywords = new HtmlMeta();
keywords.Name = "keywords";
keywords.Content = "DotNetJaps-An asp.net blog";
Header.Controls.Add(keywords);

} 
Now let look at another way of doing this.Here you can to create a static function which will return the string and that string contain the dynamic metatags.

public static string SiteMetaTags()
{
System.Text.StringBuilder strMetaTag =
new System.Text.StringBuilder(string.Empty);
strMetaTag.AppendFormat(@"<meta content='{0}' name='Keywords'/>",
"DotNetJaps-An asp.net blog");
return strMetaTag.ToString();
}
Then we have to call that function from asp.net page html like following.

<head >
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<%=SiteMetaTags()%>
</head>
That’s it it will be render as met tag in you asp.net page.Hope this will help you..

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

How To Access Control On Master Page From Content Page

It a common requirement that you have change the master page content from content page. For example you have welcome label control that is there in master page and you want to change the welcome message after user logged in from the content page. Same way there might be another requirement like you need to bind a menu from content page as per user logged in having rights. In such kind of case use find control() method to access control on master page from the content page.You can find any control of master page like grid view,repeater and other controls. Let’s take above scenario where you have a label called lblWelcome in your master page and you need to change welcome message from content so first we have to create a label in master page like following.

<asp:Label ID="lblWelcome" runat="server"></asp:Label> 
Then with the help of find control method you have to first find the control with id and then you have to typecast that control as label and then you can do same thing as you can do with label.Here our requirement is changing welcome message so we are changing text of it.Like following.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Label lblWelcome = Page.Master.FindControl("lblWelcome")
as Label;
lblWelcome.Text = "Welome message from content page";
}

}
So that’s it you can easily access master page control from the content page. Hope this will help you

Technorati Tags: ,,
Shout it
kick it on DotNetKicks.com
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews