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
DialogMsgstring where you have'Custom MSC'. You need to properly escape everysingle quote characterso 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: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:
To this:
Notice the
\'Custom MSC\'