Wednesday, January 23, 2008

How do I use ASP.NET File upload control inside AJAX Update panel?

Can you use ASP.NET File Upload control inside the AJAX Update Panel without any difficulty? Actually, ASP.NET File upload control requests full post back to post the file to server. So, when you put File upload control inside the AJAX Update panel, it will return null value as a post file.

So, what is the solution for this? Yes, you can define post back trigger to fire full page post back. I hope you realized the set-up behind this and next time I will post about “How do I use ASP.NET File upload control inside the GridView + Update Panel?”

FAQ00023

Labels: ,

Tuesday, January 22, 2008

How do I detect basic settings of my browser?

Actually this is pretty common question. Anyway, I will post small code list to detect basic browser settings at once. Specially, you better to check browser setting before you load AJAX applications, ActiveX, etc…

FAQ00022

Labels:

Thursday, January 17, 2008

How do I use decimal value with ASP.NET AJAX ASP.NET AJAX NumericUpDownExtender?

I hope you know the ache of implementing ASP.NET AJAX NumericUpDownExtender with decimal values. Specially, without a web service. Anyway, practically I never face this problem before. But I picked this post from CDNUG forum.

In this example I will show to how use ASP.NET AJAX NumericUpDownExtender to increase 0.25 and decrease 0.25 with two simple web methods.

FAQ00017_1

FAQ00017_2

Labels: ,

Wednesday, January 16, 2008

How do I call client onload from ASP.NET ContentPage?

When we use ASP.NET Master Page inside our web application, body tag automatically remove from content pages. Coz, at runtime Master page body tag render as content page body tag. But the problem arises when we want to access the content page body element. Just say we want to call JavaScript function from client page body onload event.

With ASP.NET, we call solve this issue with some extra code.

Step 1: Set ID and runat tag for Master Page body element.

FAQ00016_1

Step 2: Put below code inside the content page load event.

FAQ00016_2

Labels:

Tuesday, January 15, 2008

How do I remove unnecessary white spaces from collection of strings?

Actually this question I have gathered from CDNUG forum. So as usually, I tried to give answer and finally manage write below code.

The string need to remove unwanted white spaces:

“ I    Love          ASP.NET          forum. “

The final output should be like below:

“I Love ASP.NET forum.”

Solution:

FAQ00015

Labels:

Monday, January 14, 2008

How do I manage the MaxLength property of Multiline Mode ASP.NET TextBox?

You know we can manage the number of maximum characters of the ASP.NET TextBox through MaxLength property. We just need to set the numeric value to MaxLength property and ASP.NET TextBox will handle it. But if we alter the ASP.NET TextBox to Multiline mode, the MaxLength property will not accomplish its job by restricting the maximum characters.

But luckily, we can limit the maximum characters of Multiline Mode ASP.NET TextBox by using JavaScript or Regular Expression.

JavaScript Solution:

FAQ00014_1

Regular Expression Solution:

FAQ00014_2

Labels:

Sunday, January 13, 2008

Why does Firefox ignore the ASP.NET GridView border property?

By default, ASP.NET GridView comes with border property equal to null. So, you can see the vertical and horizontal lines in GridView. No matter about the browser type, it shows correctly. But if you set the GridView border property to zero (border=”0”), it will behave differently in Microsoft IE and Firefox web browsers. Because, Microsoft IE shows the GridView without borders, but Firefox shows the same GridView with borders even border=”0”. Simply Firefox ignore the ASP.NET GridView border property.

FAQ00013_1

Anyway, if we really want to hide the ASP.NET GridView gridlines inside Firefox web browser, we can do it without playing with CSS. Simply we have to set the GirdView GridLines = “None”. Because, by default GridLInes property comes with “Both” value. So, it shows the horizontal and vertical lines inside the ASP.NET GridView.

FAQ00013_2

Labels:

Saturday, January 12, 2008

How do I use the ASP.NET Label correctly?

Here, I am just going to show tiny tip about ASP.NET Label control. Actually, it isn’t all about tip; just want to explain the way of using it correctly. As you all know we use ASP.NET Label control to present some dynamic or static text. But ASP.NET Label can do an extra task with its AssociatedControlID property.

FAQ00012_1

FAQ00012_2

According to the above screen shot, we have one ASP.NET Label and ASP.NET TextBox. ASP.NET Label is doing its job by showing the text. But what do you say if you can set focus the cursor on TextBox when you click on the Label. Yah, you can do it easily with AssociatedControlID.

FAQ00012_3

Labels:

Friday, January 11, 2008

How do I dynamically add CSS file for ASP.NET ASPX page?

Normally, in our ASP.NET applications we use CSS in Master Pages. Then it will automatically inherit to child pages. But there is an option in ASP.NET to put page specific CSS file and it will swap the master page CSS inheritance.

To implement this we programmatically access the HTML link element under header tag in Page_Init.

FAQ00011

Labels: ,

Thursday, January 10, 2008

How do I change the default selected item of runtime data bounded ASP.NET DropDownList?

When you bind data to ASP.NET DropDownList at runtime, the default value will be the first data item of the returned dataset. So, by default we can see the selected data item of DropDownList. But what should we do, if we want to have non selected DropDownList or if I want to show text like “—Select—“at runtime.

With ASP.NET you can do it like below:

FAQ00010

Nothing big with above code. Just add what you want inside the DropDownList DataBound event and don’t forget to set the item index to zero. The other thing is here we insert the DropDownList item, not the adding.

Labels:

Wednesday, January 9, 2008

How do I add mailto: to Hyperlink control in GridView, DataList, DetailsView, etc…?

In our practical coding life, we have to display email address inside the ASP.NET data controls like GridView, DataList, DetailsView, etc… Actually, we can display (Bind/Eval) the email address with Hyperlink just as Text. But with field like email address, we should add the mailto: tag. Then customer can just click on email address and call the default local email client to send an email.

With ASP.NET you can do it with simple modification. But make sure not to add only mailto: (concatenate) with email address. You must format the string with email address like below.

FAQ00009

Labels:

Tuesday, January 8, 2008

How do I catch the RowIndex in GridView RowCommand event?

As you all know ASP.NET GridView is the successor of the ASP.NET 1.X DataGrid. But if someone asks me, what is foremost drawback of ASP/.NET GridView according to you. Absolutely, I will say “There is no built-in way to take the RowIndex in RowCommand event”. Because with DataGrid, we were able to catch the ItemIndex in ItemCommand event.

Anyway, luckily there are some unusual ways to catch the RowIndex in GridView RowCommand event. So, I will show the easiest way to do it. But if your guys have any other proven way, don’t forget to share it with me.

FAQ00008

Labels:

Monday, January 7, 2008

How do I add serial No for GridView column?

Actually this is a very simple technique but very useful. Coz, by default GridView doesn’t support for row serial no. But most of clients used to have column Serial No just like Microsoft Office Excel sheet. So, plainly we can approach the solution like below. 

 FAQ00007

Labels:

Sunday, January 6, 2008

How do I reset all the TextBox values at once?

If you have any experience with data handling asp.net applications, then this is a very common issue for you. Even I saw few  forum post  in CDNUG.
With classic ASP, we have cleared all the TextBox values with simple “Reset” button. But when we come to ASP.NET we can’t apply the same.

Here, I am going to explain some tips with ASP.NET to overcome this problem.

Solution 1:

As I said earlier still you can use classic asp way to clear all the TextBox values with some conditions.

FAQ00006
FAQ00006_1

This is not ASP.NET specific way to clear all the TextBox values. Here the reset method calls the JS onreset event of HTML form object and it just reset the original value the of the TextBoxes. Because original value is empty at the first time. But we can’t use form reset method to clear TextBox values when populated data already submitted to the server. (Because original value is not empty after submitting to the server).

Solution 2:

FAQ00006_2
Above code will clear all the TextBox values even submitted TextBox values also. The only problem is server postback experience and it will be worse (performance) when we have large collection of ASP.NET text boxes.

Solution 3:

FAQ00006_3

When you call Server.Transfer, the page execute as a new page. I guess this is the easiest way to clear all the TextBox values. But it will not clear only TextBox values, but form collections and QueryString also...

Download source code:ClearAllTextBoxValues.zip

Labels:

Saturday, January 5, 2008

How to manage image path inside ASP.NET Master Page?

Here, I am going to talk about very common issue that coming with ASP.NET Master Page. If you are frequent visitor of any ASP.NET forum for sure you should see lot of thread regarding this issue.

Problem:

FAQ00005_1

If you see above image, you can identify the project file structure inside VS IDE server explorer. The “my.master” page uses the “myimage.jpg” located in “image” folder. So, image URL should be like below.

FAQ00005_2 
The “default.aspx” page use “my.master” page and when you run the "default.aspx" page you can see the image without any problem. Because image path is correct as above. But when you run the “default.aspx” page that located inside the “admin” folder, you can’t see the image. Because image path is invalid due to its request another “image” folder inside the “admin” folder.

FAQ00005_3 
I hope, now you understand the problem behind this issue.

Solution 1:

You can overcome this issue by using ResolveClientUrl method. The ResolveClientUrl method automatically manages to exact path even inside the different folders. In this case ResolveClientUrl correctly return the image path.

FAQ00005_4

Anyway, you better to remember this method only work at runtime. So you can’t see the image at application design time. Personally I feel dull when I can’t see images at design time.

Solution 2:

FAQ00005_5

"Add runat="server" to "img" element"

I guess this is the very best technique for this issue. Even we only have to treat "img" element as server side control with runat=”server” tag. Then, ASP.NET always manages to locate the exact image path at design time and runtime. And also you better to remember you can simply overcome this problem with ASP.NET server side image control.

Download source code: MasterPageImagePath.zip

Labels:

Friday, January 4, 2008

Why we call Page.Validate() and check Page.IsValid even when we have client side validation?

From end user perspective, client side validation is absolutely giving more benefits. Anyway, technically who can trust the user inputs? Because JavaScript is key player in client side valuation but anyone can bypass the JavaScript by disabling it. Not only that, brilliant hacker can swap the JavaScript validation functions through automated process.

Solution with ASP.NET 2.0:
FAQ00004

When Page.Validate() is fired, it automatically check the CausesValidation = true controls and return the page validation status through Page.IsValid property. Here you have another option to call Page.Validate() method for specific validation group and get the status.

BTW: Don’t forget to call Page.Validate() before checking Page.IsValid.

Labels:

Thursday, January 3, 2008

What can I do with ScriptManager.LoadScriptsBeforeUI?

With ASP.NET AJAX you can have property called LoadScriptsBeforeUI. Oh, what is that…? Yes, it can’t do anything big. But worth to consider if you think about ASP.NET AJAX UI.

In our AJAX life you know some AJAX applications UI loads some fast but it takes few seconds to interact with client. On other hand some applications UI takes more times to load, but can interact with client as soon as it loads. So, with ASP.NET ASP.NET AJAX ScriptManager, you can choose what you want. It may be first load UI markup and then scripts or…

FAQ00003

If you set ScriptManager.LoadScriptsBeforeUI = True, then script load first and next UI markup. With this option UI will be handy when it loads.
If you set ScriptManager.LoadScriptsBeforeUI=False, then UI load first and next Script. With this, UI will load some fast but have to wait till script load.

FYI: LoadScriptsBeforeUI default value is TRUE.

Labels: ,

Wednesday, January 2, 2008

How to detect the connection status among server and client in my ASP.NET application?

With our ASP.NET application, sometimes we may have to process time consuming queries like database transaction, payment gateway integration, time out,  etc... In such situations we have to check the client connection is still alive or not. Coz there is a gap between client request time and server response time.

So, we better to check the client connection status after running complex / time consuming process. Otherwise, server unnecessarily keeps the client’s response and it affects to the server performances.
With ASP.NET you can do it simply like below.

FAQ00002 
FYI: IsClientConnected is a read only property. If IsClientConnected is true, that means client has live connection with the server. If IsClientConnected is false then you can release the server by ending the client response.

Source code: IsClientConnected.zip

Labels: ,

Tuesday, January 1, 2008

How to track the last modified date /time of ASPX page?

One of our customer may ask “I want to see the last modified date or time of my web site. Just show it around the footer so somewhere”. Because, then customer can see the last modernize date/time of web site’s physical files. With ASP.NET, you can archive this with few lines of code.

FAQ00001

You can place above code inside the ASP.NET master page or ASP.NET web user control. Then you can catch the last modified date time of all ASPX pages from single location.

FYI: Above code doesn’t support for non ASPX pages or dynamic ASPX pages (like URL rewriting). So in that case you will have to have added way to track it. Like storing it in the database table or XML file.

Download source code : LastModifiedDateTime.zip

Labels: ,