locked
Not Getting Checked Items RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I am using Bootstrap Multiselect . I have selected 2 items but it is not going in loop

    $('[id*=ddlLocation]').multiselect({
    includeSelectAllOption: true
    });

    ListBox lst = (ListBox)row.FindControl("ddlLocation");
    foreach (ListItem item in lst.Items)
    {
    if (item.Selected)
    {
    strLocation += item.Text + ",";
    }
    }

    <asp:TemplateField HeaderText = "Location">
    <ItemTemplate>
    <asp:ListBox ID="ddlLocation" SelectionMode="multiple" Enabled="false" runat="server"></asp:ListBox>
    </ItemTemplate>
    </asp:TemplateField>

    Thanks

    Sunday, August 26, 2018 4:02 PM

All replies

  • User409696431 posted

    When you say "it is not going in loop", what do you mean?  Do you mean it is not finding the control "ddLocation", or do you mean it is not finding any Listitems in it, or do you mean it's not finding that any of the items are Selected?

    Sunday, August 26, 2018 4:59 PM
  • User-1499457942 posted

    Hi

     it's not finding that any of the items are Selected

    Thanks

    Sunday, August 26, 2018 5:01 PM
  • User409696431 posted

    Well, I don't think a bootstrap multiselect is a server side control that would have asp.net recognized listitems.   Are you sure it is finding the listitems, at all?  I think you have to use JQuery to fetch the values and ajax send them to the server.

    Sunday, August 26, 2018 5:19 PM
  • User-1499457942 posted

    Hi

      I am trying like below code . will it not work

    <asp:TemplateField HeaderText = "Location">
    <ItemTemplate>
    <asp:ListBox ID="ddlLocation" SelectionMode="multiple" Enabled="false" runat="server"></asp:ListBox>
    </ItemTemplate>
    </asp:TemplateField>

    Thank

    Sunday, August 26, 2018 5:23 PM
  • User409696431 posted

    What does a bootstrap <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">mulitselect</g> have to do with a ListBox?  You are conflating two different things in your posts.

    Sunday, August 26, 2018 8:11 PM
  • User-1499457942 posted

    Hi

       I have changed it ti normal ListView. It is taking space of 4 items . Is it possible that it shows only 1 Item . I want that ListBox should not take more space.

    When clicked then it should  show the list.

    Thanks

    Monday, August 27, 2018 4:50 AM
  • User-1171043462 posted

    In which event you are checking?

    And have you populated GridView inside !IsPostBack condition?

    Monday, August 27, 2018 3:38 PM
  • User-893317190 posted

    Hi JagjitSingh,

    You could choose a workaround to get all the selected checkboxes and save their value in a hiddenfield to pass to the server.

    Below is my code.

    <title></title>
         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" >
         <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" >
        <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
         
         <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" ></script>
    
       <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>
        
       
    </head>
    <body>
        <form id="form1" runat="server">
          <div class="row">  
            <div class="col-lg-12 "> 
                <div class="table-responsive">
            <asp:GridView ID="gvwCategories"  runat="server"
                     CssClass="table table-bordered table-striped" AutoGenerateColumns="false" 
                        ClientIDMode="Static" BorderWidth="1px"  OnRowCommand="gvwCategories_RowCommand"
                        BorderStyle="None" BorderColor="#DEBA84" HeaderStyle-CssClass="GridHeader" 
                        EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" 
                     >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        
                       <asp:TemplateField HeaderText="Code">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblCode" Text='<%#Eval("Code") %>'></asp:Label>
                                <asp:HiddenField ID="hdfEntryNo" runat="server" Value='<%# Bind("EntryNo") %>' />
                         
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Description">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblDescription" Text='<%#Eval("Description") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="Category">
                            <ItemTemplate>
                                                                
                                <asp:HiddenField ID="HiddenField1" runat="server" />
                 <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="category" DataValueField="category" SelectionMode="Multiple" ></asp:ListBox>
    
              <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EntityExe %>" SelectCommand="SELECT [id], [category] FROM [category]"></asp:SqlDataSource>
    
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                                <asp:linkbutton id="Linkbutton1" Text="Edit"  ToolTip="Update Record" CommandName="EditRow" runat="server" CssClass="btn btn-primary"/> 
                            </ItemTemplate>
                        </asp:TemplateField>
    
                    </Columns>
                    </asp:GridView>
                       </div>
            </div>
    
            
    
            <script type="text/javascript">
        $(document).ready(function() {
            $("[id*=ListBox1]").multiselect({
    
                includeSelectAllOption: true,
                onChange: function (option, checked, select) {
                    //get the hidden field
                    var $hidden = option.parent().parent().parent().find("input[type=hidden]");
                    var str = "";
                    //get the checkboxes
                    option.parent().parent().find("div ul input:gt(0)")
                        .each(function () {
                            if ($(this).prop("checked")) {
                                str += $(this).val() + ",";
                            }
    
                        })
                   // set the hiddenfield's value
                    $hidden.val(str);
                
                },
                onDeselectAll: function () {
                    $(this).get(0).$select.parent().parent().find("input[type=hidden]").val("");
                },
                onSelectAll: function () {
                   var $hidden= $(this).get(0).$select.parent().parent().find("input[type=hidden]")
                    var str = "";
                    $(this).get(0).$container.find("input:gt(0)")
                        .each(function () {
                           
                                str += $(this).val() + ",";
                           
                        })
                    $hidden.val(str);
    
            }
            })
        });
    </script>

    Code behind.

     protected void gvwCategories_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "EditRow")
                {
                    GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                    HiddenField hdf = (row.FindControl("HiddenField1") as HiddenField);
    
                    Response.Write((row.FindControl("lblCode") as Label).Text + ":" + hdf.Value);
                }
    
            }

    The result.

    Best regards,

    Ackerly Xu

    Tuesday, August 28, 2018 9:48 AM