{"id":1961,"date":"2022-08-30T15:20:53","date_gmt":"2022-08-30T15:20:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/05\/executing-jquery-script-from-code-behind-throws-exception-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:53","modified_gmt":"2022-08-30T15:20:53","slug":"executing-jquery-script-from-code-behind-throws-exception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/executing-jquery-script-from-code-behind-throws-exception-collection-of-common-programming-errors\/","title":{"rendered":"Executing jQuery script from code-behind throws Exception-Collection of common programming errors"},"content":{"rendered":"<p>I have a problem with the following code block:<\/p>\n<pre><code>    Dim scriptHere As System.Web.UI.HtmlControls.HtmlGenericControl = control.Page.FindControl(\"scriptHere\")\n            Dim controlID As String = \"\"\n            Dim disabled As String = IIf(isDisabled, \"true\", \"false\")\n            If TypeOf (control) Is ASPxEditBase Then\n                controlID = control.ID\n            End If\n            Try\n                control.ReadOnly = isDisabled\n\n                Dim script As String = \"\"\n\n                script &amp;= \"\" &amp; vbCrLf\n\n                script &amp;= \"\/\/  \" &amp; vbCrLf\n\n\n                script &amp;= \"\" &amp; vbCrLf\n\n\n                scriptHere.InnerHtml &amp;= script\n            Catch ex As Exception\n                Console.Out.WriteLine(ex.ToString())\n            End Try\n<\/code><\/pre>\n<p>The problem is, that it throws me an Exception. To be more precise it just says &#8220;Undefined&#8221; and nothing else. I figured that the problem lies within this line:<\/p>\n<pre><code>script &amp;= \"$('#').attr('disabled', \" + disabled + \");\" &amp; vbCrLf\n<\/code><\/pre>\n<p>Furthermore, with this being a jQuery command i can assume that the Exception is caused by the &#8220;$&#8221;.<\/p>\n<p>Extra Info:<\/p>\n<ol>\n<li>I have the required jQuery library linked.<\/li>\n<li>I have tried adding the <code>jQuery(document).ready(function () {\/...\/});<\/code><\/li>\n<li>I have tried executing the same script using ScriptManager.<\/li>\n<li>I have tried calling this sub on page_load instead of an earlier phase of page rendering<\/li>\n<li>I have also tried linking the jQuery library within this script block.<\/li>\n<\/ol>\n<p>That&#8217;s all for now. Thanks in advance.<\/p>\n<ol>\n<li>\n<p>Ok, see if this would work &#8211;<\/p>\n<p><code>script &amp;= \"$('#\" + controlID.ClientID + \"').attr('disabled', \" + disabled + \");\" &amp; vbCrLf<\/code><\/p>\n<p>It looks like the inline VB isn&#8217;t pulling out the correct client id, but you&#8217;ve already got it server-side, so you should be able to build the entire script string up in one go.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-05 16:32:13. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a problem with the following code block: Dim scriptHere As System.Web.UI.HtmlControls.HtmlGenericControl = control.Page.FindControl(&#8220;scriptHere&#8221;) Dim controlID As String = &#8220;&#8221; Dim disabled As String = IIf(isDisabled, &#8220;true&#8221;, &#8220;false&#8221;) If TypeOf (control) Is ASPxEditBase Then controlID = control.ID End If Try control.ReadOnly = isDisabled Dim script As String = &#8220;&#8221; script &amp;= &#8220;&#8221; &amp; vbCrLf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1961","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1961"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1961\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}