找回密码
 立即注册

QQ登录

只需一步,快速开始

iceman

社区贡献组

270

主题

1万

帖子

1万

积分

社区贡献组

积分
19311

活字格认证微信认证勋章元老葡萄

iceman
社区贡献组   /  发表于:2012-7-24 12:12  /   查看:5710  /  回复:0
Wijmo Combobox 可以在 Server 端绑定数据源。但是,Wijmo Combobox 是一款基于 jQuery 的控件。所以我们可以再客户端绑定Wijmo Combobox 数据源。
在本文中,我们将使用 WebService 去实现该功能。
考虑到某些实际应用场景,我们将提供一个Wijmo Combobox提供用户选择国家名称,在这个基础上绑定第二个 Wijmo Combobox 来显示城市信息。在本示例中,我们使用 UpdatePanel 去实现局部刷新。
首先,我们在 Server 端为第一个 Wijmo Combobox 绑定数据源来展示用户国家信息。
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     //bind the combobox to country column
  4.     string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString;
  5.     string queryStudio = "SELECT DISTINCT [Country] FROM [Customers]";
  6.          
  7.     OleDbDataAdapter da = new OleDbDataAdapter(queryStudio, strConn);
  8.     DataTable dtCountries = new DataTable();
  9.     da.Fill(dtCountries);
  10.   
  11.     C1ComboBox1.DataTextField = "Country";
  12.     C1ComboBox1.DataSource = dtCountries;
  13.     C1ComboBox1.DataBind();
  14. }
复制代码

现在,我们将在 WebService创建 WebMethod 来取得第一个 Wijmo Combobox 中的选项信息。因为,jQuery无法直接从 webservice中取得 datatable。我们将创建List<T>来传输数据。

  1. [WebMethod]
  2. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  3. public object GetCityNames(string countryName)
  4. {
  5.    //retrieve the data
  6.    DataTable ds = new DataTable();
  7.    string queryProduct = "SELECT DISTINCT [City] FROM [Customers] where Country='" + countryName + "'";
  8.    string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString;
  9.    OleDbDataAdapter da = new OleDbDataAdapter(queryProduct, strConn);
  10.    DataTable dtCity = new DataTable();
  11.    da.Fill(dtCity);
  12.   
  13.    //create list of CityNames
  14.    List<CityNames> cityNames = new List<CityNames>();
  15.    for (int i = 0; i < dtCity.Rows.Count; i++)
  16.    {
  17.    //set the values
  18.    CityNames cn = new CityNames();
  19.    cn.CityName = dtCity.Rows<EM></EM>["City"].ToString();
  20.    cn.CityId = i;
  21.    cityNames.Add(cn);
  22.    }
  23.    return cityNames;
  24. }

  25. “CityNames”的结构如下,
  26. public class CityNames
  27. {
  28.    public string CityName
  29.    {
  30.       get;
  31.       set;
  32.    }
  33.   
  34.    public int CityId
  35.    {
  36.       get;
  37.       set;
  38.    }
  39. }
复制代码


因为,我们的操作都需要在客户端完成,我们可以在 Wijmo Combobox 的 OnClientSelect 事件中调研 WebService。
  1. <script type="text/javascript">
  2. function getSelectedItem(e, item) {
  3.     var cmb_Product = $("#C1ComboBox2");
  4.     cmb_Product.c1combobox({
  5.       data: null
  6.     });
  7.   
  8.     //call the service
  9.     GetCityNamesFromService(item.label);
  10. }
  11.   
  12. function GetCityNamesFromService(country) {
  13.    //ajax call to get the data
  14.    $.ajax({
  15.      type: "POST",
  16.      url: "GetDetails.asmx/GetCityNames",
  17.      contentType: "application/json; charset=utf-8",
  18.      dataType: "json",
  19.       //pass the country name
  20.      data: "{countryName:'" + country + "'}",
  21.      success: function (data) {
  22.             var arr = [];
  23.             try {
  24.             //push the data in an array
  25.                  $.each(data.d, function (i, elem) {
  26.                      arr.push({
  27.                           label: elem.CityName,
  28.                           value: elem.CityId
  29.                      });
  30.                   });
  31.                 FillComboWithData(arr);
  32.               }
  33.             catch (e) {
  34.                 alert(e);
  35.                 return;
  36.               }
  37.            },
  38.          error: function (result, status) {
  39.                 if (status = "error") {
  40.                    alert(status);
  41.            }
  42.        }
  43.    });
  44. }
  45.   
  46. function FillComboWithData(productArray) {
  47.        var cmb_Product = $("#C1ComboBox2");
  48.        //set the datasource of the combobobox
  49.        cmb_Product.c1combobox({
  50.            data: productArray
  51.        });
  52.    }
  53. </script>
复制代码

截图展示:
初始运行画面:

选择国家:

选择城市:

你也试一试?
Demo下载:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部