admin管理员组

文章数量:1398829

How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.?

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.

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 

How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.?

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.

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 
Share Improve this question edited Mar 26, 2012 at 11:22 Saurabh 5,7272 gold badges27 silver badges32 bronze badges asked Mar 26, 2012 at 11:12 PriyanshPriyansh 1471 gold badge5 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I would suggest to not post back (AutoPostback=true) on the TextBox' TextChanged event.

Instead i would remend 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:

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

Text defaultValue Property

You can register this function on serverside in RowDataBound of the GridView:

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>";    

本文标签: