Hi,
I have a listbox in my project that listing some values which i'm pulling from the database. The values have parent I'ds associated with them and are listed in listbox like below:
UK(parent)
London(child)
Manchester(Child)
USA(parent)
New York(child)
Florida(child)
What I would like to do is that when a user select a child then the parent should be auto select. i.e when you select London , I want UK to be auto selected. And also i would like to do this without the page being posted back so that why i want to do it in JavaScript/ jquery
Please see what i have done so far below:
What I have tried:
Markup
<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple" CssClass="inputText" AutoPostBack="false" >
Javascript
This is the way i'm getting the child value as Id:
$(document).ready(function () {
var select = $("#<%=lblMultiSelect.ClientID%>");
var txt = $("#<%=listBoxValue.ClientID%>");
select.change(function (e) {
var result = "";
var options = select.children("option:selected");
$.each(options, function (i, el) {
result += $(el).val();
if (i < options.length - 1) {
result += ",";
}
});
txt.val(result);
});
})
//Code behind
This is where i'm stuck.. i mean i know how to query the db using c# but i'm just not sure on how i can pass the Id from javascript function into a c# method.
Do i need to have something like this:
public void GetParent (int childID)
{
var p = dbcontext.Table.Where(x => x.Id == childId);
}
How would i go about in doing this? i hope this makes sense
and thank you for your time