0

I have an asp.net application which is a random generator and I want my button to disable and stay disabled when clicked.

I have tried adding OnClientClick="this.disabled = true;" to my <asp:button> and also tried adding the following to my onclick in the code behind BtnDecideForMe.Attributes.Add("onclick", "this.disabled=true;"); but none of these work.

Not bothered how its done as long as it's clean and does the job.

HTML

<div class="col-sm-2">
    <asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
</div>

On_Click Event

protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
    List<string> Eat = new List<string>();
    Eat.Add("Chinese Buffet");
    Eat.Add("Harvester");
    Eat.Add("Frankie & Benny's");
    Eat.Add("Hungry Horse");
    Eat.Add("Blaize");
    Eat.Add("Chiquito");
    Eat.Add("Cafe Football");
    Eat.Add("Nando's");

    Random Place = new Random();
    int Places = Place.Next(0, Eat.Count);
    txtDestination.Text = Eat[Places];
  //BtnDecideForMe.Enabled = false;
}

I don't really want to use BtnDecideForMe.Enabled = false; as it loses my bootstrap styling and don't really want to apply a whole lot of css.

1
  • When you say stay disabled do you mean even after postback a page reload? Commented Oct 22, 2015 at 15:34

4 Answers 4

1

You can use the below code in button click event.

BtnDecideForMe.Enabled = false;
Sign up to request clarification or add additional context in comments.

1 Comment

@vidhyardhiI I do have your suggestion in my code already but I have commented it out as the enabled style doesn't look like my bootstrap style I am using an I would have to create a whole lot of .css so decided not to go with this.
0

I haven't looked into asp.net much, but if you're just trying to use jQuery to disable a button on click can you target it as you would any other element and use something like this?

  $(document).on("click", "#your-buttons-id", function() {
    $(this).prop("disabled", true);
  });

http://codepen.io/jonathanrbowman/pen/jbZVyL#0

1 Comment

I have tried adding your code but it doesn't disable when clicked. I have added my whole on-click to my post in case this helps
0

Using JQuery, you can achieve that using below code snippet. This will disable the button when the form is posted and remove the disabled attribute once completed.

  $(document).on('invalid-form.validate', 'form', function () {
        var button = $(this).find('input[type="submit"]');
        setTimeout(function () {

            button.removeAttr('disabled');
        }, 1);
    });
    $(document).on('submit', 'form', function () {
        var button = $(this).find('input[type="submit"]');
        setTimeout(function () {

            button.attr('disabled', 'disabled');
        }, 0);
    });

2 Comments

I have tried adding your code but it doesn't disable when clicked. I have added my whole on-click to my post in case this helps
Have you received any errors? Anything that might be useful. This should work if you have the jquery reference and the right button id
0

Not want I wanted to do but I decide to duplicate code (not the best I no but it does the job), anyway answer below:

<% if (txtDestination.Text == "")
{%>
    <asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
<%}
else
{ %>
    <button class="btn btn-success" disabled>Decided</button>
<%} %>

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.