Wednesday, September 19, 2007

Associating events to dynamically created controls

Continued......

After the controls that are created dynamically now the questions is events of that controls

i came across this microsoft link which looks helpfull and is trying to match with my requirement.

Add rows to asp table web control dynamically

Came across this 'constraint' recently and was amazed to find not much solution exist in .net world for this.

Requirement: Add rows dynamically to an asp table, web control(or say dynamically you want to keep on adding controls on click / some event)

Issue: I was able to add row but previously added row 'vanishes' from UI

Reason: Viewstate of dynamically created control is lost on next post back

Solution:

Added a session to store state of table and to render/recreate the control on post back

code as below in page_load event

if (Session["rowList"] == null)
{
//No row list, Create it.
rowList = new List();
}
else
{
rowList = (List)Session["rowList"];
}
//Recreate the table. (Prevents loosing data already entered into textboxes.)
DisplayTable();

There is an addrow method called on click (/ any event you like )
You have to add our newly formed row to our row List as below.

rowList.Add(tableRow);
Then save our Row List on to the session
Session.Add("rowList", rowList);
Call the DisplayTable();

Finally recreating table as below in DisplayTable function

protected void DisplayTable()
{
//Run through the row list, and add each one to the Table's Row Collection.
foreach (TableRow tr in rowList)
Table1.Rows.Add(tr);
}

Any other suggestion / idea you have please post it here....coz i am still not convinced by this idea :(

Thursday, September 13, 2007

New features of .NET 2.0 framework

Came across this link.........lists features of .NET framework 2.0

http://asptoday.com/Content.aspx?id=2368

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 :)