如何从ASP.NET webforms中的javascript / jquery触发服务器端代码 [英] How to triggger server side code from javascript/jquery in ASP.NET webforms
问题描述
我的项目中有一个列表框,列出了我从数据库中提取的一些值。这些值有父IDI与它们相关联,并列在下面的列表框中:
英国(父母)
伦敦(儿童)
曼彻斯特(儿童)
美国(父母)
纽约(儿童)
佛罗里达(儿童)
我想做的是当用户选择一个孩子时,父母应该是自动选择。即当你选择伦敦时,我希望自动选择英国。而且我想在没有回页的情况下这样做,这就是为什么我想用JavaScript / jquery来做
请看看到目前为止我做了什么下面:
我尝试过:
Markup
< asp:ListBox runat = < span class =code-keyword> server ID = lblMultiSelect SelectionMode = multiple CssClass = inputText AutoPostBack = false >
Javascript
这就是我的方式m得到孩子的值为Id:
$( document )。ready( function (){
var select = $( #<%= lblMultiSelect.ClientID%>);
var txt = $( #< ;%= listBoxValue.ClientID%>中跨度>); // <<只是为了测试目的将id放到文本框中
select.change( function (e){
var result = ;
var options = select。 children( 选项:选中);
$ .each(options, function (i,el){
result + = $(el).val();
if (i< options.length - 1 ){
result + = ,;
}
});
txt.val(result);
});
})
//代码落后
这个是我卡住的地方..我的意思是我知道如何使用c#查询数据库,但我不知道如何将Id从javascript函数传递到ac#方法。
Do我需要这样的东西:
public void GetParent( int childID)
{
var p = dbcontext.Table.Where(x = > x.Id == childId);
// 然后在此处自动选择父级而不回发页面?
}
我将如何做到这一点?我希望这是有道理的
并感谢您的时间
( document )。ready( function (){
var select =
( #<%= lblMultiSelect.ClientID%> ;);
var txt =
( #<%= listBoxValue.ClientID%>); // <<只是为了测试目的将id放到文本框中
select.change(函数(e){
var result = ;
var options = select.children( option:selected 跨度>);
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%>"); //<<just putting id to a textbox here for testing purposes
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);
//then auto select the parent here without posting the page back?
}
How would i go about in doing this? i hope this makes sense
and thank you for your time
(document).ready(function () { var select =
("#<%=lblMultiSelect.ClientID%>"); var txt =
("#<%=listBoxValue.ClientID%>"); //<<just putting id to a textbox here for testing purposes select.change(function (e) { var result = ""; var options = select.children("option:selected");
这篇关于如何从ASP.NET webforms中的javascript / jquery触发服务器端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!