Client side scripting using javascript


ASP.NET is Web Based Application and provides many server controls for the process of development. this articles gives the details about how to inject the client side scripts using server side controls. which helps to reduce the round trips. Learn Client side scripting using javascript

An overview of Client side scripting using javascript


ASP.NET is Web Based Application and provides many server controls for the process of development. Whenever server control event is fired, PostBack occurs.
With the increasing no of postbacks, the page performance will slow down, in order to overcome this drawback. We can use client-side operaton s with the server-side controls and reduce the number of round trips.

There are number of ways where in client-side code can be used for asp.net server controls

Client side HTML Attributes: Client side HTML attributes provide a way to tie a client-side event with a piece of client-side script


<form id="form1" runat="server">
<script language="javascript">
<!--
function doClick() {
alert("Welcome to DotNetSpider");

}
//-->
</script>
<input type="button" onclick="doClick()" value="click Me" />
</form>


The page contains the HTML input button that has its onclick attribute wired up to the doClick() function whenever button is clicked client-side code in the doClick() function will be executed.
Note: HTML comments() are because old browsers will display the contents of <script> as block if they are not used.

*Client side scripts RegisterClientScriptBlock(key,script)



protected void Page_Load(object sender, EventArgs e)
{

StringBuilder str=new StringBuilder();
str.Append("<script language=JavaScript> function DoClick() {");
str.Append("alert('Welcome to DotNetSpider' )}<");
str.Append("/");
str.Append("script>");



if (!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript", str.ToString());

}

<form id="myForm" runat="server">
<input type="button" value="ClickMe" onclick="DoClick()">
</form>



RegisterClientScriptBlock Registers a client side javascript code on the dynamically.
When clickme button is clicked the javascript will be executed.

*calling the .js file using RegisterClientScriptInclude


Create the javascript file messages.js
function doClick() {
alert("Welcome to DotNetSpider");

}
Function welcome()
{
alert("Hello……");
}

Page.aspx

<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</div>
</form>
</body>


Page.cs

public void Page_Load(Object sender, EventArgs e)
{
// Define the name, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/messages.js";
Type cstype = this.GetType();

// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;

// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
}

}



RegisterClientScriptInclude- RegisterClientScriptInclude method takes key and url parameters to identify the script, as well as a type parameter to specify the identification of the client script include


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: