latest Post

Using jQuery with ASP.NET



1
   

<script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>


   

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>


Show alert window on click of ASP.NET Button.


Assuming a ASP.NET button with ID "btnSubmit " is placed on the page.


1
   

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

And now bind the click event to ASP.NET Button in document.ready section.

1
   

<script type="text/javascript">

2
   

  $(document).ready(function() {

3
   

    $("#btnSubmit").click(function() {

4
   

         alert("Alert using jQuery");

5
   

    });

6
   

  });

7
   

</script>

In above jQuery code block, we have attached click event to the button using ID selectors.


Below is a list of useful jQuery code example for ASP.NET controls that we use on daily basis. One thing, while creating object of any ASP.NET control, always use ClientID. As when Master pages are used then the ID of the ASP.NET controls is changed at run time.  But with ASP.NET 4.0, this is changed and now you have control over the Client ID


Get label value:

1
   

$('#<%=Label1.ClientID%>').text();

Set label value:

1
   

$('#<%=Label1.ClientID%>').text("New Value");

Get Textbox value:

1
   

$('#<%=TextBox1.ClientID%>').val();

Set Textbox value:

1
   

$('#<%=TextBox1.ClientID%>').val("New Value");

Get Dropdown value:

1
   

$('#<%=DropDownList1.ClientID%>').val();

Set Dropdown value:

1
   

$('#<%=DropDownList1.ClientID%>').val("New Value");

Get text of selected item in dropdown:

1
   

$('#<%=DropDownList1.ClientID%> option:selected').text();

Get Checkbox Status:

1
   

$('#<%=CheckBox1.ClientID%>').attr('checked');

Check the Checkbox:

1
   

$('#<%=CheckBox1.ClientID%>').attr('checked',true);

Uncheck the Checkbox:

1
   

$('#<%=CheckBox1.ClientID%>').attr('checked',false);

Get Radiobutton Status:

1
   

$('#<%=RadioButton1.ClientID%>').attr('checked');

Check the RadioButton:

1
   

$('#<%=RadioButton1.ClientID%>').attr('checked',true);

Uncheck the RadioButton:

1
   

$('#<%=RadioButton1.ClientID%>').attr('checked',false);

Disable any control:

1
   

$('#<%=TextBox1.ClientID%>').attr('disabled', true);

Enable any control:

1
   

$('#<%=TextBox1.ClientID%>').attr('disabled', false);

Make textbox read only:

1
   

$('#<%=TextBox1.ClientID%>').attr('readonly', 'readonly');

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment