C# 如何在第一次单击后禁用按钮
我有一个按钮的onclick事件。一旦激活此事件,我想禁用该按钮。有人能帮我理解怎么做吗 下面是我在buttonclick事件中执行的代码C# 如何在第一次单击后禁用按钮,c#,javascript,asp.net,buttonclick,C#,Javascript,Asp.net,Buttonclick,我有一个按钮的onclick事件。一旦激活此事件,我想禁用该按钮。有人能帮我理解怎么做吗 下面是我在buttonclick事件中执行的代码 protected void Button3_Click(object sender, EventArgs e) { if (Session["login"] != null && Session["db"] != null) { digit b = new digit(); String
protected void Button3_Click(object sender, EventArgs e)
{
if (Session["login"] != null && Session["db"] != null)
{
digit b = new digit();
String digitformed = b.formdigit(this.DropDownList1, this.TextBox1, this.TextBox2);
chekcount c = new chekcount();
int count = c.getmaxcountforjud_no(digitformed);
int addtocount = count + 1;
String name = Session["login"].ToString();
String databs = Session["db"].ToString();
String complex_name = name + databs;
if (DropDownList2.SelectedItem.Text != "New")
{
update u = new update();
u.update1(this.Editor1, digitformed, this.TextBox3, complex_name, name, this.DropDownList2);
Response.Write(@"<script language='javascript'>alert('Updated')</script>");
}
else
{
save d = new save();
d.dosave(this.Editor1, addtocount, digitformed, this.TextBox3, complex_name, name);
Response.Write(@"<script language='javascript'>alert('Saved')</script>");
}
}
else
{
Response.Redirect("log.aspx");
}
}
protectedvoid按钮3\u单击(对象发送者,事件参数e)
{
if(会话[“登录”]!=null和会话[“数据库”]!=null)
{
数字b=新数字();
String digitformed=b.formdigit(this.DropDownList1、this.TextBox1、this.TextBox2);
chekcount c=新chekcount();
int count=c.getmaxcountforjud_no(数字格式);
int addtocount=计数+1;
字符串名称=会话[“登录”]。ToString();
字符串databs=Session[“db”].ToString();
String complex_name=name+databs;
如果(DropDownList2.SelectedItem.Text!=“新建”)
{
更新u=新更新();
u、 update1(this.Editor1,digitformed,this.TextBox3,complex_name,name,this.DropDownList2);
响应。写入(@“警报(‘更新’)”);
}
其他的
{
保存d=新保存();
d、 dosave(this.Editor1,addtocount,digitformed,this.TextBox3,complex_name,name);
响应。写入(@“警报(‘已保存’)”);
}
}
其他的
{
重定向(“log.aspx”);
}
}
这是我想禁用的按钮
<asp:Button ID="Button3" runat="server" Text="Save" onclick="Button3_Click"
Visible="False" />
参见标记的代码
protected void Button3_Click(object sender, EventArgs e)
{
** if (Session["Clicked"] == null)
Session["Clicked"] = true;
else
{
Button3.Enabled = false;
return;
} **
if (Session["login"] != null && Session["db"] != null)
{
digit b = new digit();
String digitformed = b.formdigit(this.DropDownList1, this.TextBox1, this.TextBox2);
chekcount c = new chekcount();
int count = c.getmaxcountforjud_no(digitformed);
int addtocount = count + 1;
String name = Session["login"].ToString();
String databs = Session["db"].ToString();
String complex_name = name + databs;
if (DropDownList2.SelectedItem.Text != "New")
{
update u = new update();
u.update1(this.Editor1, digitformed, this.TextBox3, complex_name, name, this.DropDownList2);
Response.Write(@"<script language='javascript'>alert('Updated')</script>");
}
else
{
save d = new save();
d.dosave(this.Editor1, addtocount, digitformed, this.TextBox3, complex_name, name);
Response.Write(@"<script language='javascript'>alert('Saved')</script>");
}
}
else
{
Response.Redirect("log.aspx");
}
}
protectedvoid按钮3\u单击(对象发送者,事件参数e)
{
**如果(会话[“单击”]==null)
会话[“单击”]=true;
其他的
{
按钮3.Enabled=false;
返回;
} **
if(会话[“登录”]!=null和会话[“数据库”]!=null)
{
数字b=新数字();
String digitformed=b.formdigit(this.DropDownList1、this.TextBox1、this.TextBox2);
chekcount c=新chekcount();
int count=c.getmaxcountforjud_no(数字格式);
int addtocount=计数+1;
字符串名称=会话[“登录”]。ToString();
字符串databs=Session[“db”].ToString();
String complex_name=name+databs;
如果(DropDownList2.SelectedItem.Text!=“新建”)
{
更新u=新更新();
u、 update1(this.Editor1,digitformed,this.TextBox3,complex_name,name,this.DropDownList2);
响应。写入(@“警报(‘更新’)”);
}
其他的
{
保存d=新保存();
d、 dosave(this.Editor1,addtocount,digitformed,this.TextBox3,complex_name,name);
响应。写入(@“警报(‘已保存’)”);
}
}
其他的
{
重定向(“log.aspx”);
}
}
你试过了吗
protected void Button3_Click(object sender, EventArgs e)
{
Button3.Enabled = false;
//rest of code
}
如果您想通过使用jquery来实现,那么
$('#button3').attr("disabled", true);
使用OnClient单击并使用按钮控件的SubmitBehavior属性
<asp:Button runat="server" ID="BtnSubmit"
OnClientClick="this.disabled = true; this.value = 'Submit in progress...';"
UseSubmitBehavior="false"
OnClick="BtnSubmit_Click"
Text="Click to Submit" />
重新设计的HTML:
<input type="button" name="BtnSubmit"
onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')"
value="Submit Me!" id="BtnSubmit" />
这将为您提供所需的行为
发件人:
信用:戴夫·沃德(推特:@Encosia)
在客户端单击按钮时调用此按钮您需要在服务器端代码中设置按钮的Enabled属性,如其他海报所述。但是,如果您试图阻止来自同一按钮的多个提交,则需要稍微不同的策略 向类中添加方法:
static void DisableButtonDuringPostback(Page page, Button control)
{
StringBuilder sb = new StringBuilder();
sb.Append("this.disabled = true;");
sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
sb.Append(";");
control.Attributes.Add("onclick", sb.ToString());
}
在第_页中加载添加
DisableButtonDuringPostback(this.Page, Button3);
这是一篇老文章,但是我认为它没有得到完全的回答。
首先,在ASP.NET WebForms中,您向web服务器提交
HTTP GET
请求,该服务器处理您的请求并输出客户端HTML代码供浏览器呈现。当您与服务器端控件交互时,属性的值包含在隐藏的
VIEWSTATE
输入字段中(例如启用的的布尔值)。
当您单击按钮时,它会向同一页面上的web服务器发送HTTPPOST
请求。这就是单击按钮时触发加载页面事件的原因。
处理httppost
请求后,它将返回HTML代码供浏览器重新呈现。因此,如果在页面加载事件中有以下代码:
if (Page.IsPostBack) { Button3.Enabled = false; }
在处理httppost
请求并返回更新的客户端代码之前,它不会向用户显示它已被禁用。
从最初的问题来看,服务器似乎需要几秒钟才能返回响应,因此当反复单击按钮时,可能会触发多个回发事件。
解决问题的一个简单(但烦人)方法是使用一个常规的HTML按钮
在JavaScript中执行一个函数
,该函数将禁用它并触发服务器端控件的onclick事件。这样做的问题是,当httppost
请求返回响应时,它将使常规HTML按钮处于启用状态。要解决这个问题,只需使用内嵌的ASP.NET代码在JavaScript中禁用它。这里有一个例子:
.ASPX文件
适用于WPF应用程序
Button1.IsEnabled = false; // Replace Button1 with your button name, false disables button
将“单击处理程序”中按钮的“已启用”属性设置为“False”。是否可以在希望禁用此按钮时清除?客户端或服务器postbackShouldn后不是$(“#button3”).attr(“已禁用”、“已禁用”)?我认为它可以在firefox中使用,但不能在IE中使用。这不会禁用用户浏览器中的按钮,用户仍然可以不受限制地反复单击按钮。这应该是最好的答案,因为返回后按钮将恢复为启用状态良好的答案,但是,如果我使用的是ajaxToolkit:confirButtoneXtender,它就不起作用了。
DisableButtonDuringPostback(this.Page, Button3);
if (Page.IsPostBack) { Button3.Enabled = false; }
<button id="clientButton" onclick="javascript:update();" />
<asp:Button ID="serverButton" OnClick="serverButton_OnClick" runat="server" />
<script type="text/javascript">
<% if (Page.IsPostBack) { %>
document.getElementById("clientButton").enabled = false;
<% } %>
function update() {
document.getElementById("clientButton").enabled = false;
document.getElementById("<%= serverButton.ClientID %>").click();
}
</script>
<style type="text/css">
#<%= serverButton.ClientID %> {
visibility: hidden;
display: none;
}
</style>
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// Triggered when serverButton has 'javascript:click()' triggered
}
}
Button1.IsEnabled = false; // Replace Button1 with your button name, false disables button