ASP.NET Programming tips

Easwaran ParamasivamSenior Software Engineer
Published:
In this Article, I will provide a few tips in problem and solution manner.

Opening an ASPX page in Visual studio 2003 is very slow.


To make it fast, please do follow below steps:  

Open the Solution/Project.
Right click the ASPX file to be opened.

Select Open with…
Choose HTML Design Editor.
Click Set as Default option.
Restart the Visual studio.

Double clicking an ASPX page will now open faster.

Remembering connection string is difficult.


No need to remember the connection string. Follow below steps:

Open notepad. Save as “AnyName.udl”.
Open the created UDL file.
This will pop up Database selector window. Choose the database you would like to connect and give proper credentials.

Test the connection.
Once success, Open the UDL file with notepad the connection string will be there.

Don’t know the namespace for a particular class in Visual studio 2005.


You know the class name but not sure in which namespace it resides? (Say for Example NameValueCollection)

Type the classname in the .cs file (code behind for the ASP.NET page).
Right click the class name.
From the context menu select Resolve. The namespace will be added in the using namespace lists.

Your child ASPX page is not getting refreshed properly.


You might have opened a child page using window.open or some other method and passed some data from the parent page to the child page using QueryString for example. Subsequently, the child page may not populate the data based on the values passed by parent page. This is because the data passed is cached in the child browser. This could be avoided by passing Random value (e.g., querystring = querystring + “&Random=” + Math.random()) as one of the argument in query string. This will refresh the child page each time.

Need to share values between parent and child page in JavaScript.


Sometimes you may load child pages (using iFrame or Tab) in parent page. The value can be shared easily between parent and child pages using global variables in parent page. The variable can be accessed in child page using:
parent.VariableName
                      

Open in new window


Blob values may be stored in SQL as byte array and you need the corresponding Text for that.


Use the query:
select convert (varchar (max), BlobFieldName) from TableName where [Condition]
                      

Open in new window


Need to find out the protocol correctly of your page.


The serverside approach (Request.Url) to find out the protocol in a website (HTTP or HTTPS) may be error prone on a production server. This could be resolved using Clientside code (JavaScript)   window.location.href will give the full path of the requested file. We could parse and get the protocol from the above value.

Need to toggle between ASPX page blocks in client side.


 // It will hide the controls specified in the id
                      document.getElementByID (‘id’).style.display = “none”; 
                      // It will show the controls specified in the id
                      document.getElementByID (‘id’).style.display = “block”;  
                      

Open in new window


Need to add a JavaScript element dynamically in an ASPX page.


function CreateElement (ele)
                      {
                         var e = document.createElement(ele);
                         e.[Attribute1] = [Value1];
                         e.[Attribute2] = [Value2];
                         // This can be changed wherever you like to append the created element 
                         document.getElementsByTagName("head")[0].appendChild(e); 
                      }
                      

Open in new window


Need to show print dialog without showing the content page to the user.

       

On a button click we need to show only the print dialog not the content page. This can be accomplished using iFrame in client side.

1. Have the ASPX page that having the content to be printed (e.g., PrintPage.aspx). This page should call window.print() in its client side to launch the print dialog.

2. Do include (in the parent ASPX page):
<ifame id=”frmPrint” src=”javascript:return false;”></iframe>
                      

Open in new window


3. In the onload event of Parent page hide the iFrame using:
document.getElementByID (‘frmPrint’).style.display = “none”;
                      

Open in new window


4. In the click event of the Print button write below code:
//Show the iFrame
                      document.getElementByID (‘frmPrint’).style.display = “block”; 
                      //Load the page to launch the print dialog
                      document.getElementByID (‘frmPrint’).src = “PrintPage.aspx”; 
                      //Hide the iFrame
                      document.getElementByID (‘frmPrint’).style.display = “none”; 
                      

Open in new window



The list can include much more. I’ll explain some in more details in forthcoming articles.

Thanks for reading!
4
4,026 Views
Easwaran ParamasivamSenior Software Engineer

Comments (1)

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Nice collection of tips.  It is always good to have other tricks in the arsenal.  You have my Yes vote above.

For connection strings, I use this nice reference also: http://www.connectionstrings.com/

I love resolution of types.  It is good even when you know the namespace of the types you are using, but like to type out the names sometimes and then use this technique to automatically add the using clauses.  Occasionally, you will run into multiple choices where a class uses a common name that belongs to multiple namespaces, but typically as developers we should know which one we intended.  What I find as a nice backup is the Object Browser where I can see that a particular implementation has the methods/properties I intended to use.

Anyway, nice job!

Regards,
Kevin

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.