Thursday, September 13, 2007

Nested Gridviews in .NET 2.0 [Master - Detail]

This is my favourite 'feature' and it goes something like this................GridView within GridView

If you are working with a requirement for a detail view for selected item of master view then you must definitely consider this option.

Your aspx look is all your wish, if you are good in UI then you can explore what other options exist.

I choose the following,

simple table with a div section (i'll explain why we need a div later) as below.



grid1-colname will be unique column in your GridView1 (based on data)

RowBound method of your first gridview / master grid will have the following coding

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grid2 = (GridView)e.Row.FindControl("GridView2");
dbSrc = new SqlDataSource();
//Define your datasource here, connectionstring, selectcommand etc
grid2.DataSource = dbSrc;
grid2.DataBind();
}
}

Now adding a small Javascript, your Nested Gridviews are ready to go live :)

function switchViews(obj)
{
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
divobj=div;
if (div.style.display=="none")
{
div.style.display= "inline";
img.src="Images/collapse.jpg"; mce_src="Images/collapse.jpg";
}
else
{
div.style.display = "none";
img.src="Images/expand.jpg"; mce_src="Images/expand.jpg";
}
}

Now i presume you got why we need the div tag, and the ID we gave as unique column of GridView1 for the same.

Just F5....rest you know ;)

Wednesday, September 12, 2007

ASP.NET Ajax - Ajax extentions framework

Before we take a dip in Ajax Extentions

First download AJAX Extensions 1.0 on the Microsoft site

The download is contained in one Windows installer file; it also includes the Microsoft AJAX Library, which is the core component of using AJAX principles within ASP.NET.

In addition to the Ajax controls now in your developement Studio, you will find a new project type called ASP.NET AJAX-Enabled Web Site

Ajax controls packaged are as below

Timer: Allows you to update portions of a page, run server code, or post the entire page to the server at a defined interval.

ScriptManager: The starting point to provide AJAX functionality in an ASP.NET page.

UpdateProgress: Allows you to post status information pertaining to partial-page updates. You may use it when partial updates are slow, so the user gets feedback on the progress.

UpdatePanel: Allows you to perform partial-page updates.

You may only use one ScriptManager control per page but you may use multiple UpdatePanel controls.

The UpdatePanel control offers so many options to easily incorporate AJAX functionality — most notably, partial-page updates — with ease.

I'll be posting a sample application on these soon!!!

Till then happy programming...........

Monday, September 10, 2007

Invoking a windows service in .net in intervals

I had a requirement recently to run my little windows service every one minute or an interval thats configurable....

And Timers was straight forward solution for me...

Event on which you need to write your code is OnElapsedTime.

private void OnElapsedTime(object source, ElapsedEventArgs e)
{
streamWriter = new StreamWriter(new FileStream(
"C:\\WindowsServiceLogger.txt", System.IO.FileMode.Append));
this.streamWriter.WriteLine(" My Service got invoked again at " +
DateTime.Now.ToString());
this.streamWriter.Flush();
this.streamWriter.Close();
Start();
}
My task for the service is written in Start() method so i can call it directly OnStart and OnElapsedTime

Please remember to enable your timer OnStart and disable OnStop.

Seems all simple and easy....and it is :)

Happy Coding

Export to Excel from pagination enabled Gridview

Recently couple of developers asked me this question, so thought of pinning it down here....

very simple way is to turn of your gridview pagination and bind it. As you must be aware use HTMLWriter to render you control.

................
htmlWrite.RenderBeginTag(HtmlTextWriterTag.Tr);
GridView4.AllowPaging = false;
GridView4.DataBind();
GridView4.RenderControl(htmlWrite);
htmlWrite.Write(" Header of my GridView");
htmlWrite.RenderEndTag();
htmlWrite.Write("
");
...........................

to make your excel look cool add/modify the htmlwriter settings....n e questions shoot me a mail :)