Monday, November 17, 2008

Javascript and Asp.net

1) Access Asp.net controls via Javascript:

-Javascript in .aspx page


document.getElementById('<%=NameOfControl.ClientID%>').PropertyName


-Javascript in .js file

First, you have to get the id of the control after the page is rendered (IE/FireFox->View->Page Source->Find the the control id)

document.getElementById('ctl00$ContentPlaceHolder1$txtFullName').focus();


2) Add Javascript function to asp.net controls:

NameOfControl.Attributes.Add("eventname","javascriptMethod") [vb]

NameOfControl.Attributes.Add("eventname","javascriptMethod") [C#]


Example: Validate empty value in a textbox.

-In aspx page add javascript function as following

<script type="javascript">
function IsEmpty()
{
if (document.getElementById('<%=textbox1.ClientID%>').value == "")
{
alert("Textbox is empty");
}
}
</script>


-In code behind, Add Following code on page_load

btnSubmit.Attributes.Add("onclick","IsEmpty()");

3)

Validators on update panel not working properly

The problem is occurred seriously on window 2003. Error messages of validators is not displayed properly.

-Simulate the problems:

1. Create a sample asp.net ajax application.
2. Drag a textbox, a requirefieldvalidator and a button to an update panel.
3. Deploy it to web server running window 2003.
4. Leave the value in the textbox empty, then click submit button. It work fine in this stage.
5 Enter some text and click submit. At this time, server is doing asynchronous post back to the server. Finally try to leave the value empty again and click submit. You will not see the error message of validator controls.

- Solution:
1. There is a work around to solve this problem by setting EnableClientScript = False of every validator controls under update panel. This will disable client validatation, so instead we need to do validation on server.

if(Page.IsValid)
{ //do actioin
}

2. Another solution from Matt Gibbs's blog:

http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx

It works fine most of the case,but it has a problem when setting "DisplayMode" of validation controls to "Dynamic". Validation is not fired unless the page submit two times.

Wednesday, November 5, 2008

65 Excellent jQuery Resources (tutorials,cheat sheets,ebooks,demos,plugins…)

http://speckyboy.com/2008/04/02/65-excellent-jquery-resources-tutorialscheat-sheetsebooksdemosplugins/