javascript 如何禁用 ASP.Net 中的按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18653343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable a Button in ASP.Net
提问by Spenhouet
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.
我在禁用 ASP.net 中的按钮时遇到了大问题。当我在客户端禁用按钮时,该按钮在回发后启用。当我禁用服务器端的按钮时,我无法保存掩码,因为按钮不在。
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.
我该如何解决?保存操作在第一个 Preload/PostBack 上执行。这意味着第二个 PostBack 和之后的所有内容都不应执行保存过程。
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.
每次预加载后,我的 Button 也是新创建的。我怎样才能给这个 Button 一个固定的 ID,以便它总是与我正在覆盖的对象相同,并且我可以访问它。
in .cs:
在.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:
在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;
}
}
回答by huMpty duMpty
You can assign a css-class
to everytime you create the button.
您可以css-class
在每次创建按钮时分配一个。
Doing that you can do something like below using jQuery
这样做您可以使用以下方法执行以下操作 jQuery
$('.className').attr("disabled", true);
Using javascript
will need to use document.getElementsByClassName
使用javascript
将需要使用document.getElementsByClassName
回答by Awtszs
You could simply use javascript
to disable your button.
您可以简单地使用javascript
禁用您的按钮。
<script type="text/javascript">
function disableBtn()
{
document.getElementById("DivButton").disabled=true
}
</script>
回答by adripanico
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:
您可以通过使用该属性ClientIDMode
并将其设置为 来获取固定 ID Static
。而且,您还可以使用 JavaScript 访问控件,如下例所示:
<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
更多信息:http: //www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature
回答by user489998
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");
回答by dav_i
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
我的 addbutton mathod(查看我的帖子)被每个 Preload 调用。每次按下按钮时都会有一个预加载
Don't put the creation of the button in the Pre_Load
put it in Page_Load
with a check for IsPostBack
.
不要将按钮的创建Pre_Load
放在Page_Load
检查中IsPostBack
。
回答by Devraj Gadhavi
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.
你可以在Page_Init
事件中添加你的按钮,如果你想从.cs
文件中添加按钮并且不能将按钮放在.aspx
文件中,如下所示。
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.
在 中添加按钮Page_Init
将使其State
在每次回发期间保持不变。
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...
现在,您可以在 save_event 的页面中找到该按钮并像这样禁用它...
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.
好吧,如果您要添加此按钮,.aspx
则可以直接将其设置为启用 false,如下所示。
ButtonSave.Enabled = false;
instead of
代替
ActionButton b = (ActionButton)this.FindControl("ButtonSave");
ActionButton b = (ActionButton)this.FindControl("ButtonSave");
b.Enabled = false;
b.启用=假;