Calling client side scripts using in server controls.

It is a common question in ASP.net forums about how to call the VB Script or Java script in server controls so i thought to publish an example here.

In normal html tag we used to call the scripts like
<input type ="button" name="txt" onclick="Alert('This is a test');" />


But in ASP.Net server controls we do not have the design mode attribute to add client side scripting like this.

<asp:Button ID="Button1" runat="server" Text="Button" />


In order to add the onClick event in this text box , attribtes need to add in code behind.

Syntax for this is:

Servercontrol.Attributes.Add("key","value");
Example,


Code:
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Attributes.Add("onClick", "Alert('This is a test');");
    End Sub


It is also possible to add any number of attributes to server control such as style etc...


Please post your comments here if you have any.

Commentaires

Posts les plus consultés de ce blog

XAJAX with PHP – The future of web development

XAJAX with PHP – The future of web development

Database connection pooling in ADO.Net