Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to use
jquery.sumoselect.js
to get DropDownList with ComboBox on asp.net page. I put on the top of it
<script src="Scripts/jquery-3.6.0.js"></script>
<script src="Scripts/jquery.sumoselect.js"></script>
<link href="Content/sumoselect.css" rel="stylesheet" />

    <script type="text/javascript">
            $(document).ready(function () {
                $(<%=ddlMINERAL_ADD.ClientID%>).SumoSelect({ okCancelInMulti: true });
        });
    </script>
The page has table, asp.UpdatePanel, asp.repeater and the element
<asp:listbox ID="ddlMINERAL_ADD" runat="server"  selectionmode="Multiple">
is located deep inside the repeater.

I get error
Quote:
Compiler Error CS0103 - variable doesn't exist in this context
.If I move ddlMINERAL_ADD from the repeater - everything is ok.

What I have tried:

So I need "activate" several ddlMINERAL_ADD with javascript code on document.ready event.
Posted
Updated 22-Dec-22 9:21am

1 solution

We need to break apart the one line of code (shown next) and what occurs:

ASP.NET
$(<%=ddlMINERAL_ADD.ClientID%>)


The Razor engine things between <% and %> runs on the Server side.
The $() -- jQuery portion -- runs on the client side.

So, where does ddlMINERAL_ADD.ClientID actually have a value? Is it on the client (jquery / javascript side) or does that variable exist on the Server side (in C# code somewhere)?

If it exists in client code then that is why the Razor rendering engine cannot get the value (it runs on the server side before the client code runs).

I had a username that was on the server side, but I needed client-side javascript to have the value. I wrote up how to do that at this StackOverflow: https://stackoverflow.com/questions/4599169/using-razor-within-javascript/22175868#22175868[^]
 
Share this answer
 
Comments
Member 1314182 22-Dec-22 16:27pm    
Thanks! I guess ddlMINERAL_ADD.ClientID have to be supplied by server. At least I am not going to do that. However, I surprised that this code works as is:
<table>
<asp:listbox ID="ddlMINERAL_ADD"></asp:listbox>
<asp:UpdatePanel><ContentTemplate>
<asp:Repeater>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate></ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate></asp:UpdatePanel>
</table>

but this doesn't due to CS1030:

<table>
<asp:UpdatePanel><ContentTemplate>
<asp:Repeater>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate></ItemTemplate>
<FooterTemplate>
<asp:listbox ID="ddlMINERAL_ADD"></asp:listbox>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate></asp:UpdatePanel>
</table>

In reality I would like to put <asp:listbox id="ddlMINERAL_ADD"> also inside <itemtemplate> in unpredictable quantity.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900