How to return value from javascript confirmation window written in scriptmanager.registerclientscript in asp.net.?

How to return value from javascript confirmation window written in scriptmanager.registerclientscript in asp.net.?

Actually, I want the confirmation box to change the text of the gridview textbox. If the user clicks yes, I want to update the changed value, and if the user clicks no, then he should revert to the old value.

My dummy code looks 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 

      

+3


source to share


2 answers


I would suggest not returning ( AutoPostback=true

) on event TextBox

' TextChanged

.

Instead, I would recommend doing everything on clients. One way is to handle the event onchange

to show 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;
    }
}

      



Default property defaultValue

You can register this function with the server at :RowDataBound

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')");
    }
}

      

+4


source


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

      



0


source







All Articles