Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update jQuery DropdownChecklist from server-side?
    primarykey
    data
    text
    <p>I'm working on an ASP.Net webpage which will use the jQuery Dropdown Checklist (<a href="http://code.google.com/p/dropdown-check-list/" rel="nofollow noreferrer">http://code.google.com/p/dropdown-check-list/</a>). I'm fairly inexperienced with JavaScript and completely new to jQuery.</p> <p>Basically, my question is: <strong>How do you update the selected items in a jQuery DropdownChecklist from the server-side?</strong></p> <hr> <p><strong>NOTE:</strong> For details on the solution, scroll to the bottom of the question.</p> <hr> <p>Here's some background to give you a better idea of what I'm doing...</p> <p>I have a jQuery DropdownChecklist which is being populated when the page loads. When a user selects an item in the DropdownChecklist, the selected values are collected and stored in a hidden input field, then a postback is performed, which allows the server to update a server control. This part is working.</p> <p>Now, here's my problem. This server control, which is actually a usercontrol has a "Remove" button for each selected item in the DropdownChecklist. When a "Remove" button is clicked, it should cause the associated item in the jQuery DropdownChecklist to be de-selected. So, far I haven't figured out how to make that happen.</p> <p>Here's some relevant code snippets:</p> <p>Here's the markup...</p> <pre><code>&lt;asp:ScriptManager ID="smScriptMgr" runat="server" /&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;select id="s1" multiple="true" runat="server" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:UpdatePanel ID="UP1" runat="server"&gt; &lt;ContentTemplate&gt; &lt;input id="inpHide" type="hidden" runat="server" /&gt; &lt;uc1:SelectedFilterBox ID="sfbFilters" runat="server" Width="200" /&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Here's the JavaScript...</p> <pre><code>function DoPostback() { __doPostBack('UP1', ''); }; $(function () { $("#s1").dropdownchecklist({ forceMultiple: true, width: 200, textFormatFunction: function() { return "Filters:"; } }); $('#s1').change(function () { var values = $(this).val(); document.getElementById("inpHide").value = values; DoPostback(); }); }); </code></pre> <p>Here's the markup of the usercontrol...</p> <pre><code>&lt;%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SelectedFilterBox.ascx.vb" Inherits="SelectedFilterBox.SelectedFilterBox" %&gt; &lt;div&gt; &lt;asp:Table ID="tblFilters" runat="server" Width="200"&gt; &lt;asp:TableRow&gt; &lt;asp:TableCell&gt; &lt;asp:Repeater ID="rpFilters" runat="server" Visible="false"&gt; &lt;ItemTemplate&gt; &lt;table class="selectedFilter"&gt; &lt;tr&gt; &lt;td class="selectedFilterLeft"&gt; &lt;%# Eval("filterName")%&gt; &lt;/td&gt; &lt;td class="selectedFilterRight" align="right"&gt; &lt;asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='&lt;%#Eval("filterName") %&gt;' OnCommand="ibRemove_Click" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; &lt;/asp:TableCell&gt; &lt;/asp:TableRow&gt; &lt;/asp:Table&gt; &lt;/div&gt; </code></pre> <p>Here's some relevant portions of the code-behind...</p> <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then 'populate dropdown checklist s1.Items.Add("Filter A") s1.Items.Add("Filter B") s1.Items.Add("Filter C") Else 'update filters based on contents of hidden input field UpdateFilters(inpHide.Value) End If End Sub Private Sub UpdateFilters(ByVal filters As String) Dim Filter() As String = Split(filters, ",") Dim Index As Integer = 0 'clear existing filters sfbFilters.Clear() 'loop through adding filters based on supplied string For Each str As String In Filter sfbFilters.Add(str, Index.ToString()) Index += 1 Next str End Sub 'this event occurs when a Remove button is clicked Protected Sub sfbFilters_FilterChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles sfbFilters.FilterChanged 'update hidden input field inpHide.Value = UpdateHiddenField() 'update label, listing (filter, value) pairs UpdateFilterValueSets() End Sub </code></pre> <p>Here's a JavaScript function that does what I want...</p> <pre><code>function DeselectFilter(targetString){ $("#s1 option").each(function(){ if($(this).text() === targetString) $(this).attr("selected", ""); }); $("#s1").dropdownchecklist("refresh"); }; </code></pre> <p>However, I'm not sure I'm calling it right from the onClientClick event. Does this look right?</p> <pre><code>&lt;asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='&lt;%#Eval("filterName") %&gt;' OnCommand="ibRemove_Click" OnClientClick='DeselectFilter("&lt;%#Eval("filterName") %&gt;");' /&gt; </code></pre> <p>After some back-and-forth in the comments, here's the solution that worked...</p> <pre><code>&lt;a onclick='DeselectFilter("&lt;%#Eval("filterName") %&gt;");'&gt; &lt;asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='&lt;%#Eval("filterName") %&gt;' OnCommand="ibRemove_Click" /&gt; &lt;/a&gt; </code></pre> <p>We determined the simplest solution was to call the JavaScript function from the client-side rather than try to do it from the server-side. The ImageButton does have an onClientClick event, but for some reason it didn't work. However, wrapping the ImageButton in a client-side anchor tag and using its onclick event did the charm.</p> <p>Thank you, <a href="https://stackoverflow.com/users/228963/coding-gorilla">Coding Gorilla</a>, for all your help.</p>
    singulars
    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.
 

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