简体   繁体   English

如何从asp.net中的scriptmanager.registerclientscript中写的javascript确认框返回值。

[英]How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.net.?

How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.net.? 如何从asp.net中的scriptmanager.registerclientscript中写的javascript确认框返回值。

Actually I want to give confirm box on text changed event of textbox of gridview.If user click yes then I want to update changed value and if user click no then it should revert back to old value. 实际上我想在gridview文本框的文本更改事件上给出确认框。如果用户单击是,那么我想更新更改的值,如果用户单击否则它应该恢复为旧值。

My dummy code is like this: 我的虚拟代码是这样的:

Protected Sub GridViewCreateInvoice_QuantityTextChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim gr As GridViewRow
        Dim i As Boolean
        Dim txtqty, txtupdatedQty As TextBox
        Dim txtoqty, qty As String
        Dim result As MsgBoxResult
        'Dim dtPOFulfillmentInfo As DataTable

        Try

            txtqty = DirectCast(sender, TextBox)
            gr = txtqty.NamingContainer


            '' txtoqty = GridViewCreateInvoice.Rows(gr.DataItem("originalqty")).ToString()
            txtoqty = DataBinder.Eval(gr.DataItem, "originalqty").ToString()
            qty = DataBinder.Eval(gr.DataItem, "qty").ToString()


            If Not ((txtqty.Text = String.Empty And Not txtqty.Text.Trim = "" And Not txtqty.Text.Contains(" ")) And (txtoqty = String.Empty)) Then

                If txtqty.Text > txtoqty Then
                     ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "Confirm Quantity Changed", return confirm("Are you sure you want to continue"), True)

                    If i = True Then
                        DataBinder.Eval(gr.DataItem, "qty") = txtqty.Text
                    Else
                        txtqty.Text = DataBinder.Eval(gr.DataItem, "qty").ToString()
                    End If
                Else
                     ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "Confirm Quantity Changed", return confirm("Are you sure you want to continue"), True)

                    If i = True Then
                        DataBinder.Eval(gr.DataItem, "qty") = txtqty.Text
                    Else
                        txtqty.Text = DataBinder.Eval(gr.DataItem, "qty").ToString()
                    End If
                End If
            End If
        Catch ex As Exception

            Common.WriteLog(ex.Message)
            Common.WriteLog((ex.StackTrace))
            Response.Redirect("~/Error.aspx", False)
        End Try
    End Sub 

I would suggest to not post back ( AutoPostback=true ) on the TextBox ' TextChanged event. 我建议不要在TextBoxTextChanged事件上回发( AutoPostback=true )。

Instead i would recommend to do all on clientside. 相反,我建议在客户端做所有。 One way would be to handle the onchange event to show the javascript confirm and restore the old value immediately if the user clicks cancel : 一种方法是处理onchange事件以显示javascript confirm并在用户单击cancel立即恢复旧值:

function confirmUpdate(sender,message){
    var update = confirm(message);
    if(update){
        return true;
    }else{
        sender.value = sender.defaultValue;
        return false;
    }
}

Text defaultValue Property 文本defaultValue属性

You can register this function on serverside in RowDataBound of the GridView : 您可以在GridView RowDataBound中的服务器端注册此函数:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txtqty = (TextBox)e.Row.FindControl("txtqty");
        txtqty.Attributes.Add("onchange", "return confirmUpdate(this, 'Are you sure you want to continue')");
    }
}
I dont know what else logic you had implemented..
its a simple solution may be it helps u..

put a label for e.g lblScript in your page

then simply set its text from server side i.e
lblScript.Text = 
"<script> var x=window.confirm('Are you sure you are ok?')
if (x)
window.alert('Good!') //
else
window.alert('Too bad')
</script>";    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ScriptManager.RegisterClientScript 在异步面板内 FormView 内的 UserControl 中 - ScriptManager.RegisterClientScript in a UserControl within a FormView inside an Async Panel 如何在ASP.NET C#中获得确认框返回值 - How to get confirm box return value in ASP.NET C# 如何在 Javascript 中获取会话值,但我的 javascript 代码位于 asp.net 后面的代码中。 我正在尝试在 javascript 中设置变量值 - How to get session value in Javascript but my javascript code is in the code behind in asp.net. I am trying to set the variable value in javascript 如何从asp.net的文件夹中读取多个pdf文件? - How to read multiple pdf file from folder in asp.net.? ASP.NET C#后端,用户按OK或从JavaScript确认取消确认框? - ASP.NET C# backend, did the user press OK or Cancel from JavaScript confirm box? javascript在asp.net Web应用程序中确认框重定向? - Javascript Confirm box redirection in asp.net web application? 如何在asp.net中验证表单数据后获取javascript确认框? - How to get javascript confirm box after validating the form data in asp.net? 在C#中接受Javascript确认框的返回值 - Accepting return value of Javascript Confirm box in C# Asp.Net中的确认框 - Confirm Box in Asp.Net 我正在使用集合来填充asp.net中的treeview。 我将如何从树中删除节点 - I am using collection to populate treeview in asp.net. How i will delete node from tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM