Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Wednesday, June 24, 2009

Disabling an ASP.Net Validators through Javascript

If you want disable the asp.net validator through javascript then use below code.

function doSomething()
{
var myVal = document.getElementById('myValidatorClientID');
ValidatorEnable(myVal, false);
}

Monday, April 20, 2009

Using body onload with ASP.net MasterPages

we want to use body onload tag in master pages but it will have effect other content pages.The onload function is for only one content page.for this purpose we have used below code in master page.
Master Page :
<script language="javascript">
function masterpage_onload()
{
if(window.content_onload != null)
window.content_onload();
//this condition check whether the rendered page has function are not.

}
</script>

Content Page :

<script language="javascript">
function content_onload()
{
//do something
}
</script>

Friday, March 13, 2009

Disable a button control during postback.

This example is a non-AJAX solution.

The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive:

<asp:Button runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Submitting...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Submit Me!" />


OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior.

Wednesday, December 17, 2008

Check a value in a string using Javascript

In .net if we want to check a value whether it is existing in given string
we have contains function which return true or false.
But in javascript we dont have function like this.But we can implement this
by using the function indexOf()

All you need to do is check whether the return value is -1 or not. A return value of -1 implies that the specific letter is not present in the given string.

function checkString()
{
if(str.indexOf("http://")==-1)
{
alert("Value Not Found");
}
else
{
alert("Value Found");

}
}

Tuesday, November 4, 2008

Fire a button click from any text box when enter key is pressed

If you have more than one button on a form ASP.Net doesn't know which button should be the default button.

With this code you may trigger any button click from any text box when the enter key is pressed.

TextBox.Attributes.Add("onkeydown", "javascript:if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)){document.getElementById('" & Button.ClientID & "').click();return false;}else return true;")

other way is if you are using panels then you can use the property
"Default Button".

Thursday, October 23, 2008

Detecting Browser Pop up blocker.

Below is the javascript code to detect the popup blocker of a browser
<script language="javascript">
function popupblocker()
{

var sOption="toolbar=no,location=no,directories=no,menubar=no,";
sOption+="scrollbars=yes,width=500,height=500,left =200,top=50";
var i=window.open("Somepage.aspx","",sOption);

if(!i)
alert("Your browser Popup is on,Please turn off to open the child window.")


}

</script>

call this function on body load.

<body onload="popupblocker()">
</body>