如何从C#按钮onclick事件调用javascript确认 [英] How to call javascript confirm from C# button onclick event

查看:327
本文介绍了如何从C#按钮onclick事件调用javascript确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我想从c#按钮OnClick事件调用javascript确认。请帮帮我。



  protected   void  btnSave_Click( object  sender,EventArgs e)
{
string Confirmvalu = string .Empty;
bool check = checkattachments(); // 返回bool值
如果(选中)
{
/ / 然后我想调用Javascript确认
// 并且根据确认值即Confirmvalu,我必须执行类似
if (Confirmvalu ==
{
/ / Do Something1
}
else
{
// Do Something2
}
}
else
{
// JA vascript不需要
}
}





我尝试了什么:



请帮我解决这个

解决方案

检查一下,要求用户确认他们想要继续行动| ASP.NET论坛 [ ^ ]


最好在Html设计器中添加代码,如下所示。



 <   asp:button     id   =  Button1    xmlns:asp   = #unknown >  
Text = 提交
onclick =返回确认('你确定');
runat =server/> < / asp:button >


同意你应该在4.0中这样编码: -



 <   asp:按钮    onclientclick   = 返回确认('你确定,你想继续?');    runat   =  server    id   =  btnTest    onclick   =   btnTest_Click    text   = 点击此处    xmlns:asp   = #unknown    /  >  





正如您在最新版本中看到的那样,我们正在处理onclientclick和onclick事件。 onclientclick将处理客户端点击事件。



因此,首先单击将显示确认框的客户端代码,如果单击确定它将移动到旁边服务器端代码,你做任何你想做的事情,否则这将取消服务器端点击事件,返回false。



希望这对你有所帮助。 / BLOCKQUOTE>

Here I want to call javascript confirm from c# button OnClick event. Please help me.

protected void btnSave_Click(object sender, EventArgs e)
{
string Confirmvalu=string.Empty;
bool check = checkattachments();//Returns bool value
if (check)
{
//Then I Would like to Call Javascript Confirm 
//And based on confirm value i.e. Confirmvalu, I have to do something like       
if(Confirmvalu=="Yes")
{ 
//Do Something1
}
else
{
//Do Something2
}
}
else
{
//Javascript no need
}
}



What I have tried:

Please help me to resolve this

解决方案

Check this, Asking the user to confirm that they want to continue with an action | The ASP.NET Forums[^]


Better to add code in Html designer as below.

<asp:button id="Button1" xmlns:asp="#unknown">
           Text="Submit"
           onclick="return confirm('Are you sure');"
           runat="server"/></asp:button>


Agreed you should code like this in 4.0:-

<asp:button onclientclick="return confirm('Are you sure, you want to continue?');" runat="server" id="btnTest" onclick="btnTest_Click" text="Click Here" xmlns:asp="#unknown" />



As you can see in latest version we are getting both "onclientclick" and "onclick" events to handle. "onclientclick" will handle the client side click event.

So on click first the clientside code that will show the confirm box, if you click ok it will move next to serverside code where you do whatever you wanted to do or else this will cancel the server side click event by returning false.

Hope this will be of help to you.


这篇关于如何从C#按钮onclick事件调用javascript确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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