JavaScript in ASP.NET

ASP.NET provides a powerful server-based programming model with its PostBack architecture that allows performing all the work on the server while ensuring security and compatibility. The most significant weakness of postback architecture is its. It is impossible to react to mouse movements or key presses on the server efficiently. To overcome this weakness, developers use. This client-side script allows developers to react user events without submitting to the server.JavaScript is embedded directly into an HTML web page. The code is downloaded to the client computer and executed by the browser. The most straightforward approach for embedding small amounts of JavaScript code is adding directly to an event attribute for an HTML element. If you want to run the code automatically when the page loads, or react to a client-side event, you can add tag that contains the script code. Usually, developers define a function in a block and then attach the function to a client-side event using an event attribute.

<script language=”javascript”>
   Function alertOnClick(){ alert(“Clicked!”);}
</script>
 

Protected void Page_Load(object sender, System.EventArgs e)
{
   button1.Attributes.Add(“onClick”,”alertOnClick();”)
}

Whether you use event attributes, script blocks or both, you may directly add static JavaScript code to the .aspx page or you may embed code by using the methods of the Page class.The code above adds the onClick attribute to the button1 control. When the user click on the button, the event occurs and the JavaScript alert() function is called and “Clicked” message is displayed. Usually, you have to insert JavaScript by adding attributes to a control with one exception. In button (button,linkbutton ,imagebutton) controls’ clicks, you can use OnClientClick property.

<asp:button id=”btnClick” runat=”server”
     OnClientClick=”return confirm(‘Sure?’);”
     text=”Click Me”/>

The button click still post back the page but before posting back, client side confirmation prompt is displayed. Common approach for a large amount of code is to place a JavaScript function in a block and then call that function using an event attribute. This approach is even more practical, if you need to use the same code for several times. The script blocks can be embedded anywhere in the header or the body of an HTML document, and a single document can have any number of script blocks. However, if you need to call a function, that function must be defined in a script block before the event attribute that calls it. Otherwise, it is better to place scripts to the end of the document. Progressive rendering is blocked until all JavaScript have been downloaded. Scripts cause progressive rendering to stop for all content below the script until it is fully loaded.

Advertisements

8 Comments Add yours

  1. yakoob reddy says:

    sir,

    sir i am unable to install sqlserver on my system(xp2),while it installing i get only some tools in the installing window then its completion i saw only confuguration tools i cannot get management studio,please tell me it requires any troubleshotting.

    thankingu sir
    G.yakoob reddy

    Like

  2. sudhakar says:

    for interview i will follow u r note,sir is it sufficient.if not suffissient please tell me which writer books is best for begineer.thanks.

    Like

  3. D sarma says:

    sir,
    for interview i will follow u r note,sir is it sufficient.if not suffissient please tell me which writer books is best for begineer.thanks.

    Like

  4. Kalyani K. says:

    hi sir,

    I am very much thankful to you…..I have got the answer for my question which I placed the query in your blog regarding the IIS.

    I am sure to tell is nobody can teach the subject like you sir….

    I am your C# and ASP.Net morning batch student

    Like

  5. Kalyani K. says:

    Sir,

    I am not able to see Internet Information Services in my system. I am having the XP as the O/S. I am not getting the welcome page also when I give the computer name fallowed by http in the IE.

    please give me the clarification in this matter.
    yours C# and ASP.NET student morning batches

    Like

  6. ramu says:

    Nice to Know new things from u r side

    Like

  7. bindu says:

    hello sir…

    please send FAQ’s
    and also a small faq..
    –Page directives in Asp.Net?? please give me the list

    thank u sir……

    Like

  8. kunalkumar says:

    Dear Sir,
    I have Created a Javascript Calendar UserControl which I want to post on your site. Its a usefull one.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s