Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to assign C# variable to asp element using javascript
    primarykey
    data
    text
    <p>I want to assign a csharp variable to the element of asp page using javascript. It seems the assignement is not working in my code. </p> <p>Here it is: </p> <pre><code>document.getElementById('lbAccessories').innerHTML = '&lt;%#SelectLabel%&gt;'; </code></pre> <p>In my asp code i use:</p> <pre><code>&lt;asp:LinkButton ID="lbAccessories" runat="server" /&gt;' </code></pre> <p>I can't assign the value directly to the linkbutton using <code>Text='&lt;%#SelectLabel%&gt;'</code> because i want to make it more intelligent. </p> <p>Does anyone knows how to do with that? </p> <p>Thanks</p> <p><strong>Edit:</strong> </p> <p>Here is my code, I've tried to use <code>&lt;%=lbAccessories.clientId%&gt;</code>, but it generates an error : lbAccessories does not exist in the context.</p> <pre><code>&lt;script type="text/javascript"&gt; function function(Ref) { if ('&lt;%=TextBoxClientID%&gt;' == 'txtLink') { document.getElementById('lbAccessories').innerHTML = '&lt;%#SelectLabel%&gt;'; } else if ('&lt;%=TextBoxClientID%&gt;' == 'btnSearch') { document.getElementById('lbAccessories').innerHTML = '&lt;%#ViewDetail%&gt;'; } } &lt;/script&gt; </code></pre> <p><strong>Edit:</strong></p> <pre><code>&lt;asp:GridView ID="gv1" AutoGenerateColumns="false" runat="server" AllowPaging="true" OnPageIndexChanging="pic"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=""&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID="lbAccessories" runat="server" OnClientClick='&lt;%#string.Format("passAccessory(\"{0}\");", Eval("Ref"))%&gt;'&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; </code></pre> <p>Edit:</p> <p>Thanks so much for everyone who had posted the suggestions. </p> <p>I have changed the javascript code to the following:</p> <pre><code>&lt;script type="text/javascript"&gt; function passAccessory(accessoryRef) { if ('&lt;%=TextBoxClientID%&gt;' == 'txtLink') { document.getElementById('&lt;%= gvAccessories.FindControl("lbAccessories").ClientID %&gt;').innerHTML = '&lt;%#SelectLabel%&gt;'; } else if ('&lt;%=TextBoxClientID%&gt;' == 'btnSearch') { document.getElementById('&lt;%= gvAccessories.FindControl("lbAccessories").ClientID %&gt;').innerHTML = '&lt;%#ViewDetail%&gt;'; } } &lt;/script&gt; </code></pre> <p>in <code>*.aspx.cs</code> file, i have added: <code>protected LinkButton lbAccessories { get; set; }</code> </p> <p>It throws an exception: </p> <blockquote> <p>Object reference not set to an instance of an object</p> </blockquote> <p>Anyone have ideas? Thanks so much</p> <p><strong>Edit:</strong></p> <p>Finally, we did it with csharp code. It's more flexable and manageable. My colleague helped me. Thank you all for your help!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload