Tuesday, September 26, 2006

Which control caused postback in asp.net

ASP.NET server controls has a property named AutoPostBack. If we set AutoPostBack property of any ASP.NET server control to True then whenever this control is clicked page is posted back to the server automatically. Sometimes we need to find which control on the server caused the post back.

In ASP.NET, Request object contains a property named __EVENTTARGET that tells us which control caused the post back. It gives back ID of the control which caused the past back. You can try like this Request[“__EVENTTARGET”].

Note: there are two underscore characters before the EVENTTARGET text.

If you are using simple controls which do not contain other controls and you want to find the name of the control which caused the postback then you can simple use the following line of code

Response.Write(Request["__EVENTTARGET"]);

Or you can get the value in some text variable as given below.

string itemCausedPostback = Request["__EVENTTARGET"];

In case you are using some control which is actually a collection of controls then the way is slightly different. For example you are using a CheckBoxList control, which contains AutoPostBack property set to True. Now, if you want to find which checkbox caused the post from with in the list then try this.


First of all get the value of the EVENTTARGET property in some variable

string itemCausedPostback = Request["__EVENTTARGET"];


Now get the index of “:” sign from the string, because the part before the “:” is the name of the control and the part after the “:” is the index of the control with in the list. Now, get substring starting right from the next character of the colon and having length till end of the string.

int startIndex = itemCausedPostback.IndexOf(":")+1;
int length = itemCausedPostback.Length - startIndex;

int indexId = Convert.ToInt32(itemCausedPostback.Substring(startIndex,length));
Now you can use this indexId as an index of the CheckBoxList item.
Response.Write(this.CheckBoxList1.Items[indexId].Text);

For example you clicked the second item in the list the indexId will contain 1 and the this.CheckBoxList1.Items[indexId].Text will give the text of the item at that index.

Thursday, September 14, 2006

C# (C Sharp) Sample Chapters

Here is a list of sample chapters from some renowned titles. I think they are really very useful, not only, for the beginners, but also, for experienced developers.

Visual C# 2005: A Developer's Notebook - Chapter 1: C# 2.0 (PDF Format)

C# Cookbook - Chapter 8: Regular Expressions (PDF Format)

Learning C# 2005 - Chapter 11: Inheritance and Polymorphism (PDF Format)

Monday, February 06, 2006

Start and Stop Windows Services using Visual Basic 2005

Start and Stop Windows Services using Visual Basic 2005

Different windows services are used to perform different operations on Windows operating system family. These services may include database services, email services or a lot of other. So, while performing automated tasks we need to start and stop these services programmatically. Here, I’ll show you how to perform this task in Visual Basic 2005.

Go to Control Panel and then Administrative Tools and Open Services Tool. Here you can find all the windows services running at your system. For example, you want to stop and start Microsoft SQL Server Service. First of all find out the name of the service from the services tool.
Name of the Microsoft SQL Server Service





















Its name is MSSQLSERVER. We’ll use this name to start and stop the service.

Create a Visual Basic.Net Windows Application project in Visual Studio 2005.
The Namespace we are going to use in this article is System.ServiceProcess.
So, import this namespace in your Visual Basic.Net project. Create an object of ServiceController class of this namespace. While creating this object you’ll have to provide the service name as parameter.


Dim objController As New ServiceController("MSSQLSERVER")

You’ll have to find the status of the service before you stop it. So, check if the status of this service is running then call stop method on this service.

If objController.Status = ServiceControllerStatus.Running Then
objController.Stop()
End If

You can give some delay so that you’ll be able to view the result of these statements.

System.Threading.Thread.Sleep(5000)

Here we have given delay of five seconds. Now, you can start your service again with the Start method of Service controller class.
But remember, check the status again and if the service has already been stopped then start it again otherwise you’ll be caught by an exception.

If objController.Status = ServiceControllerStatus.Stopped Then
objController.Start()
End If

Thursday, February 02, 2006

Learn Microsoft.Net Framework 1.x and 2.x based technologies.

I'm a developer working in Microsoft.Net technologies, and i would like to share my knowledge with all other developers or students who are learning Microsoft.Net technologies.

I have passion for development and i love Microsoft technologies. I have been working in these technologies since there was a DLL Hell, you know! But now a days i enjoy working in latest Microsoft technologies.

I hope you'll get a lot of help from me and i would also like to learn from you all.

Thanks