点击按钮服务器端事件不会被触发 [英] At a click of button server side event doesnt gets fired

查看:99
本文介绍了点击按钮服务器端事件不会被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的会员,



我已经为客户端验证编写了以下代码,



< pre lang =cs> function isStatValid(btn){
var ddlist = document.getElementById(' <%= ddlChngStatus.ClientID%>');
if (ddlist.options [ddlist.selectedIndex]。 value == 0 ){
alert(' 请选择状态');
return false ;
}

if ((eval(totDebit)== 0 )&&(eval(totCredit)== 0 )){
alert('' 请选择至少一个模板');
return false ;
}
其他 {
btn.disabled = true ;
btn。 value = ' 更新状态。 ..';
return true ;
}
返回 true ;
}





和onclientclick我正在调用上面的函数

 <   asp:按钮   < span class =code-attribute> ID   =  btnUpdStat    runat   =  server   文本  = 更新状态   可见  =  false    usesubmitBehavior   =  false    OnClientClick   =  javascript:return isStatValid(this)  /  >  





此函数完美地执行客户端验证,但由于任何原因无法引发服务器端按钮点击事件。



请建议一些解决方案。

解决方案

你必须看到你实际上有一个服务器端事件这个







< asp:button id =   Button2 runat =   server text =  按钮 onclick =   Button2_Click onc lientclick =   javascript:return isStatValid(this) xmlns:asp =  #unknown /> 







此按钮有2个事件,一个客户端和一个服务器端

首先它执行客户端事件,如果返回true,它将执行服务器端代码



  protected   void  Button2_Click( object  sender,EventArgs e)
{

}







这是你在CS代码上找到的事件方法页面。



在你的情况下按钮没有服务器事件 onclick =Button2_Click


< blockquote>这不是因为你在回发之前已经禁用了Button。浏览器不允许禁用按钮进行回发。但是有一种实现所需功能的好方法。 Dave Ward在这里解释得非常好 - 在回发期间禁用按钮控制。 [ ^ ]



您可以选择在验证代码后使用 - __ doPostBack(''btnUpdStat'','''')进行显式回发;



希望这会有所帮助!


您尚未指定 onclick 事件按钮。

例如,您需要执行以下操作...

  <   asp:按钮    id   =  btnUpdStat    

runat < span class =code-keyword> = server
< span class =code-attribute>
< span class =code-attribute>文本 = 更新状态

OnClientClick = javascript:return isStatValid(this)

< span class =code-attribute> onclick = SubmitBtn_Click / >
< / asp:按钮 >



  void  SubmitBtn_Click( object  sender,EventArgs e)
{
Mes sage.Text = Hello World!;
}



并删除 usesubmitBehavior =false,它正在创建问题。 />
应该是真的。由于默认值为true,因此无需指定它。



参考MSDN链接中给出的示例 -

1. Button.UseSubmitBehavior Property [< a href =http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspxtarget =_ blanktitle =New Window> ^ ]。

2. 按钮.OnClientClick属性 [ ^ ]


Dear members,

I have written the below code for client side validation,

function isStatValid(btn) {
           var ddlist = document.getElementById('<%=ddlChngStatus.ClientID %>');
           if (ddlist.options[ddlist.selectedIndex].value == 0) {
               alert('Please select status');
               return false;
           }

           if ((eval(totDebit) == 0) && (eval(totCredit) == 0)) {
               alert('Please select atleast one template');
               return false;
           }
           else {
               btn.disabled = true;
               btn.value = 'Updating Status...';
               return true;
           }
           return true;
       }



and onclientclick i am calling the above function

<asp:Button ID="btnUpdStat" runat="server" Text="Update Status" Visible="false" usesubmitBehavior="false" OnClientClick="javascript:return isStatValid(this)"/>



this function perform client validation perfectly, but by any reason fails to raise server side button click event.

please suggest some solutions.

解决方案

You must see that you have actually have a server side event for this



<asp:button id="Button2" runat="server" text="Button" onclick="Button2_Click" onclientclick="javascript:return isStatValid(this)" xmlns:asp="#unknown" /> 




This button has 2 events one client side and one server side
First it executes client side event and if that returns true it will execute server side code

protected void Button2_Click(object sender, EventArgs e)
{

}




This is the Event Method you will find on CS code page.

In your case there is no server event for the button onclick="Button2_Click"


It won''t because you have disabled the Button before it posts back. Browsers do not allow a disabled button to postback. But there is a nice way of implementing the functionality you want. Dave Ward explains it very well here - Disable a button control during postback.[^]

You may also optionally do an explicit postback after your validation code using - __doPostBack(''btnUpdStat'','''');

Hope this helps!


You have not specified the onclick event for the button.
For example, you need to do something like below...

<asp:button id="btnUpdStat" 

            runat="server"

            Text="Update Status"

            OnClientClick="javascript:return isStatValid(this)"

            onclick="SubmitBtn_Click" />
</asp:button>


void SubmitBtn_Click(object sender, EventArgs e)
{
    Message.Text = "Hello World!";    
}


And remove the usesubmitBehavior="false", it is creating the problem.
It should be true. As the default is true, so no need to specify it.

Refer the examples given in MSDN links -
1. Button.UseSubmitBehavior Property[^].
2. Button.OnClientClick Property[^]


这篇关于点击按钮服务器端事件不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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