Category Archives: Asp.Net

Debugging JavaScript in asp.net User Controls

Little while back I was facing issue is debugging JavaScript code written inside an asp.net User Control (ascx) file in visual studio 2010.

Interesting thing is that breakpoints doesn’t work for User Controls in asp.net but you could still do the debugging if you write debugger; keyword before the line where you want to set the breakpoint. The visual studio will automatically detect this keyword in your code and would pause the execution at that line in debug mode.

But in my case even the debugger keyword was not working. Whenever visual studio designer used to encounter this keyword, VS used to halt at the first line of page’s markup (pagename.asps[dynamic]), and by this I could not find that exactly which of my debugger statement got hit.

The issue seems to be of the page’s temporary file/markup generated by VS.

Solution

deleted the website’s .pdb file from project’s bin folder and things started working.

Hope this information would help someone other there in coding world.

Thanks


Visual Studio 2010 does not recognize asp tags

Visual Studio 2010 suddenly stopped recognizing asp tags in asp.net webpages. I don’t know the exact reason behind this behavior but I think it could be because of IE update (from 9 to 10), installing VS 2012 parallelly on same machine, or corrupted VS data files.

My development machine’s environment was

  • Win7 Professional
  • VS 2010 Professional
  • VS 2010 Professional SP1
  • VS 2012 Ultimate
  • SQL Server 2008 Express Management  Tools

I tried below mentioned workaround to fix the issue but no effect

  • Uninstalling the VS 2012 Ultimate
  • Revert back IE from 10 to 9
  • Repairing VS 2010 Professional SP1

Solution

Finally, deleting the C:\Users\UserName\AppData\Roaming\Microsoft\VisualStudio\10.0 folder resolved the issue.

Note: Replace the UserName with your logged-in username in above string.


Run JavaScript after page is loaded

If you need to call a JavaScript code when the html page is completely loaded, you can use JQuery.

At times some controls are not rendered and the JavaScript code runs, hence it is  not able to find controls which is not rendered yet and client side code throws exception.

Use $(document).ready feature of JQuery as belows:

<script language=”javascript” type=”text/javascript”>

$(document).ready(
function()
{
showMsg();
}
);

function showMsg()
{
// your code here
}

</script>

The above is also a replacement of the old body onload property: <body onload=”javascript showMsg()”> thing


Generic function to display images on webpage asp.net

Hi fellas,

I am writing this post after a long time. This new year I am back at blogging to help the software developers in writing better code .

Ever need of a generic/common function to display the images on website?
In normal practice, whenever we need to show an image, we used to directly write
Image.Src= “/userfiles/abc.jpg”;

This can be painful sometimes if your folder path gets changed,  in that case you need to replace the folder (userfiles) everywhere.

Smart and better way is to create a common function that will display images. What you need to do is to call this method every time.


Method:

public string ShowImage(string ImageName)
{
string ReturnVal = string.Empty;

if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(“~/userfiles/” + ImageName)) == true)
{
ReturnVal = VirtualPathUtility.ToAbsolute(“~/userfiles/” + ImageName);
}
else
{
ReturnVal = VirtualPathUtility.ToAbsolute(“~/Images/ImageNotAvailable.jpg”);
}

return ReturnVal;
}


Usage:

imgThumbnail.ImageUrl = ShowImage(“abc.jpg”);


Explanation:

This method, takes a string parameter as image name to display. Then it checks that if the particular image exists or not.
If the image exists on the specified path then it returns the image name with full path (userfiles/abc.jpg) else it returns the alternate image path, which is imageNotAvailable.jpg.

This way if the image is not founded on server then user will be shown a imageNotAvailable.jpg

Thats it for now.


2010 in review

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads This blog is doing awesome!.

Crunchy numbers

Featured image

The average container ship can carry about 4,500 containers. This blog was viewed about 17,000 times in 2010. If each view were a shipping container, your blog would have filled about 4 fully loaded ships.

 

In 2010, there was 1 new post, growing the total archive of this blog to 34 posts.

The busiest day of the year was August 30th with 97 views. The most popular post that day was Username Regex Validation.

Where did they come from?

The top referring sites in 2010 were forums.asp.net, google.co.in, stackoverflow.com, google.com, and velocityreviews.com.

Some visitors came searching, mostly for mssql rename column, operator ‘+’ cannot be applied to operands of type ‘string’ and ‘method group’, username regex, ms sql rename column, and rename column mssql.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

Username Regex Validation October 2008

2

Operator + cannot be applied to operands of type string and method group, C# error September 2008
4 comments

3

Rename a table Column in MS SQL September 2008

4

ASp.Net 2.0 GridView Delete Button Confirmation pop-up August 2008
3 comments

5

Rotate a Label Text August 2008
1 comment


URL rewriting using Global.asax, Error: The resource cannot be found.

Today I implemented my first application having URL rewriting enabled.
The application is created in Asp.net, C# and MS Sql 2005.

The url rewriting logic was working on the my localhost but it didn’t worked on production server.

I was getting an error: The resource cannot be found. while accessing the newly created URL pages.

I searched on Google about this and read lot, but nothing worked.

Finally I re-published the website and again uploaded all the files to production server and now everything worked like magic. Here is the URL: http://theplayclan.com/blog

Somewhere I read to check the permissions on server’s IIS, this method is explained below:

1) Open IIS manager.
2) Right click on your website’s virtual directory, select Properties.
3) Now, click on Configuration (in front of execute permissions option).
4) In the new popped-up window a list comes up as Application Extensions.
5) select .aspx option and click on edit.
6) Make sure that the Verify That File Exists is unchecked. This option explicitly check for the existence of file.
7) Save the settings and try again.

Somewhere it was mention to remove the *.* and regular expression extension mappings from the IIS may also work. The procedure of this is mentioned above.


Traverse / Loop through all form controls in asp.net

This post explains that how to traverse (loop through) all form controls in asp.net page.

foreach (System.Web.UI.Control ctrl in this.form1.Controls)
{
//here your code
}

Further if you want to get the state or data or manipulate the control, use the below code:
Here I am checking that at least one DropDownList item is selected out of all DropDownLists in the current webform.

bool isSelected = false;

foreach (System.Web.UI.Control ctrl in this.form1.Controls)
{

if (ctrl.GetType() == typeof(System.Web.UI.WebControls.DropDownList))
{
DropDownList ddl = (DropDownList)ctrl;

if (ddl.SelectedItem.Text != "")
{
isSelected = true;
break;
}
}

}

if (isSelected == true)
{
Response.Write("selected");
}
else
{
Response.Write("none selected");
}