如何从用 asp.net 中的 scriptmanager.registerclientscript 编写的 javascript 确认框返回值?
Posted
技术标签:
【中文标题】如何从用 asp.net 中的 scriptmanager.registerclientscript 编写的 javascript 确认框返回值?【英文标题】:How to return value from javascript confirm box written in scriptmanager.registerclientscript in asp.net.? 【发布时间】:2012-04-09 21:26:44 【问题描述】:如何从asp.net中scriptmanager.registerclientscript编写的javascript确认框返回值?
实际上我想在gridview的文本框的文本更改事件上给出确认框。如果用户单击是,那么我想更新更改的值,如果用户单击否,则它应该恢复为旧值。
我的虚拟代码是这样的:
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
【问题讨论】:
【参考方案1】: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>";
【讨论】:
【参考方案2】:我建议不要在 TextBox
'TextChanged
事件上回帖 (AutoPostback=true
)。
相反,我建议在客户端做所有事情。一种方法是处理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
您可以在服务器端在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')");
【讨论】:
非常感谢蒂姆...我要试试这个..:)以上是关于如何从用 asp.net 中的 scriptmanager.registerclientscript 编写的 javascript 确认框返回值?的主要内容,如果未能解决你的问题,请参考以下文章
如何仅为 ASP.NET 5 (ASP.NET Core) 中的受保护操作添加令牌验证