Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C#
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>


SQL
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" } ]"
Posted

1 solution

XML
<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">
    $(function() {
    $.ajax({
        type: 'POST',
        url: '/Webservice/MyService/FillDropDown',
        cache: false,
        async:false,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(data) {
            $('#ddslick').html(data.d);
        }
    });
    });

    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <select id="ddslick"></select>
    </div>
    </form>
</body>
</html>



Now the Webservice.....

VB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
 [System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService {


    [WebMethod]
    public string FillDropDown()
    {
        string _str = string.Empty;
        MyClass obj = new MyClass();
       _str= obj.FillDropDown();
       return _str;
    }

}




Now, the class code called...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

public class MyClass
{
string con = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string _str = string.Empty;

public string FillDropDown()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (SqlConnection conn = new SqlConnection(con))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "proc_GetddlData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
sb.Append("<option>" + dr[0] + "</option>");
}
_str = sb.ToString();
}
catch (Exception ex)
{
throw ex;
}

}
return _str;
}
}

Into the SP ,you only need to get the dropdown values .....
Try and Run, hope your problem will be solved.
 
Share this answer
 
Comments
altanalansu 11-Dec-13 7:40am    
Thank you Jain. I constructed the dropdown with the code you gave.
Jain Nishant 11-Dec-13 23:44pm    
If you liked it... plz vote for me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900