Showing posts with label ASP.NET. Show all posts
Showing posts with label ASP.NET. Show all posts
Saturday, April 5, 2014

Overriding/set default button in master page in

Some time ago I have written a blog post about Default focus and default button in ASP.NET. In that post I have explained how we can set default button for form. Recently I got email related to that post asking that how we can set or override default button on a page with master page. 

In this page we will learn how we can override default button in master page. So what we are waiting for? Let’s start by creating a an empty application via File –> New Project –> ASP.NET Empty web application.


Once You are done with creating project it’s time to add a master page in solution via right click-> Add new Item.


Following is HTML code ASP.NET master page.

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs"
     Inherits="MasterPageDemo.Site1" %>
<!DOCTYPE html>
<html xmlns="">
<head runat="server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

Now it’s time to add new content page with master page and selecting above master page.


Following is a code for content page.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
     CodeBehind="Default.aspx.cs" Inherits="MasterPageDemo.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:TextBox runat="server" ID="Message"></asp:TextBox>
    <ASP:Button  runat="server" ID="DefaultButton"
        OnClick="DefaultButton_Click" Text="Default"/>
    <asp:Button runat="server" ID="AnotherButton"
        OnClick="AnotherButton_Click" Text="Another"/>
Here I have added two buttons “Default Button” and “Another button” and a textbox. Now I want to make this “Default Button” default dynamically. Below code I have written for both button’s click event. It will only print in textbox which button is clicked.

protected void DefaultButton_Click(object sender, EventArgs e)
    Message.Text = "Default Button Clicked";

protected void AnotherButton_Click(object sender, EventArgs e)
    Message.Text = "Another button clicked";

Now I want to make default button as default button of form so whenever I press enter it will fire click event of default button. So to dynamically set / override default button on master page form I have to write following code on content page page_load event.

protected void Page_Load(object sender, EventArgs e)
    Form.DefaultButton = DefaultButton.UniqueID;

Now when you run and press enter in browser output will be as expected as follows.


That’s it. Hope you like it. Stay tuned for more..
Sunday, December 8, 2013

ASP.NET and Web Tools 2013.1 Tools for Visual Studio 2012

Recently Microsoft has released ASP.NET and Web Tools 2013.1 for visual studio 2012. There are tons of new feature and improvements are there. So Now it’s possible to have all the latest feature on ASP.NET stack on Visual Studio 2012 like ASP.NET MVC 5, ASP.NET Web API 2, Entity Framework 6.0 etc.
You can download this update from the following link.


Once you install this tools, ASP.NET MVC 5 templates are also installed.


So now you can use all the ASP.NET MVC 5 feature like One ASP.NET, ASP.NET Identity,Bootstrap MVC template, Authentication Filters,Filter overrides etc.


ASP.NET Web API2 is also available here.


So all ASP.NET Web API2 features like attribute routing, Cross Origin Resource Sharing, OWIN (Open Web Interface for .NET), IHttpActionResult, Web API Odata all are available Visual Studio 2012.

Twitter BootStrap Template:

Now ASP.NET MVC 5 application comes with Twitter Bootstrap, So all goodies of Twitter Bootstrap will be there with your ASP.NET MVC application.

Entity Framework 6.0:

It’s also comes with Entity framework 6.0. Where you have some new great features like Async Query and Save,Connection Resiliency, Code-Based Configuration, Dependency Resolution, Interception/SQL Logging, Improved transaction support etc will be available in Visual Studio 2012 itself.

Also there is a new version of Nuget is also available with this release. So now if one developer has Visual Studio 2012 and another one is having Visual Studio 2013 then also they can work on same project.

This post is a just an overview of all the new features that are available with this release. I will write in detail about all this new features. Stay tuned for more..
Friday, August 2, 2013

How to add a control to user control from a web page in

This post in a reply to a question on Where the person who asked this question want to add control from the page dynamically. I decided to write a blog post here.

So let’s first create a user control for this.


After creating a user control It’s try to put the user control in the aspx page like following.

Saturday, July 13, 2013

Compile time view checking for ASP.NET MVC.

While developing an ASP.NET MVC application, you need to create lots of views. At that time it does not compile the view. So if you have some typo in your view like namespace or any where you will get to know when you load that view in browser. At that time ASP.NET Complier will compile the complete view. 

We all are humans and we tends to make spelling mistakes and sometimes it a productivity loss when you create a very big view or complicated view. So I was searching on internet that whether there is a tool available to compile view also. After digging into it. I have found that there is a setting where we can tell whether its compile view or not. So once you enable that settings It will also compile views.

Programmatically Hiding ASP.NET Grid View Column.

Recently I have been to and found that lots of people are finding solution for hiding the Grid View column so I though it will be a good Idea to write a blog post. In this post I will explain how we can hide GridView Column programmatically. So let’s take same example that I have taken in previous post.

Hide ASP.NET GridView Column:

So let’s take  a simple example. I am going to bind a grid with list of Employee class and then on a button click I am going to hide the first column of grid. Following is a HTML code for that.
    <asp:Button runat="server" Text="Hide Column" ID="btnHideColumn" OnClick="btnHideColumn_Click"/>
        <asp:GridView runat="server" ID="employeeGrid" AutoGenerateColumns="False">
                <asp:BoundField HeaderText="Employee Id" DataField="EmployeeId"/>
                <asp:BoundField HeaderText="First Name" DataField="FirstName"/>
                <asp:BoundField HeaderText="Last Name" DataField="LastName"/>

Saturday, July 6, 2013

Dynamically setting GridView Column width in ASP.Net

I was looking into the and I have found lots of people was searching about how to set GridView Column Width dynamically. So I thought it will be a good idea to write a blog post about it.

Example(Setting Width Dynamically in ASP.Net):

So let’s take a an example for that. For that I have created a small model Employee class which will have three properties.
public class Employee
    public int  EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

Sunday, June 9, 2013

Creating PDF with ASP.Net MVC and RazorPDF

Update: I have written a new blog post about better approach to create a PDF with mvc- You can find that following location.- A Better Solution to create PDF with Rotativa and ASP.NET MVC

In this post we are going to learn how we can easily create PDF from ASP.Net application with the help of Razor PDF NuGet package.

About Razor PDF:

This NuGet package is created by Al Nyveldt It internally uses ITextSharp an open source PDF convertor library. RazorPDF uses Razor View engine to create iTextXML which is in tern used to produce PDF file. You can get more information about that at below link.

Example(Creating PDF with ASP.Net MVC):

So what we are we waiting for ? Let’s create a simple example. To create example first thing we need to a create and ASP.Net MVC application.

Horizontal and vertical scrollbar in HTML5 and CSS3

In this post we are going to learn how we can put horizontal and vertical scrollbar in HTML5 and css3. In earlier version of HTML and CSS if we have to put only horizontal or vertical scrollbar that was not possible. Because there was no such properties there in CSS to describe whether we have horizontal scrollbar or vertical scrollbar.

Scroll bar in earlier version of HTML and CSS3:

In earlier version of HTML and CSS we have to use overflow property to display scrollbar for a particular div tag and If we add scrollbar it will add both horizontal and vertical scrollbar even if there is no need for that.Following is a code for that.
Saturday, June 8, 2013

Rounded corner div with HTML5 and CSS3

I have been recently playing with HTML5 and CSS3 and it is really fun. I am learning lots of new features of HTML5. Rounded corner is one of them. I thought it will be a good idea to share that with my readers. So In this post we are going to learn how we can create rounded corner div with HTML5.

In earlier version of HTML and CSS, If we need rounded corner div then we all needs to use image and without image it was not really possible. But now with the HTML5 and CSS3, It is very easy to create round corner div.

How to apply round corner to div?

There is a new CSS3 declaration for that. Border-Radius, With this you apply round corners to boxes and div without using images for rounded corners.


Let’s create an example for rounded corner div. I have created an sample page with Visual Studio 2012.

Rounded Corner div html5 and css3
Sunday, June 2, 2013

How to call ASP.Net page method with jQuery

In this blog post we are going to learn about how we can call page methods from jQuery.

What is ASP.Net Page Method?

Page methods are available from ASP.Net 2.0. It’s an alternative to web services in ASP.Net application. This methods are exposed at client side and you can call that page methods directly from JavaScript.It is an easy way to communicate asynchronously with the server using Ajax.

Creating a page method:

So for creating a page method let’s create a web empty web application.

Calling page methods with jquery
Monday, May 6, 2013

Unobtrusive validations in ASP.Net 4.5 Web Forms

With the release of ASP.Net 4.5 web forms there are tons of features added in the ASP.Net and Unobtrusive validations support is one of them. We have already seen that kind of validation in ASP.Net MVC and now we are going to have that in ASP.Net web forms. In this post I am going to explain how its works and how its different from earlier versions.

How validation was working with earlier versions?

In the earlier versions of ASP.Net it was working via putting a JavaScript for that. Let’s take a simple example. I have putted three things here. A textbox, required field validator and a button like following.

Friday, May 3, 2013

Server.Map alternative in WCF - HostingEnvironment.MapPath

If you are ASP.NET programmer then you already know about Server.MapPath. It is used to map a physical location on webserver for  You can find more information about Server.MapPath from the following location.

You can still use that in WCF(Windows Communication Foundation) service http bindings. As we know WCF Service can use other non http bindings like TCP/IP,MSMQ and Named Pipe. At that time HttpContext of WCF will be none so server.mappath will not be available as current context is not available. Because its a child class of HttpContext.Current.

HostingEnvironment.MapPath :

HostingEnvironment.MapPath works on all kind of bindings. So when you have other bindings like TCP/IP and MSMQ it will be available as it is inherited from the System.Web.Hosting namespace. You can find more information from below MSDN link.
Sunday, April 21, 2013

Server.MapPath variations

Those are working on in web technologies like ASP.Net and ASP are already familiar with server.MapPath method.  It is used to Maps a virtual or relative path to a physical path. Recently I had discussion with my friends what kind of options we have for Server.MapPath and We have discussed lots of things. So I thought it will be a good idea to write a blog post about it. I know this is very basic but sometimes we were not aware about basic things. So following are different kind of options we have with Server.MapPath.
  • Server.MapPath("~") return the root physical path of application.
  • Server.MapPath(“.”) returns the current physical path of a executing file(i.e .aspx file)
  • Server.MapPath(“..”) returns the current physical path of parent directory of executing file.
  • Server.MapPath("/") returns the he physical path to the root of the domain name
It’s very basic so if you try first one. It will load c:\users\lenovo\documents\visual studio 11\Projects\WebApplication1\WebApplication1\

It’s very easy hope you like it. Stay tuned for more.
Thursday, April 11, 2013

Two free spell checker extension for visual studio 2012 every developer should use

We all are humans and we tends to make mistakes. I am personally take care about lots of spelling mistakes when writing code but sometimes we don’t identify whether there is a spelling mistake there or not. At that time this spell checker extensions come handy.There are plenty of options available for spell checker extensions but In this post I am going to explain those two free spell checker plugins that I am using.

Spell Checker by Noah Richards:

This is a excellent plugin for identifying the spelling mistakes. You can download plugin from following url.

It’s provides spelling checks for almost all the files.
  1. Plain text files where entire file will be checked for incorrect spelling.
  2. Source code(.cs files)
  3. HTML/ASP files.
For each spelling error, the user is presented with a list of alternative spellings, via a smart tag (activate with ctrl+.), and the option to ignore the word or add it to the user's dictionary.

You can configure the color of the squiggle under misspelled words by changing the foreground color of Spelling Error (in Tools->Options->Environment->Fonts and Colors).  The default color is red. Following is a example for that.

Monday, March 25, 2013

ASP.NET 4.5 TextBox TextMode Property

Right now everybody is talking about HTML5 and its contains lots of new features like web sockets, canvas, new intput types with validation etc.

ASP.NET 4.5 text mode property enhancement:

With HTML5 new input types is going to be one of coolest feature and in future more and more people are going to use that feature. So in 4.5 development team given a support via TextMode property enhancements.

In earlier versions of ASP.NET we used have only three properties for TextMode property of textbox control.
  1. MultiLine- for multiline textbox.
  2. Password- for password textbox
  3. SignleLine –for single line textbox
With ASP.NET 4.5 you are going to have tons of options with TextMode property.
  1. Color- for Color entries
  2. Date-  for date entries. You can enter dates only
  3. DateTime – for datetime entries with respect to local time zone.
  4. DateTimeLocal- for datetime entries with respect to local time zone.
  5. Email- for email address
  6. Month- for month and year entry.
  7. Number- for  entering numeric values.
  8. Range- for containing range between two numbers.
  9. Search- for search field. A search field is like regular text fields
  10. Tel- for telephone number.
  11. Url- for website url entries. It will only contain urls.
  12. Week- for entering weeks and year.
Friday, March 22, 2013

My blog post appears on community spot light

Once again my blog post about CRUD operations with and petapoco appears on the Microsoft official website. I would like to thank you all for specially Jon Galloway and team.


A big thank you for every one for supporting me.
Wednesday, March 13, 2013

Default focus and default button in

Sometimes ago a friend ask me how to put default focus and default button in ASP.NET. He was not knowing that there is property for that in form tag in I thought it will be good idea to write a blog post about that.So those who are not aware of this feature can get an idea for that.

In some cases we need to have default button and default focus. For example take a example of a shopping cart registration form. Where you need to have default focus into the first textbox if you contain list of textboxes over there. Same way you can have more then one button on forms for example save and cancel so once user press enter which button should be pressed. On above both scenario this Default button and default features can come very handy.

From 2.0, The form tag has this two properties DefaultButton and DefaultFoucs where you can set the Id of control that you need as default button and same way you need to put Id of a control for default focus.
Sunday, March 10, 2013

How to consume ASP.NET WebAPI from jQuery

Before some months Microsoft has launched WebAPI framework for building and consuming HTTP services. This WebAPI can be called from anything like from mobile sites to JavaScript anything. I thought it will be a good idea to call a WebAPI from jQuery.

In this post I am going to explain how to consume ASP.NET WebAPI from jQuery.

Creating a basic WebAPI:

To create a web API you need to install ASP.NET MVC 4.0 and then you create a new project for MVC .

How to Create Webp API in Visual studio 2012

Now once you click Ok. It will ask for type of project. You want to create you need to select Web API there.
Thursday, March 7, 2013

SelectMethod in ASP.NET 4.5 Model binding

New version of ASP.NET provides lots of way of binding data to the data controls like Grid View, Repeater etc. You can now bind this controls with strong type. I have earlier written a blog post about How we can use Model binding with ASP.NET 4.5 and Visual Studio 2012.

SelectMethod in ASP.NET 4.5:

In 4.5, It is also possible to bind controls with SelectMethod attribute. This method specify the data that you want to bind with data controls like grid view and repeater controls.

So what we are waiting for?? Let’s take an simple example. I have create a basic model class “Employee” like below.
public class Employee
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
This class contains three properties EmployeeId,FirstName and LastName. Now I have created one method to get a generic list of employee model like following.
Wednesday, March 6, 2013

Tip- InProc session state was not working with IIS7.5

In this post I am going to explain about reason for InProcsessionState was not working with IIS 7.5.


Yesterday in one of my PC I got updated IIS 7.5 and suddenly after updating the IIS my ASP.NET application session state stopped working. It was a very weird behaviour and after doing some R and D I have found that my worker process per application was more then one.

That was the reason for the  session state was not working as “InProc” session state is using IIS memory to manage sessions.  You can find more information about this in following link.


There are two ways of resolving this problem.

1) You can use other session state mode like State Server or SQL server which supports multiple worker processes.

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