[英]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. 我建议不要在
TextBox
的TextChanged
事件上回发( 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.