禁用按钮并运行代码 [英] Disable button and run code

查看:93
本文介绍了禁用按钮并运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在ASP.NET(C#)中有一个表单。两个文本框和一个ASP.NET按钮。这个想法很简单:按下按钮,禁用它,运行代码(在后面的代码中)并重定向到另一个页面。



听起来很简单,但我可以'让它工作。我已经尝试过Javascript,JQuery和.NET本身。 Jquery现在要在网站上构建复杂。 Javascript似乎工作。在按钮上是一个OnClientClick。这被触发并且该函数禁用了该按钮,但后面的代码没有被触发。禁用后面代码中的按钮不是一个选项,因为该页面不是Ajax或类似的东西。



我的猜测是Javascript接近。我只需要执行按钮的.net代码。我怎么能这样做?





这是工作的一半:



 受保护  void  Page_Load( object  sender,EventArgs e)
{
btnClick.Attributes.Add( onclick this.disabled = true);
}

受保护 void 测试( object sender,EventArgs e)
{

}





在void Test上设置断点并运行freaking项目。该按钮被禁用,但脚本永远不会达到测试。



任何人都知道解决方案吗?



谢谢

解决方案

只需在javascript中进行一些更改 -



制作一个功能



< script> 
function disableMe(me)
{
me.disabled = true;
返回true;
}
< / script>





现在更改页面加载时的行



 btnClick.Attributes.Add(onclick,disableMe(this)); 





返回true;如果你写回返false,则声明是调用服务器端代码的理由;或删除return语句服务器端代码不会执行。



--Pankaj


你真的不需要明确使用Button.Enable true / false禁用它。最好的方法是使用 Page.ClientScript.GetPostBackEventReference



仅举例来说,

 受保护  void  Page_Load( object  sender,EventArgs e)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, )+ ; this.value ='Processing ...'; this。 disabled = true;;
}



注意:更改文本完全取决于您。这就是您如何实现您的要求。

你可以在这里查看我的完整帖子在ASP.NET中执行某些操作时禁用按钮 [ ^ ]

干杯!!


试一试一,我希望这可以帮到你



 受保护  void  Button1_Click( object  sender,EventArgs e)
{
Button1.Enabled = ;
Response.AddHeader( 刷新 5; Url = Default.aspx);

}





快乐编码.........


Hi,

I have a form in ASP.NET (C#). Two textboxes and a ASP.NET button. The idea is simple: Press the button, disable this, run the code (in code behind) and redirect to an other page.

Sounds simple, but I can't get it to work. I've tried Javascript, JQuery and .NET itself. Jquery is to complex to build in the website right now. Javascript seemed to work. On the button was a "OnClientClick". This is fired and the function disabled the button, but the code behind was not fired. Disabling the button in the code behind is not an option, because the page is not Ajax or something like that.

My guess is that Javascript comes close. I only need to execute the .net code of the button. How can I do this?


This is what worked half:

protected void Page_Load(object sender, EventArgs e)
{
    btnClick.Attributes.Add("onclick", "this.disabled=true");
}

protected void Test(object sender, EventArgs e)
{

}



Set a breakpoint on void Test and run the freaking project. The button is disabled, but the script never reaches Test.

Anyone know the solution?

Thanks

解决方案

just make some changes in javascript-

make a function

<script>
function disableMe(me)
{
   me.disabled=true;
   return true;
}
</script>



and now change the line at page load as

btnClick.Attributes.Add("onclick", "disableMe(this)");



return true; statement is reason to call server side code if u write return false; or remove return statement the server side code will not execute.

--Pankaj


You really don't need to explicitly disabled it by using Button.Enable true/false. The best ways is to use Page.ClientScript.GetPostBackEventReference

Just for example,

protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick = ClientScript.GetPostBackEventReference(Button1, "") + "; this.value=’Processing…’;this.disabled = true;";
}


Note: Changing the text is totally depends on you. This is how you can achieve your requirement.
You can check out my complete post here Disabling button when performing some operation in ASP.NET[^]
Cheers !!


Try this one , I hope this may help you

protected void Button1_Click(object sender, EventArgs e)
       {
           Button1.Enabled = false;
           Response.AddHeader("Refresh", "5;Url=Default.aspx");

       }



Happy codeing.........


这篇关于禁用按钮并运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆