简体   繁体   中英

ScriptManager.RegisterClientScriptBlock not displaying dialogbox under certain conditions

I have a couple of textboxes inside an updatepanel for which I'm doing some simple validation. If the validation check fails, an error message pops up in a dialog box. My problem is that the RegisterClientScriptBlock displays the dialog box when the validation for one of the textboxes fails but not the other. In both cases the event is firing. In the below code, the dialog box displays correctly when the txtCustMSCName (the second one below under the Else of the outer If statement) textbox fails the validation criteria but does not display when the txtMSCName textbox fails. Any ideas why this could be happening? Does it have something to do with the fact that txtMSCName is set to 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

Markup:

<asp:TextBox ID="txtMSCName" runat="server" ReadOnly="true" CssClass="DisplayTextBoxStyle"/>
<asp:TextBox ID="txtCustMSCName" runat="server" CssClass="MSCInputTextBoxStyle"/>    

The problem is with DialogMsg string where you have 'Custom MSC' . You need to properly escape every single quote character so that the resulting JavaScript is well formed and can be correctly processed by the Web-Browser.

Since you start the alert() JavaScript function with a single quote, you must then end with another single quote, so:

Example 1: alert('hello world'); works great!

Example 2: alert('hello 'world''); will not work, in-fact it will result in a JavaScript error:

SCRIPT1006: Expected ')'

To fix this you need to ESCAPE every single quote inside the alert string .

Example 2 should be: alert('hello \\'world\\'');

So to help you with your question, you need to change from this:

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

To this:

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

Notice the \\' Custom MSC \\'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM