技术开发 频道

无刷新三级连动用户控件


【IT168技术文档】

  1、AjaxPro的ajax框架

  不是我不想用微软的asp.net ajax框架来实现,只是据说udatePanel是一种局部显示、但仍然是整页回传到局部更新,另外微软的ajax每个页面,都必须要一个scriptmanager,麻烦了点,最主要的是本人项目不是soa的,所以客户端调用服务的方法都是页面方法,而不是Service method,于是先天上早已决定了,不能使用AjaxControlToolkit现成的联动控件CascadingDropDown,如果你的项目是soa的你可以选择CascadingDropDown;由于以上原因,选择了相对“原始”的Ajax框架AjaxPro.2(2.0版本),不过使用很简单

  2、给用户控件增加属性
1//标记不同类型的类别 2public string Type 3 { 4 set {ViewState["type"]=value;} 5 get { return ViewState["type"].ToString(); } 6 7 } 8 9//提供对下拉框1的选择值到访问 10 public string selValue1 11 { 12 get { return hdf1.Value; } 13 14 } 15 16//提供对下拉框2的选择值到访问 17 public string selValue2 18 { 19 20 get { return hdf2.Value; } 21 22 } 23 24//提供对下拉框3的选择值到访问 25 public string selValue3 26 { 27 get { return hdf3.Value; } 28 29 }
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11 12 13public partial class controls_ThreeLevelDropdownList : System.Web.UI.UserControl 14{ 15 16 public string Type 17 { 18 set {ViewState["type"]=value;} 19 get { return ViewState["type"].ToString(); } 20 21 } 22 public string selValue1 23 { 24 get { return hdf1.Value; } 25 26 } 27 public string selValue2 28 { 29 30 get { return hdf2.Value; } 31 32 } 33 public string selValue3 34 { 35 get { return hdf3.Value; } 36 37 } 38 39 40 protected void Page_Load(object sender, EventArgs e) 41 { 42 43 if (!IsPostBack) 44 { 45 AjaxPro.Utility.RegisterTypeForAjax(typeof(controls_ThreeLevelDropdownList)); //必要的 46 47 ddl1.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'" + ddl2.ClientID + "','" + Type + "');selectValue(this.options[selectedIndex].value,'" + hdf1.ClientID + "');"); 48 ddl2.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'" + ddl3.ClientID + "','" + Type + "');selectValue(this.options[selectedIndex].value,'" + hdf2.ClientID + "');"); 49 ddl3.Attributes.Add("onchange", "selectValue(this.options[selectedIndex].value,'"+hdf3.ClientID+"');"); 50 51 ddl1.Items.Add(new ListItem("请选择","0")); 52 DataSet ds = GetLevelList("0", Type); 53 54 if (ds != null && ds.Tables[0].Rows.Count > 0) 55 { 56 foreach(DataRow dr in ds.Tables[0].Rows) 57 { 58 ddl1.Items.Add(new ListItem(dr["txt"].ToString(), dr["val"].ToString())); 59 } 60 61 } 62 ddl1.SelectedValue = "0"; 63 } 64 } 65 66 [AjaxPro.AjaxMethod] 67 public DataSet GetLevelList(string parentId,string type) 68 { 69 return new Dao.CategoryDao().getListDs(type, parentId); 70 71 } 72} 73

0
相关文章