Mitsosp
Mitsosp

Reputation: 399

Executing jQuery script from code-behind throws Exception

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 &= "<script type=""text/javascript"">" & vbCrLf

                script &= "// <![CDATA[" & vbCrLf
                script &= "try {" & vbCrLf
                script &= "$('#<%= " + controlID + ".ClientID %>').attr('disabled', " + disabled + ");" & vbCrLf

                If selectedIndex <> -1 Then
                    control.SelectedIndex = selectedIndex
                    script &= "" + controlID + ".SetValue(" + selectedIndex.ToString() + ");" & vbCrLf
                End If
                script &= "} catch (e) { " & IIf(showErrors, "alert(e.message);", ";") & " }" & vbCrLf
                script &= "// ]]> " & vbCrLf


                script &= "</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 &= "$('#<%= " + controlID + ".ClientID %>').attr('disabled', " + disabled + ");" & vbCrLf

Furthermore, with this being a jQuery command i can assume that the Exception is caused by the "$".

Extra Info:

  1. I have the required jQuery library linked.
  2. I have tried adding the jQuery(document).ready(function () {/.../});
  3. I have tried executing the same script using ScriptManager.
  4. I have tried calling this sub on page_load instead of an earlier phase of page rendering
  5. I have also tried linking the jQuery library within this script block.

That's all for now. Thanks in advance.

Upvotes: 0

Views: 213

Answers (1)

Jamie Burns
Jamie Burns

Reputation: 1318

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.

Upvotes: 1

Related Questions