12 Dec 2011

Adding attributes on body tag from server-side

today, I ran into an issue that I had to make some interface changes based on different devices like iPhone, iPad, Android etc via CSS.

I thought to apply a css class on the body tag, and then handle the inteface changes in the stylesheet.

since body tag is in the masterpage and can not be accessed directly, therefore I had to do a small trick ;) see the code below.

Markup code

<!-- Assign an id to body tag and add runat="server". -->
<body id="page_body" runat="server">

Server side

/// <summary>
/// Page Load event of current page
/// </summary>
/// <param name="sender" />
/// <param name="e" />
protected void Page_Load(object sender, EventArgs e)
{
    // initialize browser detector class
    BrowserDetector browserDetector = new BrowserDetector();

    // get the operating system name, being used by client
    var os = browserDetector.OS.ToLowerInvariant();

    // get the body tag from masterpage
    HtmlGenericControl body = (HtmlGenericControl)this.Page.Master.FindControl("page_body");

    // if body tag found successfully
    // then apply class attribute on it, so simple :)
    if (body != null)
    {
        body.Attributes.Add("class", "os-" + os);
    }
}
16 Jan 2011

ASP.NET MVC 3 released

For so long, I was waiting for the final release of ASP.NET MVC 3, and now the wait ends and I should properly dive into MVC. ;)

Download and Install ASP.NET MVC 3

Following is the list of some key features and improvments in ASP.NET MVC 3:

There are a lot more great improvements in ASP.NET MVC 3 listed below:

  • Improved New Project dialog that makes it easy to start new ASP.NET MVC 3 projects from templates.
  • Improved Add->View Scaffolding support that enables the generation of even cleaner view templates.
  • New ViewBag property that uses .NET 4’s dynamic support to make it easy to pass late-bound data from Controllers to Views.
  • Global Filters support that allows specifying cross-cutting filter attributes (like [HandleError]) across all Controllers within an app.
  • New [AllowHtml] attribute that allows for more granular request validation when binding form posted data to models.
  • Sessionless controller support that allows fine grained control over whether SessionState is enabled on a Controller.
  • New ActionResult types like HttpNotFoundResult and RedirectPermanent for common HTTP scenarios.
  • New Html.Raw() helper to indicate that output should not be HTML encoded.
  • New Crypto helpers for salting and hashing passwords.
  • And much, much more…

More about ASP.NET MVC 3

read more details on Scott Gu’s blog

6 Jan 2011

Join twitter for 7 reasons

I feel like a twitter representative :D (unfortunately I am not), I keep asking people to join twitter, and in reply they ask me ‘why?’, I tell them benefits of twitter, and then they join twitter. So finally I thought to write a blog post telling the 7 reasons to join twitter. :)

A great source of traffic

Increase web traffic

Image credit

Your twitter followers know that you run a blog, so whenever you write a new blog post, just post a link on twitter with the blog post title. And I am damn sure you’ll get hundreds or may be thousands of clicks only from twitter.

Real-time

Twitter is all real-time, You’ll get real-time results about anything you search on twitter. According to Wikipedia: Twitter has 190 million users, generating 65 million tweets a day and handling over 800,000 search queries per day. Which is around 750 tweets per second. so you’ll surely get the best out of it.

Free of cost for your business

Twitter only requires a very small investment for marketing. And the investment is not money, its your time. :) Stay active on twitter, spend some time in building up your own community with twitter. set up your blog with auto-posting services, and keep sharing some great stuff with your twitter followers.

It’s all social

Twitter is a way of spreading your words worldwide. All your tweets on twitter, are not only visible to your followers, but can be viewed by more than 175 million users. And people will share them on facebook and other social networks. They will also link them to their blog.

Helpful for Building Valuable Relationships

Laura Fitton, the author of Twitter for Dummies, chalks up success on Twitter to four basic concepts: listen, learn, care, and serve. Basically, if you listen to the community, you will learn, and if you show that you care, you are more likely to get more out of your efforts. Serving means providing something of use to the community. If you what you’re not doing that, you may be setting yourself up to fail, as Fitton talked about in this interview with WebProNews.

The power of the Re-Tweet equals Viral Gold

It’s been said that blogs can take an idea and spread it from New York to Tokyo in minutes. If that’s true, then it’s also true Twitter can do it in seconds. It takes very little time to blog something. It takes less time to email something. It takes even less time to tweet it. Send something interesting out into Twitter and folks will pick it up and repost to their own list of followers in seconds. Suddenly your own network grows infinitely. read more »

Know what you want to

Twitter is not as formal as LinkedIn and making friends on Twitter is even easier than Facebook. Start following people of your domain, and stay up to date about whats happening in the world right now. Twitter is just an open place for sharing. People share here everything including Good morning, Good night, having a cup of coffee, taking dinner, meeting with a client, signing a project, going on a drive, singing, listening to music, studying, or even thinking.

Twitter loves your tweets :)

25 Dec 2010

Working with Ajax and HTML/Text files

In my previous post, I told about Working with AJAX and ASP.Net Generic Handlers and today I am gonna tell how load the content of any html/text file into your web page. This is damn easy, and we can re-use the code of ajax.js from my last post.

Create a html file named ‘htmldatafile.htm’ and a text file named ‘textdatafile.txt’ in your project, and put some content in them. We will load the contents of these files using ajax in our webpage.

Now create a html file with any name and put following html in it.

HTML

Some Other text<br />Some Other text<br />Some Other text<br />

<div id="textResultDiv"></div>  

<div id="htmlResultDiv"></div>  

Some Other text<br />Some Other text<br />Some Other text<br />

<script type="text/javascript" src="ajax.js"></script> <!-- get ajax.js code from http://bit.ly/f5WDt5 -->

Now put the following javascript in your same html page. and you are done :)

The following javascript makes two ajax calls for html and text file respectively and loads their contents into their respective divs.

I have also put comments with each line of code for better understanding :)

Javascript

// Make an ajax call to htmldatafile.htm
// You will receive the data from html file in the 
// data parameter of callback function
ajax.get("htmldatafile.htm", function (data) {

    // get the div element
    var resultDiv = document.getElementById('htmlResultDiv');

    // put the data in the div
    resultDiv.innerHTML = data;

});


// Make an ajax call to textdatafile.txt
// You will receive the data from text file in the 
// data parameter of callback function
ajax.get("textdatafile.txt", function (data) {

    // get the div element
    var resultDiv = document.getElementById('textResultDiv');

    // put the data in the div
    resultDiv.innerHTML = data;

});

Source Code

Download the source code »

21 Dec 2010

Yahooooo! I successfully cleared MCTS – 70-515

Yahooooo! today, I am so much happy that I have successfully cleared the MCTS Exam TS 70-515: Web Applications Development with Microsoft .NET Framework 4 and have been awarded with the title of Microsoft Certified Technology Specialist.

Since this was my first certification, therefore now I am Microsoft Certified Professional as well. :)

Zain Shaikh's Posterous

A 'Workaholic', MCP & MCTS, Web Designer & Developer Working with ASP.Net, Silverlight and WCF, and a Poetry lover as well :)
My Google Profile


I am Microsoft Certified Professional :)
Follow me on twitter

 Subscribe in a reader

If you would like to recieve updates via email, simply enter your email address below & click subscribe.