0

我在更新面板中有几个文本框,我正在为此做一些简单的验证。如果验证检查失败,则会在对话框中弹出错误消息。我的问题是当其中一个文本框的验证失败但另一个文本框没有验证时,RegisterClientScriptBlock 会显示对话框。在这两种情况下,事件都会触发。在下面的代码中,当 txtCustMSCName(外部 If 语句的 Else 下面的第二个)文本框未通过验证条件时,对话框正确显示,但在 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"/>    
4

2 回答 2

1

问题在于您拥有的DialogMsg 字符串'Custom MSC'。您需要正确地转义每个single quote character,以便生成的 JavaScript格式正确并且可以被 Web 浏览器正确处理。

由于您alert()以单引号开始 JavaScript 函数,因此您必须以另一个单引号结束,因此:

示例 1: alert('hello world');效果很好!

示例 2: alert('hello 'world'');不起作用,实际上它会导致 JavaScript 错误:

SCRIPT1006: 应为 ')'

要解决此问题,您需要转义警报字符串中的每个单引号

示例 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\'

于 2014-06-26T20:11:08.267 回答
0

尝试 Page.ClientScript.RegisterClientScriptBlock() 因为我相信 Page.RegisterClientScriptBlock 在 asp.net 版本中已过时

于 2014-06-26T20:02:29.617 回答