Friday, 2 October 2015

Access the .NET Server Controls using javascript easily


  • There are many developers that are depend on intellisense provided by Microsoft Visual Studio.
  • But some times it may not work for scripting, though in VS 2012 and higher version it quite run much good.
  • So here are some of the JS code that I would like to share with developers for accessing the .NET controls and how to retrieve and set value to controls.
  • Get Label Value:
  • $('#<%=Label.ClientID%>').text();
  •  Set label value:
  • $('#<%=Label.ClientID%>').text("New Value");
  •  
  • Get Textbox value:
  • $('#<%=TextBox.ClientID%>').val();
  •   Set Textbox value:
  • $('#<%=TextBox.ClientID%>').val("New Value");
  •  Get Dropdown value:
  • $('#<%=DropDownList.ClientID%>').val();
  •  Set Dropdown value:
  • $('#<%=DropDownList.ClientID%>').val("New Value");
  •  Get text of selected item in dropdown:
  • $('#<%=DropDownList.ClientID%> option:selected').text();
  •  Get Checkbox Status:
  • $('#<%=CheckBox.ClientID%>').attr('checked');
  •  Check the Checkbox:
  • $('#<%=CheckBox.ClientID%>').attr('checked',true);
  •  Uncheck the Checkbox:
  • $('#<%=CheckBox.ClientID%>').attr('checked',false);
  • Get Radiobutton Status:
  • $('#<%=RadioButton.ClientID%>').attr('checked');
  •  Check the RadioButton:
  • $('#<%=RadioButton.ClientID%>').attr('checked',true);
  •  Uncheck the RadioButton:
  • $('#<%=RadioButton.ClientID%>').attr('checked',false);
  •  Disable any control:
  • $('#<%=TextBox.ClientID%>').attr('disabled', true);
  •  Enable any control:
  • $('#<%=TextBox.ClientID%>').attr('disabled', false);
  •  Make textbox read only:
  • $('#<%=TextBox.ClientID%>').attr('readonly', 'readonly');

  • For getting the data you may also use as what I have said in previous blog, that is simpler

No comments:

Post a Comment

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....