Tag Archives: JavaScript

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

Advertisements

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


Preload images in HTML using JavaScript

Preloading means loading an object (graphics, text, movie) before showing it to output.

A good example is of  preloading in flash files, when you see a loader before displaying the content, and after loading site shows a smooth experience of flow of data.

This is mostly used when there is an mouser over images to be loaded. If user mouse overs an image and then you loads the new mouse-over image it may take a few seconds to load, which leads to a small blank image over the image area. By pre-loading the image you can show mouseover image instantly without any delay.

In this post I am explaining how to do preaload imags in HTML using JavaScript, without using flash.

We have to use the Head tag of html as this is the one which loads on first page executiution.

<head>
<script type=”text/JavaScript”>

if (document.images)
{
pic1= new Image(190,53);
pic1.src=”images/productSmall.gif”;

pic2= new Image(190,53);
pic2.src=”images/storeSmall.gif”;
}

</script>
</head>

The above code lods two images naming productSmall.gif and storeSmall.gif respictively from images directory. It creates a new object of image and assigns it a dimention and a file to load.

The document.imags condition has been used to determine that the browser supports the images or not.


Show Message Box in Asp.net

It’s not possible to show an alert message box in asp.net using server side code (like vb.net windows apps, msgbox).

But this can be achieved by generating a client side Java Script code in a server-side event.

Code

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
showMsg(“hey there”, Me)
End Sub

Public Sub showMsg(ByVal sMsg As String, ByVal frm As Web.UI.Page)

Dim sb As New System.Text.StringBuilder
Dim oFormObject As System.Web.UI.Control

sMsg = sMsg.Replace(“‘”, “\'”)
sMsg = sMsg.Replace(Chr(34), “\” & Chr(34))
sMsg = sMsg.Replace(vbCrLf, “\n”)
sMsg = “<script language=javascript>alert(“”” & sMsg & “””)</script>”

sb = New System.Text.StringBuilder
sb.Append(sMsg)

For Each oFormObject In frm.Controls
If TypeOf oFormObject Is HtmlForm Then
Exit For
End If
Next
oFormObject.Controls.AddAt(oFormObject.Controls.Count, New LiteralControl(sb.ToString()))
End Sub

Explanation:

On the button click event i write a JavaScript method to the page, the JavaScript method is displaying the message your pass to the method  showMsg().

Note:
Please don’t pass special characters as the message, as they will conflict with the special characters of JavaScript and message won’t come properly.

For more articles updates and sourcecodes please visit dotnetcoderoom


Show default Text in Email TextField

Show default Text in Email Field.
Like on form load show “enter email id” in email field.
On click clear this text.
on mouse out with nothing entered show again this into textbox.

Code:
<body><input type=”text” name=”email” value=”Your e-mail adress” onfocus=”if(this.value==’Your e-mail adress’) this.value=”” onblur=”if(this.value==” ) this.value=’Your e-mail adress’;” />
**End of article**

For more articles updates and sourcecodes please visit dotnetcoderoom