JG Vimalan's Blog

It is all about coding!

Clickable GridView with Postback in ASP.NET 4.0

In this post, I will show you how to make a GridView row clickable and read the selected row values after postback.

Step 1:

Create an ASP.NET 4.0 web application. Add a gridview control in the application.

Step 2:

Create RowCreated and RowDataBound events and set their values as shown below,

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Attributes.Add(“onclick”, “CreatePostBack(this);”);

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
e.Row.Attributes.Add(“onmouseover”, “mouseIn(this);”);
e.Row.Attributes.Add(“onmouseout”, “mouseOut();”);

Here, the CreatePostBack(this); is a javascript function. In this function call the _doPostBack function and pass the necessary parameters as shown below,

<script type=”text/javascript”>
function mouseIn(row) {
document.body.style.cursor = ‘pointer’;
function mouseOut() {
document.body.style.cursor = ‘auto’;

function __doPostBack(eventTarget, eventArgument) {
var theForm = document.forms[‘ctl01’];
if (!theForm) {
theForm = document.ctl01;
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;

function CreatePostBack(row) {

__doPostBack(‘GridView1’, row.rowIndex);

//in this example, i am just passing the rowIndex. You can read the selected row values and pass them.

Step 3:

Add two hidden fields as shown below for capturing the  event target and event argument,

<input type=”hidden” name=”__EVENTTARGET” id=”__EVENTTARGET” value=”” />
<input type=”hidden” name=”__EVENTARGUMENT” id=”__EVENTARGUMENT” value=”” />

Step 4:

In the Page Load event, set the data source for the gridview control and the code for reading the values when there is a post back,

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
GridView1.DataSource = GetUsers(); //use your method for getting datasource value here
else if (Request.Form[“__EVENTTARGET”] == “GridView1”)
var arg = Request.Form[“__EVENTARGUMENT”];
//now you got the selected row values

Step 5:

Thats all! The Gridview markup is given below for your reference,

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>
<input type=”hidden” name=”__EVENTTARGET” id=”__EVENTTARGET” value=”” />
<input type=”hidden” name=”__EVENTARGUMENT” id=”__EVENTARGUMENT” value=”” />
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”false”
onrowcreated=”GridView1_RowCreated” onrowdatabound=”GridView1_RowDataBound”>



So, whenever a gridview row is clicked, the postback will happen and you can read the selected row values.

The working sample can be downloaded from here,




September 15, 2012 Posted by | ASP.NET, C#.NET, VS 2010 | , | 9 Comments

Email critical exceptions using Enterprise Library 5.0 Logging Application Block

I got a task where I need to email specific exceptions (like database connectivity issues) to an email address.  The idea is to notify the support team immediately about the critical issues. My project was developed using Visual Studio 2010, ASP.NET and C#.  In order to complete this task,  I used Microsoft Enterprise Library 5.o Logging Application Block. The steps are very simple and it is given below,

a) Install Microsoft Enterprise Library 5.0 in your system. You can download from : http://www.microsoft.com/en-us/download/details.aspx?id=15104

b) Add reference to Microsoft.Practices.EnterpriseLibrary.Logging in your project.

c) Launch ‘Edit Enterprise Library V5 Configuration’ wizard by right clicking the web.config file as shown below,

d) Under ‘Blocks’, select ‘Add Logging Settings’. This will display default logging settings in the wizard.

e) Now define the exception categories (Lets say, General, Error, Critical etc). The idea is to send email only if the exception category is Critical. Now add the email trace listener and provide necessary details as shown in the image below. Add a log message formatter. In my case, I added Text Formatter. In email trace listener set the formatter as ‘Text Formatter’. In Critical category, set the Listeners as Email Trace Listener. Finally, the settings should look as shown below,

f) Save the file and close it. The settings will be displayed in the web.config file.

g) In the code, add multiple catch blocks as shown below and set the category to ‘Critical’ for those exceptions you want to capture in email.

Now, whenever there is a null reference exception, an email will be sent.

Sample email with exception is given below,


August 26, 2012 Posted by | C#.NET, Enterprise Library 5.0 | , , | Leave a comment