2

In my .aspx file I have a button

<asp:Button ID="BtnSearch" runat="server" Text="Search" ValidationGroup="SearchRoles" CausesValidation="true" OnClick="BtnSearch_Click" />

Normally in the .cs file if I needed to call any methods for the button I would call BtnSearch.method() but I have a function in the JavaScript for EditClick() and I would like to call BtnSearch.Enable = false but I get an exception. How would I disable this button in the JavaScript function?

2 Answers 2

1

You can use jQuery. These days, you won't normally see a website which doesn't use jQuery or Javascript Framework.

Since you cannot predict Server Control ID at run-time, you want to use #<%= SearchButton.ClientID %>.

enter image description here

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<asp:Button ID="SearchButton" runat="server" Text="Search"
    ValidationGroup="SearchRoles" CausesValidation="true"
    OnClick="SearchButton_Click" />
<br />
<input type="checkbox" id="DisableButtonCheckBox" />
    Check to disable button
<script type="text/javascript">
    $(function () {
        $("#DisableButtonCheckBox").click(function () {
            if (this.checked) {
                $("#<%= SearchButton.ClientID %>").prop("disabled", true);
            } else {
                $("#<%= SearchButton.ClientID %>").prop("disabled", false);
            }
        });
    });
</script>

I personally do not like using ClientIDMode="static", unless I do not have any other option.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use the

OnClientClick=""

attribute in the asp button?

Then run inline code similar to

$(this).prop('disabled', true);

Final view:

<asp:Button ID="BtnSearch" runat="server" Text="Search" ValidationGroup="SearchRoles" CausesValidation="true" OnClick="BtnSearch_Click" OnClientClick="$(this).prop('disabled', true);"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.