使用javascript和VB进行服务器端确认 [英] Server side confirmation using javascript and VB

查看:91
本文介绍了使用javascript和VB进行服务器端确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我点击是,则返回否



返回第二次点击时的第一次点击值,



如果我点击是第一次它返回否,我点击第二次没有它返回我的第一个值是..



我尝试过:



 功能确认(){
var confirm_value = document .createElement( INPUT);
confirm_value.type = hidden;
confirm_value.name = confirm_value;
if (确认( 你想要吗?删除数据?)){
confirm_value.value = ;
} else {
confirm_value.value = ;
}
document .forms [ 0 ]。appendChild(confirm_value);
}



VB代码

 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page,  GetType (页面), 脚本 确认();

Dim confirmValue As String = Request.Form( confirm_value
如果 confirmValue = 然后

Response.Write( 用户点击是
否则
响应。写( 用户点击否
结束 如果

解决方案

这里的方法是错误的。如果您想确认用户正在做某事,请在JavaScript中执行以下操作:

  function  ConfirmDelete(){
return confirm( 要删除数据吗?); // 这意味着该函数将返回true或false,具体取决于用户点击的内容。
}





然后在你的按钮的ASPX一侧你这样做:

 <   aspButton     id   =  btnConfirm   文字  = 确认    OnClientClick   = 返回ConfirmDelete();    OnClick   =  btn点击    /  >  





如果用户在确认对话框中单击取消,则将OnClientClick添加到按钮然后从ConfirmDelete()返回值,将返回false并且OnClientClick将返回false,这将阻止按钮触发回发和服务器端代码。如果用户在Confirm对话框中单击OK,则返回true,服务器端代码将被执行。


由于我们不知道执行VB代码的上下文,因此很难回答。它是否在Page Load中?或者在其他地方?



您的问题可能源于一个误解,即RegisterClientScriptBlock以某种方式在代码中运行您的javascript并且您的.net代码在浏览器中运行并等待输入,然后在浏览器中单击确认后执行if行。这一切都没有发生。



RegisterClientScriptBlock正在做的是将javascript添加到输出中,以便当你的所有.net代码都已执行并且所有html都已执行时构建后,然后将html发送到浏览器,然后运行javascript。



 System.Web.UI .ScriptManager.RegisterClientScriptBlock(Page, GetType (页面), 脚本 确认(); True 
' 执行.net代码上方的行继续运行

Dim confirmValue 作为 String = Request.Form( confirm_值

' 请求中没有confirm_value,因此confirmValue将是

如果 confirmValue = 然后

Response.Write( 用户点击是
否则
Response.Write ( 用户点击否
结束 如果

' 现在您的.net代码已经完成构建html以发送给客户端,
' 发送了html,然后浏览器运行你的js并出现确认框


此代码正常工作

  function 确认(){
var confirm_value = document .createElement( INPUT);
confirm_value.type = hidden;
confirm_value.name = confirm_value;
if (确认( 你想要吗?删除数据?)){
confirm_value.value = ;
} else {
confirm_value.value = ;
}
document .forms [ 0 ]。appendChild(confirm_value);
}



 <   asp:按钮    ID   =  Button1    OnClientClick   = 确认()   < span class =code-attribute> runat   =  server     文字  =  按钮  /  >  



 受保护的  Sub  Button1_Click(se nder 作为 对象,e  As  EventArgs) 句柄 Button1.Click 

Button1.OnClientClick = return Confirm();

Dim confirmValue As 字符串
confirmValue = Request.Form( confirm_value

如果 confirmValue = 然后
Response.Write( 用户点击是
否则
Response.Write ( 用户点击否
结束 如果

结束 Sub


If i click yes its returns No

It return First click value on my second click ,

if i click yes first time it returns no,and i click second time no its return my first value yes..

What I have tried:

function Confirm() {
             var confirm_value = document.createElement("INPUT");
             confirm_value.type = "hidden";
             confirm_value.name = "confirm_value";
             if (confirm("Do you want to Delete data?")) {
                 confirm_value.value = "Yes";
             } else {
                 confirm_value.value = "No";
             }
             document.forms[0].appendChild(confirm_value);
         }


VB CODE

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Confirm();", True)

        Dim confirmValue As String = Request.Form("confirm_value")
        If confirmValue = "Yes" Then

            Response.Write("User Clicked yes")
        Else
            Response.Write("User Clicked No")
        End If

解决方案

The approach here is wrong. If you want to confirm that a user is doing something then do something like this in JavaScript:

function ConfirmDelete(){
return confirm("Do you want to Delete data?"); // this means that the function will either return true or false depending on what user clicks.
}



Then in the ASPX side on your button you do this:

<aspButton id="btnConfirm" Text="Confirm" OnClientClick="return ConfirmDelete();" OnClick="btnClick" />



By adding OnClientClick to your button and then returning the value from ConfirmDelete(), if the user clicks Cancel in the Confirm dialog box, a false will be returned and OnClientClick will return false which will prevent the button from firing the postback and server side code. If user clicks OK in Confirm dialog then true is returned and the server side code will get executed.


It's hard to answer as we don't know the context the VB code is executed in. Is it in Page Load? Or somewhere else?

Your issue probably stems from a misunderstanding that RegisterClientScriptBlock is somehow running your javascript in code behind and that your .net code is running inside the browser and waits for your input and then does your "if" line after you click the confirmation in the browser. Well none of that is happening.

What RegisterClientScriptBlock is doing is adding that javascript to the output so that when all of your .net code has executed and all html has been built, that html is then sent to the browser and it is then that the javascript runs.

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "Confirm();", True)
' after executing the line above your .net code continues running

Dim confirmValue As String = Request.Form("confirm_value")

' there is no confirm_value in the request so confirmValue will be ""

If confirmValue = "Yes" Then

Response.Write("User Clicked yes")
Else
Response.Write("User Clicked No")
End If

' now that your .net code has finished building html to send to the client, that
' html is sent and the browser then runs your js and the confirm box appears


This code Working Fine

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("Do you want to Delete data?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
}


<asp:Button ID="Button1" OnClientClick="Confirm()" runat="server"  Text="Button"/>


Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       
    Button1.OnClientClick = "return Confirm();"

    Dim confirmValue As String
    confirmValue = Request.Form("confirm_value")

    If confirmValue = "Yes" Then
        Response.Write("User Clicked yes")
    Else
        Response.Write("User Clicked No")
    End If

End Sub


这篇关于使用javascript和VB进行服务器端确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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