ASP.NET Client-Side Script

Description

ASP.NET Client-Side Script

Reviews
Shared by: Muhammad Ramzan
Stats
views:
131
rating:
not rated
reviews:
0
posted:
11/4/2009
language:
English
pages:
0
ASP.NET Client-Side Script FAQ (Various contributors) We have a fair amount of forum questions here on Eggheadcafe.com that center around the area of client-side scripting with ASP.NET, and we thought it would be about time to start an FAQ article on the subject. We'll include all the basics of client - side script with ASP.NET here, drawn from reliable sources (and simplified!) along with our own "discoveries". We'll also insert links to other resources where appropriate. This is a community "work in progress" which we will add to on a regular basis, so please feel free to take advantage of the little "article discussion forum" at the bottom to post or answer questions and make requests, as well as to make repeat visits to see "what's new" on the page! All contributions are encouraged, either via the discussion at the bottom of this page, or via email. Basic Concepts: There are two important component parts of any ASP.NET "page" - the ".ASPX" portion, which is really a modified HTML page that holds the asp:control server tags, client script, and HTML elements, and the "Codebehind" or server-side portion of the page, which can be either inside tags in the ASPX portion (or with a src= attribute), or it could be generated from server side processing, as we will soon see. ASP.NET Intrinsic Client-Script Related Methods ASP.NET has several built in methods of the Page class that were specifically designed to help with client-side scripting. Let's look at these first: RegisterClientScriptBlock and RegisterStartupScript methods of the Page class. These methods identify your script with a string key, so that multiple server control instances can request the script block without it being inserted in the output more than once. The IsClientScriptBlockRegistered and IsStartupScriptRegistered methods of the Page object are used to determine if the script has already been registered. Differences between RegisterClientScriptBlock and RegisterStartupScript The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's element. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's element. You need to include the Then in your Page_Load: private void Page_Load(object sender, System.EventArgs e) { System.Web.UI.HtmlControls.HtmlForm frm =(System.Web.UI.HtmlControls.HtmlForm)this.FindControl("Form1"); frm.Attributes.Add("onSubmit","AlwaysFireBeforeFormSubmit();"); } ValidateRequest Directive ASP.Net 1.1 will not allow html code to be sent to the server unless you use the page directive validateRequest = "false". This can also be set globally in web.config in the pages element: Add the "pages" line within the pre-existing tag: NOTE: If you use the above, your site may be vulnerable to cross-site scripting attacks, unless your code was already well-written to prevent that. GetPostBackClientEvent Method The GetPostbackClientEvent method returns a string containing the client-side script for the postback event of a given control: Parameters: control - the control that should be passed to the postback method on the client side as the source of the postback event. argument- the argument that will be passed to the postback method String = GetPostBackClientEvent(control, argument) GetPostBackClientHyperlink Method The GetPostBackClientHyperlink method returns a string representing a hyperlink to the client-side script call that initiates the postback process. Parameters: control - the control that should be passed to the postback method on the client side as the source of the postback event. argument- the argument that will be passed to the postback method This method allows developers to create hyperlinks that will cause the page to postback to the server. For example: ">Link 1 The above creates a link to the postback method associated with the control with id "button1", and passes in an argument of "submit". The GetPostBackEventReference Method Gets a reference to the client-side function that initiates the postback. When building web pages, it can be helpful to get a reference to the postback script function in order to programmatically cause the page to postback. This method will cause the page to postback, indicating that the specified control caused the postback. String = GetPostBackEventReference(control, [argument]) Parameters: control - the control that should be passed to the postback method on the client side as the source of the postback event. argument- the argument that will be passed to the postback method The RegisterArrayDeclaration Method This method creates a client-side script block that declares an array. In addition, you can add an element to the array. Subsequent calls to this method using the same array name add new elements to the same array. In this way, multiple controls in a page request can add themselves or elements of their content to this array. Void RegisterArrayDeclaration (arrayname, value) Parameters: arrayname (string)- the name to give to the client script array variable value (string) - the value to be added to the array. Can be the name of an object or control on the page. Example: RegisterArrayDeclaration("TextBoxes","TextBox1") RegisterArrayDeclaration("TextBoxes", "TextBox2") Client - side output of the above two calls would look like this: Causing a page to reload via server - side action: // cause page to reload from server-side Response object: Response.Write(""); // Cause page to repeatedly reload Response.Write("");

Related docs
asp.net validators
Views: 49  |  Downloads: 22
ASP.Net Bible
Views: 1530  |  Downloads: 197
ASP.net Web Developer's Guide
Views: 2021  |  Downloads: 298
Silver Light With Asp.net
Views: 19  |  Downloads: 2
Master Pages in ASP.NET 2.0
Views: 675  |  Downloads: 175
Tworzenie ASP.NET Web Form
Views: 458  |  Downloads: 8
Better Web Experience with ASP.NET
Views: 440  |  Downloads: 136
ASP.NET Web Forms
Views: 64  |  Downloads: 5
ASP.NET
Views: 537  |  Downloads: 28
premium docs
Other docs by Muhammad Ramza...
Muhammad Ramzan SSE
Views: 106  |  Downloads: 7
Setting and Changing Column Collations
Views: 97  |  Downloads: 0
Design Patterns - Slides
Views: 113  |  Downloads: 9
Data Set - The Good and Bad
Views: 46  |  Downloads: 0