.NET 应用webservices的实现全国各地省市县的选择....

1.首先要在项目里建一个SNWebService.asmx:

SNWebService.cs的代码:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// SNWebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]一定要加上这一句,以后应用的时候..
 

public class SNWebService : System.Web.Services.WebService
{

    public SNWebService()
    {
        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
    /// <summary>
    /// 获取省份数据
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
   
    public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> provinceList = new List<CascadingDropDownNameValue>();
        try
        {


           // string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
            //string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WorkDataConnectionString1"];
            SqlConnection sqlConn = new SqlConnection(connectionString);

            sqlConn.Open();

            string strSql = "Select * From Provice";

            SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

            SqlDataReader dtrProvince = sqlCmd.ExecuteReader();

            while (dtrProvince.Read())
            {
                provinceList.Add(new CascadingDropDownNameValue(dtrProvince["ProName"].ToString(), dtrProvince["ProId"].ToString()));
            }
           

            dtrProvince.Close();

            sqlConn.Close();

        }
        catch (Exception ex)
        {
           
            throw;
        }
       
        return provinceList.ToArray();
    }
    /// <summary>
    /// 获取市数据
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category)
    {
        StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        //string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        SqlConnection sqlConn = new SqlConnection(connectionString);

        sqlConn.Open();

        string strSql = "Select * From city Where ProId=" + provinceList["Province"] + "";

        SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

        SqlDataReader dtrCity = sqlCmd.ExecuteReader();

        List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>();

        while (dtrCity.Read())
        {
            cityList.Add(new CascadingDropDownNameValue(dtrCity["CityName"].ToString(), dtrCity["CityID"].ToString()));
        }

        dtrCity.Close();

        return cityList.ToArray();
    }
    /// <summary>
    /// 获取乡镇数据
    /// </summary>
    /// <param name="knownCategoryValues"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category)
    {
        StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

        SqlConnection sqlConn = new SqlConnection(connectionString);

        sqlConn.Open();

        string strSql = "Select * From County Where CityId='" + cityList["City"] + "'";

        SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);

        SqlDataReader dtrViliage = sqlCmd.ExecuteReader();

        List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>();

        while (dtrViliage.Read())
        {
            viliageList.Add(new CascadingDropDownNameValue(dtrViliage["CountyName"].ToString(), dtrViliage["Countyid"].ToString()));
        }

        dtrViliage.Close();

        return viliageList.ToArray();
    }
   
}

2.前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AgentCenterInformation.aspx.cs"
    Inherits="zhigouwang_web_AgentCenterInformation" EnableEventValidation="false" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   
    <title>代理管理中心--修改/补充代理商信息</title>
    <link href="css/css.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <!-- right begin-->
            <div class="agent_right">
                <div class="agent_right_list">
                    <p>
                        &nbsp;</p>
                </div>
                <div class="agent_right_list">
                    <li>
                        <p>
                            <table style="width: 494px; height: 281px">
                                <tr>
                                    <td colspan="3">
                                        <h4>
                                            修改补充代理商个人信息</h4>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        代理商名:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox1" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        用户的姓名
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        真实姓名:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox2" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />用户的真实姓名</td>
                                </tr>
                                <tr>
                                    <td style="width: 86px; height: 1px;">
                                        &nbsp; 性 &nbsp; &nbsp; 别:</td>
                                    <td style="width: 84px; height: 1px;">
                                        <asp:RadioButtonList ID="Radio" runat="server" Font-Size="12px" Width="89px" Height="44px"
                                            RepeatDirection="Horizontal">
                                            <asp:ListItem Value="男" Selected="True">男</asp:ListItem>
                                            <asp:ListItem Value="女">女</asp:ListItem>
                                        </asp:RadioButtonList></td>
                                    <td style="width: 188px; height: 1px;">
                                        <img src="images/icon_18.gif" />
                                        用户的性别</td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        手机号码:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox3" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        用户的手机号码</td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        电子邮件:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox4" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        必填请输入有效的电子邮件地址
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        出生年月:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox5" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        用户的出生日期</td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        身份证号:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox6" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        用户的身份证号码</td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        是否修改地址:</td>
                                    <td colspan="2">
                                        <asp:RadioButton ID="rabtrue" runat="server" AutoPostBack="True" GroupName="0" OnCheckedChanged="rabtrue_CheckedChanged" />是
                                        <asp:RadioButton ID="rabfalse" runat="server" AutoPostBack="True" Checked="True"
                                            GroupName="0" OnCheckedChanged="rabfalse_CheckedChanged" />否&nbsp;</td>
                                </tr>
                                <tr>
                                    <td colspan="3">
                                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                            <ContentTemplate>
                                                所在地区:&nbsp;&nbsp;<asp:DropDownList ID="ddlProvince" runat="server">
                                                </asp:DropDownList>省<asp:DropDownList ID="ddlCity" runat="server">
                                                </asp:DropDownList>市<asp:DropDownList ID="ddlVilliage" runat="server">
                                                </asp:DropDownList>区
                                                <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlProvince"
                                                    ServicePath="SNWebService.asmx" ServiceMethod="GetProvinceContents" Category="Province"
                                                    PromptText="请选择省份" LoadingText="省份加载中...">
                                                </cc1:CascadingDropDown>
                                                <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" ParentControlID="ddlProvince"
                                                    ServicePath="SNWebService.asmx" ServiceMethod="GetCityContents" Category="City"
                                                    TargetControlID="ddlCity" PromptText="请选择城市" LoadingText="城市加载中...">
                                                </cc1:CascadingDropDown>
                                                <cc1:CascadingDropDown ID="CascadingDropDown3" runat="server" Category="Villiage"
                                                    LoadingText="区县加载中..." ParentControlID="ddlCity" PromptText="请选择区县" ServiceMethod="GetViliageContents"
                                                    ServicePath="SNWebService.asmx" TargetControlID="ddlVilliage">
                                                </cc1:CascadingDropDown>
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                        联系地址:</td>
                                    <td style="width: 84px">
                                        <asp:TextBox ID="TextBox7" runat="server" Height="20px"></asp:TextBox></td>
                                    <td style="width: 188px">
                                        <img src="images/icon_18.gif" />
                                        用户的详细地址
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 86px">
                                    </td>
                                    <td style="width: 84px">
                                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/icon_20.gif" OnClick="ImageButton1_Click" /></td>
                                    <td style="width: 188px">
                                    </td>
                                </tr>
                            </table>
                        </p>
                    </li>
                </div>
            </div>
            <!-- right end-->
        </div>
    </form>
</body>
</html>

3.后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using OurWork.DataServer;

public partial class zhigouwang_web_AgentCenterInformation : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.UpdatePanel1.Visible = false;
            if (Session["agentname"] == null)
            {
                Response.Write("<script language='JavaScript'>");
                Response.Write("alert('请先登录!')");
                Response.Write("</script>");
                Response.End();
                return;
            }

            try
            {
                string agentname = Session["agentname"].ToString();
                string sql = "SELECT * FROM Shop_Dls where DlsName='" + agentname + "'";
                SqlDataReader dr = DbHelperSQL.ExecuteReader1(sql);

                if (dr.Read())
                {
                    TextBox1.Text = dr["DlsName"].ToString();
                    TextBox2.Text = dr["DlsRN"].ToString();
                    Radio.SelectedValue = dr["DlsSex"].ToString();
                    TextBox3.Text = dr["DlsPhone"].ToString();
                    TextBox4.Text = dr["DlsEmail"].ToString();
                    TextBox5.Text = dr["DlsBirthday"].ToString();
                    TextBox6.Text = dr["Dlssfzh"].ToString();
                    TextBox7.Text = dr["DlsAdd"].ToString();
                    dr.Close();
                }
                else
                {
                    Response.Write("<script language='JavaScript'>");
                    Response.Write("alert('没有该用户!')");
                    Response.Write("</script>");
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string AgentName, TrueName, sex, phone, email, birthday, idcard, address;
        AgentName = TextBox1.Text;
        TrueName = TextBox2.Text;
        sex = Radio.SelectedItem.Value;
        phone = TextBox3.Text;
        email = TextBox4.Text;
        birthday = TextBox5.Text;
        idcard = TextBox6.Text;

        string provice = this.ddlProvince.SelectedValue;
        string city = this.ddlCity.SelectedValue;
        string villliage = this.ddlVilliage.SelectedValue;
        string xiaddres = "";
        if (provice != null && provice != "请选择省份" && provice != "")
        {
            xiaddres += this.ddlProvince.SelectedItem.Text;
        }
        if (city != null && city != "请选择城市" && city != "")
        {
            xiaddres += this.ddlCity.SelectedItem.Text;
        }
        if (villliage != null && villliage != "请选择区县" && villliage != "")
        {
            xiaddres += this.ddlVilliage.SelectedItem.Text;
        }
        address = xiaddres;

        this.TextBox7.Text = address;
        //执行sql更新命令
        string sql2 = "update Shop_Dls set DlsName='" + AgentName + "', DlsRN='" + TrueName + "',DlsSex='" + sex + "',DlsPhone='" + phone + "',DlsEmail='" + email + "',DlsBirthday='" + birthday + "',Dlssfzh='" + idcard + "',DlsAdd='" + address + "' where DlsName='" + AgentName + "'";
        DbHelperSQL.ExecuteReader1(sql2);

        Response.Write("<script language='JavaScript'>");
        Response.Write("alert('修改成功!')");
        Response.Write("</script>");
    }

    protected void rabtrue_CheckedChanged(object sender, EventArgs e)
    {
        this.UpdatePanel1.Visible = true;

    }
    protected void rabfalse_CheckedChanged(object sender, EventArgs e)
    {
        this.UpdatePanel1.Visible = false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值