简体   繁体   中英

How to disable a Button in ASP.Net

I have big problems with disabling a Button in ASP.net. When i disable the button on client side, the button is enabled after postback. When i disable the button on server side, i can't save a mask because the button is away.

How can i fix that? The saving action is executed on the first Preload/PostBack. Meaning that the second PostBack and everything after should not do the save process.

Also my Button is created new after every preload. How can i give this Button a fixed ID so that it is always the same object that i'm overwriting and on which i have acess.

in .cs:

    private void AddSaveButton()
    {
        ActionButton b = new ActionButton(GetString(6025));
        b.ID = "ButtonSave";
        b.ClientInstanceName = "ButtonSave";
        b.UseSubmitBehavior = true;
        b.Click += new EventHandler(save_event);
        DivButton.Controls.Add(b);
    }

in aspx:

 <div runat="server" id="DivButton" style="margin-top: 10px; padding-bottom: 15px; float: left">

ActionButton:

public class ActionButton : DevExpress.Web.ASPxEditors.ASPxButton
{
    public ActionButton(string text)
    {
        this.Text = text;
        this.CssClass = "actionButton";
        this.CssPostfix = "actionButton";
        this.AllowFocus = false;
        this.UseSubmitBehavior = false;
        this.CausesValidation = true;
    }
}

You can assign a css-class to everytime you create the button.

Doing that you can do something like below using jQuery

 $('.className').attr("disabled", true);

Using javascript will need to use document.getElementsByClassName

You could simply use javascript to disable your button.

  <script type="text/javascript">
    function disableBtn() 
    {
    document.getElementById("DivButton").disabled=true
    }
  </script>

You can get a fixed ID by using the property ClientIDMode and setting it to Static . But also, you can access to the control using JavaScript like in the example below:

<script type="text/javascript">
  function DoSomething() {
    alert('<%= Control.ClientID %>');
  }
</script>

More info: http://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature

I'm not sure what you mean by "my button is created after every preload" or "I can't save the mask".

If you want to affect the enabled status on postback you can do either of the following:

button.Enabled = false;

or

button.Attributes.Add("disabled", "disabled");

From your comment :

my addbutton mathod (look in my post) is called by every Preload. And everytime the button is pressed there is a preload

Don't put the creation of the button in the Pre_Load put it in Page_Load with a check for IsPostBack .

You can add your button in the Page_Init event, if you want to add the button from the .cs file and can't place the button in the .aspx file, like below.

protected void Page_Init(Object sender, EventArgs e)
{
    AddSaveButton();
}

Adding the button in the Page_Init would make it maintain its State during every post back.

Your method to add save button as it is...

private void AddSaveButton()
{
    ActionButton b = new ActionButton(GetString(6025));
    b.ID = "ButtonSave";
    b.ClientInstanceName = "ButtonSave";
    b.UseSubmitBehavior = true;
    b.Click += new EventHandler(save_event);
    DivButton.Controls.Add(b);
}

Now, you can find the button from the page in your save_event and disable it like this...

protected void save_event(Object sender, EventArgs e)
{
    ActionButton b = (ActionButton)this.FindControl("ButtonSave");
    b.Enabled = false;
    //do your saving stuff here...
}

After this, button would be disabled on every postback.

EDIT :

Well, if you are adding this button from .aspx then you can directly set it to enabled false like below.

ButtonSave.Enabled = false;

instead of

ActionButton b = (ActionButton)this.FindControl("ButtonSave");

b.Enabled = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM