About Me

Professional Practical HumanBeing
Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Thursday, December 23, 2010

The Ever-Useful $get and $find ASP.NET AJAX Shortcut Functions

$get

Overview

$get can be used as shorthand for the document.getElementById and element.getElementById functions. The $get shortcut function points to the Sys.UI.DomElement.getElementById JavaScript function which is defined as part of the ASP.NET AJAX client side library (which means you will need to include a ScriptManager on the page to be able to use it). $get accepts two parameters, the first is the ID of the DOM element you want to retrieve, the second is the parent element of where the search starts. The second parameter is optional and when it is not supplied defaults to the document element. Here is the official API reference.

Ex:- $get('divid')

On Master Page:-

Ex:- var div = $get('<%= this.label.ClientID %>');

$find

Overview

The $find shortcut function allows you to look up an ASP.NET AJAX client side Component by it's ID. Here is a link to the $find shortcut's documentation and below is the API description.

Use $find to Fetch an AjaxControlToolkit Extender Behavior

Many of the extender controls of the AjaxControlToolkit include a client side JavaScript API that allows your page to interact with the extender control from the client. While the documentation on the toolkit is well above average, it focuses on the most common properties/attributes of the control and skims over the other less used properties/attributes as well as the client side API. I have found the best way to learn about the client side API is to download the toolkit and browse the code.

Once you discover the capabilities of an extender's the client side API, you are going to need to obtain a reference to the component so you can interact with it from your page. All of the extender controls in the toolkit expose a property called BehaviorID (it is defined on the ExtenderControlBase so all toolkit controls inherit it by default). You can set the BehaviorID in the controls markup, or you can leave it blank, in which case it will have the same ID of the control. The 2 code samples below show both of these options. In the first one, no BehaviorID is specified so this attribute takes on the same value as the ID. In the second sample, I am explicitly specifying a BehaviorID of 'rceBehaviorID', so that is what I can use to look up the Component.

The behavior for this extender can be fetched using the following syntax:

var behavior = $find('rce1ID');

ajaxToolkit:ResizableControlExtender ID="rce1ID" runat="server" ... />

The behavior for this extender can be fetched using the following syntax:

var behavior = $find('rceBehaviorID');

ajaxToolkit:ResizableControlExtender ID="rce2ID" BehaviorID="rceBehaviorID" runat="server" ./>



Wednesday, November 17, 2010

Limiting the Number of Characters in a MultiLine TextBox

for TextBox onkeypress="javascript:CheckLengthStatus(this);" if 16000 is the max limit

function CheckLengthStatus(X) {
try {
var length = document.getElementById(X.id).value.length

if (length < 16000)
{ }
else
{ window.event.keyCode = 0; }
}
catch (e) { }
}

Monday, November 15, 2010

Manipulate Validator controls and Validation Group from JavaScript

Scenario 1:-
•To enable Validator from JavaScript
ValidatorEnable('Validator Client ID', true);

•To disable Validator from JavaScript
ValidatorEnable('Validator Client ID', false);

•To Validate all the Validator control from JavaScript
Page_ClientValidate();

This function will return true if there is no validation error otherwise it will return false.

•To Validate a Specific Validator Group from JavaScript
Page_ClientValidate('ValidationGroupName')

Example :-
OnClientClick="javascript:return SavePunchoutCommercial();"

function SavePunchoutCommercial() {
if (typeof (Page_ClientValidate) != 'function' Page_ClientValidate(ValidationGroup)) { }
}

So, this way we can manipulate validator controls and validation group from JavaScript. I hope this will help you out.


Scenario 2:-

Put this code for Button named “Done”

OnClientClick=”EnableDisableValidation();”

javascript function EnableDisableValidation() looks like below.

function EnableDisableValidation()
{
if($L(’rdbMyAddress’).checked)
{
return Page_ClientValidate(’Address’);
}
else if($L(’rdbAddressGift’).checked)
{
if( Page_ClientValidate())
{ return true;}
else
{ return false;}
}
}

Where $L is replacement of document.getElementById(”);

Page_ClientValidate(’Address’) function Validates All Control having property ValidationGroup =”Address”.

Where as Page_ClientValidate() Blindly validates all validation Controls within page.

Source :-
http://blog.zoomasp.net/?p=26
http://programminghack.wordpress.com/2008/09/25/manipulate-validator-controls-validation-group-from-javascript/


Sunday, November 14, 2010

Add JavaScript programmatically using RegisterStartupScript during an Asynchronous postback

Scenario 1:
Create an ASP.NET project. Now add a Label(lblDisplayDate) and a Button(btnPostback) control to the page.
On the Button click, register the following script dynamically using the ClientScriptManager.RegisterStartupScript to change the color of the label controls to Red.
C#
protected void Page_Load(object sender, EventArgs e)
{
lblDisplayDate.Text = System.DateTime.Now.ToString("T");
}
protected void btnPostback_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Include Script Tag in anchor
sb.Append(@"script language='javascript'");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"script");
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
}
}

Scenario 2:
Now wrap the label and the button control inside an UpdatePanel as shown below:
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblDisplayDate" runat="server" Text="Label">asp:Label>
<asp:Button ID="btnPostback" runat="server" onclick="btnPostback_Click"
Text="ClickMe" />
ContentTemplate>
asp:UpdatePanel>
div>
When you now run the application and click on the button, you will observe that the label is updated with the new time (which means the postback occurred), however the color of the label does not turn to red. This is because when using an Update Panel, the JavaScript that is dynamically added to the page using ClientScript.RegisterStartupScript() does not execute.
Solution:
Use the ScriptManager.RegisterStartupScript(). If you take a look at the methods of the ScriptManager class, you will observe that the methods to register client script to the page using the ClientScriptManager class, are also present in the ScriptManager class. So modify the code as shown below:
C#
protected void btnPostback_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Include Script Tag in anchor
sb.Append(@"script language='javascript'");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"script");
ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);
}
Note 1: Observe that the last parameter to RegisterStartupScript is set to 'false'. This prevents us from inserting the 'script' tags automatically. Since we have already inserted the 'script' tags while creating the script in the StringBuilder, we do not need to insert them now. Setting the last parameter to 'true' in such cases is a very common mistake developers do.
Note 2: Building up javascript using the StringBuilder as shown above is not advisable in large projects. Instead you should keep the javascript in a seperate .js file and refer the .js.
Source :-