คร่าว ๆ นะครับ
ส่วนแรก หน้า aspx
<ajaxToolkit:ToolkitScriptManager runat="Server" EnableScriptGlobalization="true"EnableScriptLocalization="true" ID="ScriptManager1" /><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList><br /><ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" ServicePath="CommonService.asmx" ServiceMethod="GETPROVINCE" Category="Province"PromptText="เลือกจังหวัด" LoadingText="กำลังอ่านข้อมูล..."></ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="DropDownList2"ServicePath="CommonService.asmx" ServiceMethod="GETAMPHUR"ParentControlID="DropDownList1"Category="Amphur" PromptText="เลือกอำเภอ" LoadingText="กำลังอ่านข้อมูล..." ></ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"ServicePath="CommonService.asmx" ServiceMethod="GETTUMBOL" ParentControlID="DropDownList2"Category="GETTUMBOL" PromptText="เลือกตำบล" LoadingText="กำลังอ่านข้อมูล..." ></ajaxToolkit:CascadingDropDown>
<ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="DropDownList3"ServicePath="CommonService.asmx" ServiceMethod="GETTUMBOL" ParentControlID="DropDownList2"Category="GETTUMBOL" PromptText="เลือกตำบล" LoadingText="กำลังอ่านข้อมูล..." ></ajaxToolkit:CascadingDropDown>
ส่วนที่สอง ส่วนของ Web Service
public class CommonService : System.Web.Services.WebService{ public CommonService() { //Uncomment the following line if using designed components //InitializeComponent(); } public string ConnStr = ConfigurationManager.ConnectionStrings["TestDB"].ConnectionString; [WebMethod(EnableSession = true)] public CascadingDropDownNameValue[] GETPROVINCE(string knownCategoryValues, string category) { SqlConnection myConnection = new SqlConnection(ConnStr); SqlCommand myCommand = new SqlCommand("select '1',changwatname,provinceid from changwat where provinceid= @provinceid union select '2',changwatname,provinceid from changwat", myConnection); myCommand.CommandType = CommandType.Text; myCommand.Parameters.AddWithValue("@provinceid", "32" /*provinceid*/); myConnection.Open(); List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { values.Add(new CascadingDropDownNameValue((string)reader["changwatname"], reader["provinceid"].ToString())); } myConnection.Close(); return values.ToArray(); } }
[WebMethod] public CascadingDropDownNameValue[] GETAMPHUR(string knownCategoryValues, string category) { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString( knownCategoryValues); int Provinceid; if (!kv.ContainsKey("Province") || !Int32.TryParse(kv["Province"], out Provinceid)) { return null; }
SqlConnection myConnection = new SqlConnection(ConnStr); SqlCommand myCommand = new SqlCommand("select '1',amphurname,cityid from amphur where provinceid= @provinceid and cityid is not null /*union select '2',amphurname,cityid from amphur where cityid is not null*/", myConnection); myCommand.CommandType = CommandType.Text; myCommand.Parameters.AddWithValue("@provinceid", Convert.ToString(Provinceid)); //"01" /*districtid*/); myConnection.Open();
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) {
values.Add(new CascadingDropDownNameValue((string)reader["amphurname"], reader["cityid"].ToString())); } } return values.ToArray(); }
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) {
values.Add(new CascadingDropDownNameValue((string)reader["amphurname"], reader["cityid"].ToString())); } } return values.ToArray(); }
[WebMethod] public CascadingDropDownNameValue[] GETTUMBOL(string knownCategoryValues, string category) { StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString( knownCategoryValues); int Amphurid; int Provinceid; string a; string b; if (!kv.ContainsKey("Amphur")) { return null; } b = kv["Amphur"]; a = kv["Province"]; SqlConnection myConnection = new SqlConnection(ConnStr); SqlCommand myCommand = new SqlCommand("select '1',tumbolname,districtid from tumbol where provinceid = @provinceid and cityid= @cityid ", myConnection); myCommand.CommandType = CommandType.Text; myCommand.Parameters.AddWithValue("@provinceid", Convert.ToString(a)); myCommand.Parameters.AddWithValue("@cityid", Convert.ToString(b)); //"01" /*districtid*/); myConnection.Open();
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { values.Add(new CascadingDropDownNameValue((string)reader["tumbolname"], reader["districtid"].ToString())); } } return values.ToArray(); }
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); using (SqlDataReader reader = myCommand.ExecuteReader()) { while (reader.Read()) { values.Add(new CascadingDropDownNameValue((string)reader["tumbolname"], reader["districtid"].ToString())); } } return values.ToArray(); }
เสร็จออกมาแล้วจะได้เวปหน้าตาเยี่ยงนี้ครับ... พยายามเข้า
No comments:
Post a Comment