Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update jQuery DropdownChecklist from server-side?

I'm working on an ASP.Net webpage which will use the jQuery Dropdown Checklist (http://code.google.com/p/dropdown-check-list/). I'm fairly inexperienced with JavaScript and completely new to jQuery.

Basically, my question is: How do you update the selected items in a jQuery DropdownChecklist from the server-side?


NOTE: For details on the solution, scroll to the bottom of the question.


Here's some background to give you a better idea of what I'm doing...

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.

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.

Here's some relevant code snippets:

Here's the markup...

<asp:ScriptManager ID="smScriptMgr" runat="server" />
<table>
     <tr>
          <td>
               <select id="s1" multiple="true" runat="server" />
          </td>
     </tr>
     <tr>
          <td>
               <asp:UpdatePanel ID="UP1" runat="server">
                    <ContentTemplate>
                         <input id="inpHide" type="hidden" runat="server" />
                         <uc1:SelectedFilterBox ID="sfbFilters" runat="server" Width="200" />
                    </ContentTemplate>
               </asp:UpdatePanel>
          </td>
     </tr>
</table>

Here's the JavaScript...

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();
 });
});

Here's the markup of the usercontrol...

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SelectedFilterBox.ascx.vb"
    Inherits="SelectedFilterBox.SelectedFilterBox" %>

<div>
    <asp:Table ID="tblFilters" runat="server" Width="200">
        <asp:TableRow>
            <asp:TableCell>
                <asp:Repeater ID="rpFilters" runat="server" Visible="false">
                    <ItemTemplate>
                        <table class="selectedFilter">
                            <tr>
                                <td class="selectedFilterLeft">
                                    <%# Eval("filterName")%>
                                </td>
                                <td class="selectedFilterRight" align="right">
                                    <asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='<%#Eval("filterName") %>' OnCommand="ibRemove_Click" />
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
</div>

Here's some relevant portions of the code-behind...

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

Here's a JavaScript function that does what I want...

function DeselectFilter(targetString){
  $("#s1 option").each(function(){
      if($(this).text() === targetString) $(this).attr("selected", "");
  });
  $("#s1").dropdownchecklist("refresh");
};

However, I'm not sure I'm calling it right from the onClientClick event. Does this look right?

<asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='<%#Eval("filterName") %>' OnCommand="ibRemove_Click" OnClientClick='DeselectFilter("<%#Eval("filterName") %>");' />

After some back-and-forth in the comments, here's the solution that worked...

<a onclick='DeselectFilter("<%#Eval("filterName") %>");'>
   <asp:ImageButton ID="ibRemove" runat="server" ImageUrl="~/images/delete.gif" CommandArgument='<%#Eval("filterName") %>' OnCommand="ibRemove_Click" />
</a>

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.

Thank you, Coding Gorilla, for all your help.

like image 957
ks78 Avatar asked Nov 10 '10 15:11

ks78


1 Answers

So through a long back-and-forth through the comments, the solution turned out to be to move the deselection process to the client side, and use javascript and jquery to deselect the item when an control is clicked.

like image 104
CodingGorilla Avatar answered Oct 22 '22 21:10

CodingGorilla