public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Archive for February, 2009

Capture button’s click event of user control in the page.

Posted by Virendra Dugar on February 22, 2009


Yesterday one of my colleagues was facing a problem with button placed in user control. He wanted to capture button’s click placed in user control inside the page that holds the user control.

I made a sample application for him. I thought I should share with you also.

Download Sample Code.

This is my user control (.ascx) file. As you can see I have placed a button and onclick event is also defined. (See image.)

User Control

If you want to extend the button click placed in the user control to the page, then declare an Event handler and on the click of the button just make a call to the declared event handler.  This will work as a property for any web page. See below image.

User Control

Place the control in your .aspx page. In the Page_Load event of your page, assign a new event handler to the User control’s event handler property. And, now use the newly declared event handler.

Web Page

So, now when button (which is placed in user control) is clicked, event handler declared in the page will be used to handle the click event.



Posted in ASP.NET | Tagged: , | 4 Comments »

ASP.NET Questionnaire

Posted by Virendra Dugar on February 17, 2009

Hi All,

I found some pretty good stuff of ASP.NET Questionnaire. Check it….



Posted in ASP.NET | Tagged: | Leave a Comment »

Difference Between RegisterClientScript & RegisterStartupScript

Posted by Virendra Dugar on February 13, 2009

The main difference is that the RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing /form element. The RegisterClientScriptBlock method places the JavaScript directly after the opening <form> element in the page.

Both of these methods take two strings as input. The second parameter, script , is the client-side script-including the opening and closing script tags. The first parameter, key , serves as a unique identifier for the inserted client-side script. 

The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object’s <form runat=”server”> element. 

The code cannot access any of the form’s elements because, at that time, the elements haven’t been instantiated yet. This explains why hdnView variable had a null value in my case. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object’s <form runat=”server”> element. 

The code can access any of the form’s elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the “order” in which you want your script to be run by the browser when rendering the page.

As for example if you have declared a hidden variable and want to use in your java script by any of these method then java script method registred with RegisterClientScriptBlock method won’t be able to find the hidden variable as the form elements are not instantiated yet.


Posted in ASP.NET | Tagged: | Leave a Comment »

Weird thing with inner queries in SQL SERVER 2005

Posted by Virendra Dugar on February 12, 2009

Few days ago, I found a very strange thing with SQL SERVER 2005 and I strongly feel that THIS IS A BUG in SQL SERVER  which actually can create lots of data discrepancy.

To prove my point I have created a sample SQL Script. Just run the script in SQL SERVER.

Details of the Script

Create new database.


Use this Database.


Create a table named tblCategory

CREATE TABLE tblCategory
      CategoryID INT PRIMARY KEY,
      CategoryNAME VARCHAR(50)

 Create another table named tblProduct

      ProductID INT PRIMARY KEY,
      CategoryID INT FOREIGN KEY REFERENCES tblCategory(CategoryID),

Insert 5 rows in tblCategory

INSERT INTO tblCategory VALUES (1,‘Category1’)
INSERT INTO tblCategory VALUES (2,‘Category2’)
INSERT INTO tblCategory VALUES (3,‘Category3’)
INSERT INTO tblCategory VALUES (4,‘Category4’)
INSERT INTO tblCategory VALUES (5,‘Category5’

Insert 10 rows in tblProduct

INSERT INTO tblProduct VALUES (1,1,0)
INSERT INTO tblProduct VALUES (2,1,0)
INSERT INTO tblProduct VALUES (3,2,0)
INSERT INTO tblProduct VALUES (4,2,0)
INSERT INTO tblProduct VALUES (5,3,0)
INSERT INTO tblProduct VALUES (6,3,0)
INSERT INTO tblProduct VALUES (7,4,0)
INSERT INTO tblProduct VALUES (8,4,0)
INSERT INTO tblProduct VALUES (9,4,0)
INSERT INTO tblProduct VALUES (10,5,0)

Select statement to confirm the data is inserted or not.

SELECT * FROM tblCategory
SELECT * FROM tblProduct 

Here is a select query which is not correct. The query tells select ProductID from tblCategory table where CategoryID = 1. But the problem here is tblCategory  is not having a column named ProductID. So this query throws an error and that’s a correct behavior.

SELECT ProductID FROM tblCategory WHERE CategoryID = 1

Here is the magic. I have used the above select query which is not correct as a inner query with a update statement. What to do you think? What will happen? Update query should throw an error. Logically it should.I was thinking the same But just execute this update query and you will be shocked.

UPDATE tblProduct SET ISCOMPLETED = 1 WHERE ProductID IN (SELECT ProductID FROM tblCategory WHERE CategoryID = 1)

Oops, 10 rows affected. Surpised. All the data in ISCOMPLETED field is set to 1 but my inner query (SELECT ProductID FROM tblCategory WHERE CategoryID = 1) is wrong.

This is just a sample query but When I executed such a similiar statement, 3364 rows in my table was updated.

SELECT * FROM tblCategory
SELECT * FROM tblProduct

Initally I thought that this is a bug but actaully it’s not.  The inner query first tries to find the column in the current table(inner query’s table) and if it does not found then it will look for the outer query table. It is the best practice to use the tableName.ColumnName in the inner query. 

UPDATE tblProduct SET ISCOMPLETED = 1 WHERE ProductID IN (SELECT tblCategory.ProductID FROM tblCategory WHERE CategoryID = 1)


Waiting for your comments…..

Posted in SQL Server | Tagged: | Leave a Comment »

Handle back button of the browser

Posted by Virendra Dugar on February 11, 2009

The “Back” browser button cannot be actually disabled by a web application as the browser security will not allow this.
If we want user to stay on the same page, even if user presses back button, then we can use javascript to achieve this.


This method will always redirect to the user on the current page itself.

For example, user is on page 1 and user goes to Page 2. Now from page 2 if user press back button of the browser then user will be redirect to the page 2 itself.

window.history contains the collection of the page visited by the user for particular browser session. window.history.go(+1) and window.history.forward(); are same.

But if we want to display web page expired warning on the click of back button, then we must allow the browser to cache the page. This can be achieved by ASP.NET

/*To not to store the page in cache.*/

But one problem with this approach is that this will only work when there are some dynamic content in the page. i.e. change any combo or dropdown box value and then try to press back button.

If you can find out better solution then this please let me also know.


Posted in ASP.NET | Tagged: | 1 Comment »

ASP.NET Best Practices for High Performance Applications

Posted by Virendra Dugar on February 10, 2009

Hi All,

I found some great articles on Best pracites and improving the performance of ASP.NET application. Check out these great articles.

Article 1

Article 2

Article 3


Posted in ASP.NET | Tagged: | Leave a Comment »

SubmitDisabledControls – A New Property in ASP.NET 2.0

Posted by Virendra Dugar on February 9, 2009

In one of my previous post, I blogged about ReadOnly and Enabled property of TextBox, One of the issues with disabled controls is that if controls are marked disabled on the client-side, the values are not posted back and therefore any changes are lost from the previous visit. 

ASP.NET 2.0 add a new property “submitdisabledcontrols ” to HTMLForm Class, that when set to true will submit the disabled controls to the server also.


Posted in ASP.NET | Tagged: | Leave a Comment »

ReadOnly and Enabled property of TextBox

Posted by Virendra Dugar on February 6, 2009

One of thing about textbox that confuses me is the Enabled and ReadOnly Property. Both the property makes the text content non editable but there are couple of differences in the way they work.  Here they are:-

Difference No. 1

When we use Enabled Property to false, Textbox’s text is grayed out and we cannot focus on the control and when we use ReadOnly property to true then you will see that the textbox’s content is not grayed out but the content is non editable. 

Difference No. 2

When Enabled = “false” is used, control renders the HTML element as disabled=”disabled” and in case of ReadOnly=”true” then the HTML element is readonly=”readonly”.

I have read on some blogs that disabled controls values are not sent back to the server on postback but that’s not true. If you have disabled any control on client side then only its value will not be sent back to the server. Disabling on client side means either through JavaScript or by adding attributes to the control like

txt.Attributes.Add(“disabled”, “disabled”);


Posted in ASP.NET | Tagged: | Leave a Comment »

Good Programming Practices

Posted by Virendra Dugar on February 5, 2009

I found a very cool document reagrding good programming practices.. Go and check it out guys..

If you are not able to view then click here.



Posted in ASP.NET, C# | Tagged: , | Leave a Comment »

ref and out in C#

Posted by Virendra Dugar on February 3, 2009

When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable. Even the variable passed as out parameter is similar to ref, but there are few implementation differences when you use it in C# .

Argument passed as ref must be initialized before it is passed to the method, where as in case of out its is not necessary,but after a call to the method as an out parameter the variable must be initialized.

When to use ref and out parameter

Out parameter can be used when we want to return more than one value from a method.


Posted in C# | Tagged: | Leave a Comment »

%d bloggers like this: