使用asp.net c #ddslick下拉json数据 [英] ddslick dropdown json data with asp.net c#
问题描述
我在网站上搜索但找不到我想要的内容。然后我最后问了一个问题。我的问题是我无法使用来自asp.net c#webmethod的json数据填充ddslick下拉列表。下拉列表显示默认值。但是当我点击下拉列表以查看其他选项时,没有任何反应。我可以'我认为,将数据填入下拉列表。这是我的代码。有人能告诉我我做错了什么吗?组合未开启
<前lang =c#> public class ComboBox
{
private bool _selected = false ;
public ComboBox()
{
}
public string text { get ; set ; }
public string value {获取; set ; }
public bool 已选择
{
get { return _selected; }
set {_ select = value ; }
}
public string description {获得跨度>; set ; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetVehicleComboData()
{
SqlToolkit mySqlKit = new SqlToolkit();
mySqlKit.DoConnect();
DataTable dt = mySqlKit.DoDTstr( 从TBLVEHICLETYPES UNION ALL选择VehicleTypeId,VehicleTypeName,VehicleTypeInfo选择'0' ,'LütfenAraçTipiSeçiniz',''按车型订购');
列表< ComboBox> comboData = new List< ComboBox>();
foreach (DataRow dr in dt.Rows)
{
ComboBox combo = new ComboBox();
combo.text = dr [ VehicleTypeName]。ToString();
combo。 value = dr [ VehicleTypeId 跨度>]的ToString();
combo.description = dr [ VehicleTypeInfo]。ToString();
comboData.Add(combo);
}
var jsonForVehicles = JsonConvert.SerializeObject(comboData,Formatting.Indented);
mySqlKit.DoDisconnect();
return jsonForVehicles;
}
Javascript
< script type =text / javascript>
$ .ajax({
type:'POST',
url:'CreateOffer.aspx / GetVehicleComboData',
data:'{}',
contentType: application / json; charset = utf-8,
dataType:json,
success:function(myData){
$('#demoTruncated')。ddslick({
data:myData,
selectText:选择车型,
truncateDescription:true,
onSelected:function(data){
//回调函数:做一些事情selectedData;
alert(data.selectedIndex);
displaySelectedData(5:带有截断描述的下拉列表,数据);
})
});
},
错误:function(){
}
});
< / script>
我调试了javascript with firebug 和我看到myData对象来自服务器端 如下。我只能看到一个物体。但我认为必须有 4 个对象。因为 4 记录 可以看到 下拉列表。
:TenteliTır , value : 1 ,选择 :false, description : de1 },{ text :KapalıKasaTır , value : 2 ,选择 :false, description : de2 },{ text :KamyonRömorkTır ,值 : 3 ,选择 :false, description : de3 },{ text < span class =code-string> : Kamyon , value : 4 ,选择 :false , description : de4 }]
myData:Object {d = [\\\\ n {\ r \ n text : Lü... ription : de4 \r\\\
} \\\\ nn]}
d: [{ text :LütfenAraçTipiSeçiniz ,值 : 0 ,选择 :false, description :},{ text < span class =code-string>
.ajax({
类型:'POST',
url:'CreateOffer.aspx / GetVehicleComboData',
data:'{}',
contentType:application / json; charset = utf-8,
dataType:json,
success:function(myData){
('#demoTruncated')。ddslick({
数据:myData,
selectText:选择车型,
truncateDescription:true,
onSelected:function(data){
//回调函数:做点什么with selectedData;
alert(data.selectedIndex);
displaySelectedData(5:Dropdown with truncated description,data);
}
});
},
错误:function(){
}
});
< / script>
我使用 firebug调整javascript 和我看到了来自服务器端的的myData对象 喜欢下面。我只能看到一个物体。但我认为必须有 4 个对象。因为 4 记录 可以看到 下拉列表。
myData:Object {d = [\\\\ n {\ r \ n text : Lü... ription : de4 \r\\\
} \\\\ nn]}
d: [{ text :LütfenAraçTipiSeçiniz ,值 : 0 ,选择 :false, description :},{ text < span class =code-string> :TenteliTır , value : 1 ,选择 :false, description : de1 },{ text :KapalıKasaTır , value : 2 ,选择 :false, description : de2 },{ text :KamyonRömorkTır ,值 : 3 ,选择 :false, description : de3 },{ text < span class =code-string> : Kamyon , value : 4 ,选择 :false , description : de4 }]
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title >
< script 类型 = text / javascript src = http://code.jquery.com/jquery-1.9.1.js > < / script >
< script 类型 = text / javascript src = http://code.jquery.com/ui/1.10.3/jquery-ui.js > < / script >
< script 类型 = text / javascript language = javascript >
I searched the site but couldn't find what I exactly looking for. Then I end up with asking a question. My problem is I can't fill the ddslick dropdown with the json data from asp.net c# webmethod.The dropdown list appears with the default value.But when I click the dropdown in order to see other options, nothing happens.I can't fill the data to dropdown I think. Here is my codes. Could someone tell me what am I doing wrong? Combo is not opening
public class ComboBox
{
private bool _selected = false;
public ComboBox()
{
}
public string text { get; set; }
public string value { get; set; }
public bool selected
{
get { return _selected; }
set { _selected = value; }
}
public string description{ get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetVehicleComboData()
{
SqlToolkit mySqlKit = new SqlToolkit();
mySqlKit.DoConnect();
DataTable dt = mySqlKit.DoDTstr("Select VehicleTypeId, VehicleTypeName, VehicleTypeInfo from TBLVEHICLETYPES UNION ALL select '0','Lütfen Araç Tipi Seçiniz','' Order by VehicleTypeId ");
List<ComboBox> comboData = new List<ComboBox>();
foreach (DataRow dr in dt.Rows)
{
ComboBox combo = new ComboBox();
combo.text = dr["VehicleTypeName"].ToString();
combo.value = dr["VehicleTypeId"].ToString();
combo.description = dr["VehicleTypeInfo"].ToString();
comboData.Add(combo);
}
var jsonForVehicles = JsonConvert.SerializeObject(comboData, Formatting.Indented);
mySqlKit.DoDisconnect();
return jsonForVehicles;
}
Javascript
<script type="text/javascript"> $.ajax({ type: 'POST', url: 'CreateOffer.aspx/GetVehicleComboData', data: '{}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (myData) { $('#demoTruncated').ddslick({ data : myData, selectText: "Select a vehicle type", truncateDescription: true, onSelected: function (data) { //callback function: do something with selectedData; alert(data.selectedIndex); displaySelectedData("5: Dropdown with truncated description", data); } }); }, error: function () { } }); </script>
I debug the javascript with firebug and I saw that myData object coming from server side is like below. I can only see one object. But there must be 4 objects, I think. Because I have 4 records to be seen on dropdown list.
myData : Object { d="[\r\n {\r\n "text": "Lü...ription": "de4"\r\n }\r\n]"}
d : "[ { "text": "Lütfen Araç Tipi Seçiniz", "value": "0", "selected": false, "description": "" }, { "text": "Tenteli Tır", "value": "1", "selected": false, "description": "de1" }, { "text": "Kapalı Kasa Tır", "value": "2", "selected": false, "description": "de2" }, { "text": "Kamyon Römork Tır ", "value": "3", "selected": false, "description": "de3" }, { "text": "Kamyon", "value": "4", "selected": false, "description": "de4" } ]"
.ajax({ type: 'POST', url: 'CreateOffer.aspx/GetVehicleComboData', data: '{}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (myData) {
('#demoTruncated').ddslick({ data : myData, selectText: "Select a vehicle type", truncateDescription: true, onSelected: function (data) { //callback function: do something with selectedData; alert(data.selectedIndex); displaySelectedData("5: Dropdown with truncated description", data); } }); }, error: function () { } }); </script>
I debug the javascript with firebug and I saw that myData object coming from server side is like below. I can only see one object. But there must be 4 objects, I think. Because I have 4 records to be seen on dropdown list. myData : Object { d="[\r\n {\r\n "text": "Lü...ription": "de4"\r\n }\r\n]"} d : "[ { "text": "Lütfen Araç Tipi Seçiniz", "value": "0", "selected": false, "description": "" }, { "text": "Tenteli Tır", "value": "1", "selected": false, "description": "de1" }, { "text": "Kapalı Kasa Tır", "value": "2", "selected": false, "description": "de2" }, { "text": "Kamyon Römork Tır ", "value": "3", "selected": false, "description": "de3" }, { "text": "Kamyon", "value": "4", "selected": false, "description": "de4" } ]"
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <script type="text/javascript" language="javascript">
这篇关于使用asp.net c #ddslick下拉json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!