1
Answer

How to make enable and disable for controls in MVC 5 ?

Photo of Asp.Net Hein

Asp.Net Hein

8y
682
1
I want to control my 'Search' button to be enable and disable in MVC.
 
I have two textbox - ( fromdate and todate ). If there is no value in these text textbox, I want to make the button 'Search' to be disable. If there are values in these two textbox, I want to make the button 'Search' to be enable. 
 
If anyone have idea to know how to implement, kindly let me know. Thanks a lot for viewing my question. 

Answers (1)

1
Photo of Vinay Singh
NA 5.9k 126.2k 8y
Hi
you can use jQuery code to perform the operation
Js Function
  1. <script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
  2. <script language="javascript">
  3. function EnableButton(){
  4. $('#Button1').prop("disabled",false);
  5. }
  6. function DisableButton(){
  7. $('#Button1').prop("disabled",true);
  8. }
  9. function ValidateContent()
  10. {
  11. if($('#txtFromDate').attr('value')=="" && $('#txtToDate').attr('value')=="")
  12. {
  13. DisableButton();
  14. }
  15. else
  16. {
  17. EnableButton();
  18. }
  19. }
  20. </script>
Now on textbox event
  1. FromDate: @Html.TextBox("txtFromDate", new{@onblur="javascript:ValidateContent();"})
  2. ToDate: @Html.TextBox("txtTodate", new{@onblur="javascript:ValidateContent();"})
for detail have a look of the link

http://www.aspdotnet-pools.com/2015/09/disable-and-enable-button-using-jquery.html