我在updatepanel中有几个文本框,我正在做一些简单的验证。如果验证检查失败,则会在对话框中弹出错误消息。我的问题是,当其中一个文本框的验证失败而另一个文本框失败时,RegisterClientScriptBlock会显示该对话框。在这两种情况下,事件都在解雇。在下面的代码中,当txtCustMSCName(下面的第二个在外部If语句的下面)文本框未通过验证条件但在txtMSCName文本框失败时不显示时,对话框会正确显示。任何想法为什么会发生这种情况?是否与txtMSCName设置为ReadOnly = True?
的事实有关VB:
If chkCustomMSC.Checked = False Then
If txtMSCName.Text = "No sales contact for this account" Then
DialogMsg = "alert('There are no main sales contacts for this account in CRM; please check the 'Custom MSC' box and " _
+ "manually enter the main sales contact information');"
ErrorDialog(DialogMsg)
Exit Sub
End If
Else
If txtCustMSCName.Text = "" Then
DialogMsg = "alert('You must enter a main sales contact name');"
ErrorDialog(DialogMsg)
Exit Sub
End If
End If
Protected Sub ErrorDialog(ByVal Message As String)
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), Message, True)
End Sub
标记:
<asp:TextBox ID="txtMSCName" runat="server" ReadOnly="true" CssClass="DisplayTextBoxStyle"/>
<asp:TextBox ID="txtCustMSCName" runat="server" CssClass="MSCInputTextBoxStyle"/>
答案 0 :(得分:1)
问题在于DialogMsg
字符串,您有 'Custom MSC'
。您需要正确地转义每个 single quote character
,以便生成的JavaScript 格式良好,并且可以通过Web浏览器正确处理。
由于您使用单引号启动alert()
JavaScript函数,因此必须以另一个单引号结束,因此:
示例1: alert('hello world');
效果很好!
示例2: alert('hello 'world'');
无效,实际上会导致JavaScript错误:
SCRIPT1006:预期')'
要解决此问题,您需要 ESCAPE 警告字符串中的每个单引号。
示例2应为: alert('hello \'world\'');
DialogMsg = "alert('There are no main sales contacts for this account in CRM; please check the 'Custom MSC' box and " _
+ "manually enter the main sales contact information');"
DialogMsg = "alert('There are no main sales contacts for this account in CRM; please check the \'Custom MSC\' box and " _
+ "manually enter the main sales contact information');"
请注意 \'
自定义MSC \'
答案 1 :(得分:0)
尝试Page.ClientScript.RegisterClientScriptBlock(),因为我相信Page.RegisterClientScriptBlock在asp.net的版本中已经过时了