Executing jQuery script from code-behind throws Exception-Collection of common programming errors
I have a problem with the following code block:
Dim scriptHere As System.Web.UI.HtmlControls.HtmlGenericControl = control.Page.FindControl("scriptHere")
Dim controlID As String = ""
Dim disabled As String = IIf(isDisabled, "true", "false")
If TypeOf (control) Is ASPxEditBase Then
controlID = control.ID
End If
Try
control.ReadOnly = isDisabled
Dim script As String = ""
script &= "" & vbCrLf
script &= "// " & vbCrLf
script &= "" & vbCrLf
scriptHere.InnerHtml &= script
Catch ex As Exception
Console.Out.WriteLine(ex.ToString())
End Try
The problem is, that it throws me an Exception. To be more precise it just says “Undefined” and nothing else. I figured that the problem lies within this line:
script &= "$('#').attr('disabled', " + disabled + ");" & vbCrLf
Furthermore, with this being a jQuery command i can assume that the Exception is caused by the “$”.
Extra Info:
- I have the required jQuery library linked.
- I have tried adding the
jQuery(document).ready(function () {/.../});
- I have tried executing the same script using ScriptManager.
- I have tried calling this sub on page_load instead of an earlier phase of page rendering
- I have also tried linking the jQuery library within this script block.
That’s all for now. Thanks in advance.
-
Ok, see if this would work –
script &= "$('#" + controlID.ClientID + "').attr('disabled', " + disabled + ");" & vbCrLf
It looks like the inline VB isn’t pulling out the correct client id, but you’ve already got it server-side, so you should be able to build the entire script string up in one go.