0

I am developing one asp.net application. I have implemented asp.net dropdownlistbox with jquery multiselect option. users can select more than one values in dropdownboxlist. I want these selected values in server side on buttonclick event. I am getting selected all values in javascript. I am trying to get all the values inside button click event. Other than ajax is there any way to access multiple selected values in server side? Any help would be appreciated. This is my dropdownbox.

 <h3>Awarded To:&nbsp;<asp:DropDownList ID="ddlvendors" CssClass="limitedNumbSelect2" Multiple="True" runat="server" Width="30%"></asp:DropDownList></h3>

I am binding values in server side on page load event.

This is my previous code.

$(".limitedNumbSelect2 option").each(function () {
                var val = $(this).val();
                var tempVal = $(".limitedNumbSelect2").val();
                if (tempVal.indexOf(val) >= 0 && selected.indexOf(val) < 0) {
                    selected.push(val);
                } else if (tempVal.indexOf(val) < 0 && selected.indexOf(val) >= 0) {
                    selected.splice(selected.indexOf(val), 1);
                }

            })

This is the code to enable all the checkboxes based on selected values from dropdownlist.

 $('#<%= gdvRegretletter.ClientID %> input[type="hidden"]').each(function () {
   $(this).closest('tr').find('input[type="checkbox"]').prop('disabled', false);
});
6
  • If you're using mvc then post back using ajax Commented Jan 2, 2017 at 13:39
  • get the values using jquery then send them over to server using ajax Commented Jan 2, 2017 at 13:39
  • Thank you for your response. I am inside sharepoint visual web part. My ajax calls are not working because I am using webparts. So other than ajax I am trying. Commented Jan 2, 2017 at 13:40
  • You can set values in hidden field using jquery and access on server side. Commented Jan 2, 2017 at 13:41
  • Thanks sandeep. Can i get some related example please... Commented Jan 2, 2017 at 13:42

1 Answer 1

2

Create a ListBox like below:

<asp:ListBox ID="lstFruits" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Mango" Value="1" />
    <asp:ListItem Text="Apple" Value="2" />
    <asp:ListItem Text="Banana" Value="3" />
    <asp:ListItem Text="Guava" Value="4" />
    <asp:ListItem Text="Orange" Value="5" />
</asp:ListBox>
<asp:Button Text="Submit" runat="server" OnClick="Submit" />

Add following items:

  1. jQuery JS file
  2. Bootstrap JavaScript and CSS files.
  3. jQuery BootStrap Multi-Select Plugin JavaScript and CSS files.

which would be like this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"
    rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=lstFruits]').multiselect({
            includeSelectAllOption: true
        });
    });
</script>

Following is the Button click event handler, inside which a loop is executed over the ListBox Items and its Selected property is checked. If it returns True then the Item was selected and if False then the Item was not selected.

protected void Submit(object sender, EventArgs e)
{
    string message = "";
    foreach (ListItem item in lstFruits.Items)
    {
        if (item.Selected)
        {
            message += item.Text + " " + item.Value + "\\n";
        }
    }
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
}
Sign up to request clarification or add additional context in comments.

6 Comments

I am al most implemented as above. Binded values from database also. I am getting error near clientscript. Error says the name clientcontext does not exist in the current context. May i know what is should include here? Thank you
could u edit your question with what've you wrote in your code-behind? so i get a better idea?
Your code works fine. I am able to get required values in server side. Actually i wanted multiselect dropdown box list.
@NiranjanGodbole Its a multiselect dropdownlist mate, whats the issue?
Hi Valkyriee... Code is working fine. Previously i had multiselect dropdownlist. I will post the code above. I have one gridview as well below dropdownlistbox. Whenever i select one value from dropdownlist corresponding value from gridview(with checkbox) I am disabling. Now enabling not happening. When i uncheck the any value from listbox i would like to enable the checkbox in the gridview.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.