Archive for the ‘ASP.Net’ Category

Another small tip for you 🙂 Sometimes when you have your web page hosted on multiple servers,you may want to get the complete url of the loaded page from the code behind.Below code can help you get the complete path.



Here are the steps to validate EmailAddress in using RegularExpressionValidator Control.

Add a RegularExpressionValidator control to your web page.

Set the ControlToValidate property to indicate which control to validate.
Set the ValidationExpression property to  regular expression as shown
in below image.

Set the Text property to define the message to display if the validation fails.

If you ask me what caused this exception,i am really not sure about that but would like to share what helped me to resolve this  Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

To resolve this, set the validateRequest attribute of the Page directive to false ValidateRequest=”false”

Read more about this attribute here

When you have a GridView with editable controls like TextBox,CheckBox etc it becomes necessary that you validate the controls before clicking update.I am sure there could be different ways to access the controls in GridView.Here I am posting  how it is done without hitting the server side using javascript.

<EditItemTemplate> <asp:LinkButton ID="lbkUpdate" runat="server"
 OnClientClick='javascript:return Validategrid(this);' CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> </EditItemTemplate>
 function Validategrid(btnCtrl)
var gridView = document.getElementById('<%= grd1.ClientID %>');
var selectedRowIndex = btnCtrl.parentNode.parentNode.rowIndex;
var txt1 = gridView.rows[parseInt(selectedRowIndex)].cells[0].children[0];
var txt2 = gridView.rows[parseInt(selectedRowIndex)].cells[1].children[0];
var chk1 = gridView.rows[parseInt(selectedRowIndex)].cells[2].children[0]; ;
var chk2= gridView.rows[parseInt(selectedRowIndex)].cells[3].children[0];

 if (chk1.checked == false && txt1.value == "" && chk2.checked == false && txt1.value == "")
   alert("Please provide inputs");
   return false;


Some of the users might have already faced the scenario of having databound controls like checkboxes inside GridView, which gets created at runtime.The problem here is if you look at the client id of checkboxes generated at run time,it will be quite unpredictable to use with document.getElementById and accessing it is not very straight.At run time the clientID will be something like GridView1_ctl01_CheckBox1 for a particular check box. ctl01 will not remain same it will be ctl02, ctl03 etc for the checkboxs in the respective rows. So you will have to manipulate the clientid someway so that it turns predictable & you can acess each checkbox to do some operations like making it checked or unchecked based on your conditons.

What I explained so far is history now 🙂 Things have changed in .Net 4.0 .ClientId is no longer unpredictable.Cool right!A much awaited feature is introduced now.

Details of changes in .Net4.0

.Net 4.0 introduces two new properties ClientIDMode Property & ClientIDRowSuffix Property which can help you in this.If you set the ClientIDMode property to Predictable, ASP.NET generates the ClientID by appending a suffix that is derived from the data field specified in ClientIDRowSuffix(Any column name you can specify as ClientIDRowSuffix for eg EmpId column of your datasource ) . If ClientIDRowSuffix is not set, the suffix is a sequential number.This number begins at zero and is incremented by 1 for each row. and each segment is separated by an underscore character (_).

ClientIDMode enumeration values are as below:

This algorithm was used in versions of ASP.NET earlier than ASP.NET 4 which I have already explained.If you use Visual Studio to convert a Web project to ASP.NET 4 from an earlier version, Visual Studio automatically sets the site default to AutoID in the Web.config file
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
I have already explained this how it works with ClientIDRowSuffix Property
The control inherits the ClientIDMode setting of its parent control.

Right now we saw how this property can be set at the Control level.In two more ways you can set this:

Page Level -Set the ClientIDMode value for a page in the @ Page directive
Application Level-Set the ClientIDMode value for all pages in a Web site by setting the pages element in the site’s Web.config file.

MSDN has got very good explanation for this.But there it’s a bit vast & might be difficult for a beginner to understand.Hence I have grabbed few things from there & presented it in my own style.Hope you find this helpful!

FaceBook users might have noticed the Like button which allows you to connect web pages & share them on Facebook.User logged into Facebook will see the profile pictures of friends who like the page & the number of “likes” for that particular page.

I have seen this integration of web pages with FB & have wondered how it works & made me take a deep dive into the ViewSource of one page & I figuered out the logic behind that

It’s very simple,the Like button can be added as a iframe to your aspx page.

<div align="center">
<iframe src=
scrolling="no" frameborder="0" style="background:#FFF; border:none; overflow:hidden; width:370px; height:300px;">

What matters is,the id which you see here “66386416740”. If you change it accordingly to your FaceBookPage,then you are done 🙂 Now,if you are curious how I figured out the id for the example page,it’s simple login to FB,Search for the page,from browser choose View Source,look for this below tag & get the id

<link rel="alternate" 
media="handheld" href="" 

You can read about iframe code & different parameters here:

Hope you enjoyed the post.Try integrating your sample aspx with your favourite FB page & let me know how it went 🙂

There are many times when you want to retrieve and manipulate the rendered HTML in code before its displayed to the user.Dynamic Loading of ASP.NET User Controls is required before you render the html & it can be done using the LoadControl Method.

Control control = this.LoadControl("~/WebUserControl.ascx") as Control;

If you try to use the LoadControl method of page,the drawback of this is that it has no control over where on the page the user control will actually appear.Say you have a button in user control & you are trying to load the user control with LoadControl method of page,then it will throw exception as below

Control ‘Button1’ of type ‘Button’ must be placed inside a form tag with runat=server.

Hence I suggest,use the LoadControl method of UserControl as below

UserControl userControl = new UserControl();
Control control = userControl.LoadControl("~/WebUserControl.ascx") as Control;

Below method allows to pass control and it will return the rendered HTML as a string

public string ControlRender(Control control)
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
return sb.ToString();