c# barcode maker Lesson 2: Creating Client Scripts with the Microsoft AJAX Library in C#.NET

Generator Quick Response Code in C#.NET Lesson 2: Creating Client Scripts with the Microsoft AJAX Library

Lesson 2: Creating Client Scripts with the Microsoft AJAX Library
Make QR Code ISO/IEC18004 In C#
Using Barcode generation for .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
www.OnBarcode.com
Decoding QR Code JIS X 0510 In Visual C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
ChAPTER 9
Bar Code Generator In C#.NET
Using Barcode maker for .NET Control to generate, create barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Bar Code Scanner In C#
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Notice that this markup is inside an <asp:Content/> tag, indicating that this page uses a master page. In the case of master pages, your control IDs are reset by ASP.NET to include the ContentPlaceholderID content area, followed by an underscore and the control name. Therefore, to use JavaScript to reference the TextBoxPassword control in this example, you would use the MainContent_TextBoxPassword as the ID. This markup also includes a CheckBox control that is set to automatically post back when the user selects the check box (AutoPostBack= true ). Therefore, you need to handle its CheckedChanged event in your code. Here you can check to see if the check box is selected and if so, register client script to verify the password. The following code listing shows an example.
Painting Denso QR Bar Code In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
QR Code 2d Barcode Generation In .NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
www.OnBarcode.com
Sample of Visual Basic Code Partial Class PasswordCheckTestVb Inherits System.Web.UI.Page Protected Sub CheckBoxPassStrengthOn_CheckedChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles CheckBoxPassStrengthOn.CheckedChanged If CheckBoxPassStrengthOn.Checked Then Dim passFunc As New StringBuilder() passFunc.Append("function CheckPassword() {") passFunc.Append("var passLen = document.forms[0].MainContent_ TextBoxPassword.value.length;") passFunc.Append(" if (passLen < 4) {") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("innerText = ""weak"";") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("style.color = ""red"";}") passFunc.Append(" else if (passLen < 6) {") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("innerText = ""medium"";") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("style.color = ""blue"";}") passFunc.Append(" else if (passLen > 9) {") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("innerText = ""strong"";") passFunc.Append(" document.getElementById(""passwordStrength"").") passFunc.Append("style.color = ""green"";}}") 'register the script Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), _ "CheckPasswordScript", passFunc.ToString(), True) 'add an event to the text box to call the script TextBoxPassword.Attributes.Add("onkeyup", "CheckPassword()") Else 'remove the event from the text box TextBoxPassword.Attributes.Remove("onkeyup") End If End Sub End Class
Quick Response Code Creation In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
www.OnBarcode.com
PDF-417 2d Barcode Creator In C#
Using Barcode maker for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
ChAPTER 9
Encoding UCC - 12 In C#.NET
Using Barcode encoder for VS .NET Control to generate, create GS1 128 image in VS .NET applications.
www.OnBarcode.com
Code-128 Creation In C#.NET
Using Barcode printer for .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Working with Client-Side Scripting, AJAX, and jQuery
Code39 Creator In Visual C#
Using Barcode encoder for .NET Control to generate, create Code 39 Full ASCII image in VS .NET applications.
www.OnBarcode.com
Delivery Point Barcode (DPBC) Generator In C#
Using Barcode generator for VS .NET Control to generate, create Postnet image in VS .NET applications.
www.OnBarcode.com
Sample of C# Code public partial class PasswordCheckTest : System.Web.UI.Page { protected void CheckBoxPassStrengthOn_CheckedChanged(object sender, EventArgs e) { if (CheckBoxPassStrengthOn.Checked) { System.Text.StringBuilder passFunc = new System.Text.StringBuilder(); passFunc.Append("function CheckPassword() {"); passFunc.Append( @"var passLen = document.forms[0].MainContent_ TextBoxPassword.value.length;"); passFunc.Append(@" if (passLen < 4) {"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"innerText = ""weak"";"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"style.color = ""red"";}"); passFunc.Append(@" else if (passLen < 6) {"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"innerText = ""medium"";"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"style.color = ""blue"";}"); passFunc.Append(@" else if (passLen > 9) {"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"innerText = ""strong"";"); passFunc.Append(@" document.getElementById(""passwordStrength"")."); passFunc.Append(@"style.color = ""green"";}}"); //register the script Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CheckPasswordScript", passFunc.ToString(), true); //add an event to the text box to call the script TextBoxPassword.Attributes.Add("onkeyup", "CheckPassword()"); } else { //remove the event from the text box TextBoxPassword.Attributes.Remove("onkeyup"); } } }
QR Code Generator In Visual Studio .NET
Using Barcode creator for Reporting Service Control to generate, create Quick Response Code image in Reporting Service applications.
www.OnBarcode.com
GTIN - 12 Printer In Java
Using Barcode generation for Eclipse BIRT Control to generate, create UPCA image in BIRT reports applications.
www.OnBarcode.com
When you run the page, users have no indication of their password strength. However, if they select the check box, the page posts to the server and a client script is added to the page as a response to the postback. Users can now verify their password strength, as shown in Figure 9-5. Also, notice in the preceding code listing that the event handler is removed from the TextBox control if the check box is not selected. This is necessary to avoid encountering an error as the script is removed after a postback (unless it is added back in).
Print USS Code 128 In Objective-C
Using Barcode drawer for iPad Control to generate, create ANSI/AIM Code 128 image in iPad applications.
www.OnBarcode.com
Painting Code 39 Extended In Objective-C
Using Barcode drawer for iPhone Control to generate, create Code 39 Full ASCII image in iPhone applications.
www.OnBarcode.com
Lesson 2: Creating Client Scripts with the Microsoft AJAX Library
Encode UCC-128 In Objective-C
Using Barcode maker for iPad Control to generate, create UCC.EAN - 128 image in iPad applications.
www.OnBarcode.com
Create Barcode In Visual Basic .NET
Using Barcode encoder for .NET framework Control to generate, create bar code image in Visual Studio .NET applications.
www.OnBarcode.com
ChAPTER 9
Making Data Matrix ECC200 In Objective-C
Using Barcode creator for iPad Control to generate, create ECC200 image in iPad applications.
www.OnBarcode.com
Drawing Bar Code In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
www.OnBarcode.com
FigURE 9-5 Dynamically adding JavaScript to a page by using RegisterClientScriptBlock.
You can also register client scripts to be executed only when the page is submitted. This allows the application to know when the user has caused a page submission. The page can then use your script code to validate the submission and cancel it if necessary. To register a client script to be executed only on page submission, you use the RegisterOnSubmitStatement method of the ClientScriptManager. This method works in a fashion similar to the operation of RegisterClientScriptBlock. It takes a type, a key, and the script as parameters.
Registering Client Scripts with the ScriptManager Control
The ScriptManager control, as discussed in the previous lesson, is used by the ASP.NET AJAX Extensions server controls. It automatically registers the appropriate script files defined by the Microsoft AJAX Library. If you are using a ScriptManager control, you can also use this library in your scripts. (This is discussed in greater detail later in the lesson.) In addition, you can use the ScriptManager control to register your own scripts with the page. You can do so declaratively or programmatically. To add a script to the ScriptManager declaratively, you use the <Scripts> collection element of the ScriptManager control. The following markup shows an example.
<asp:ScriptManager ID="ScriptManager1" runat="server"> <Scripts> <asp:ScriptReference Name="AppScripts.js" /> </Scripts> </asp:ScriptManager>
Note that if your JavaScript file is embedded in an assembly, you can add the Assembly attribute to the ScriptReference tag and point to the DLL file. This is useful when you do not have full source code files or are working with precompiled objects.
Copyright © OnBarcode.com . All rights reserved.