62,075
社区成员
发帖
与我相关
我的任务
分享
<form id="form1" runat="server">
<div id="provinceDiv">
<select id="province" name="province" onchange="SelectCapital()">
<option>省份</option><% = a %>
</select>
</div>
<div id="capitalDiv"><select id="capital" name="capital">
<option>地市级</option></select>
</div>
</form>
public string a = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
SqlConnection conn = new SqlConnection(DB.strConn);
conn.Open();
SqlDataAdapter sdr = new SqlDataAdapter();
sdr.SelectCommand = new SqlCommand("select * from sheng where city='0'", conn);
DataSet ds = new DataSet();
sdr.Fill(ds, "emp");
string b = "";
for (int i = 0; i < ds.Tables["emp"].Rows.Count; i++)
{
b += "<option>" + ds.Tables["emp"].Rows[i]["sheng"].ToString() +"</option>";
}
a = b;
sdr.Dispose();
ds.Dispose();
conn.Close();
conn.Dispose();
}
}
var path='services/system.asmx/';
function SelectCapital()
{
var b=
{
method:'get',
parameters:'name='+encodeURIComponent($F('province')),
onComplete:function(a)
{
$('capitalDiv').update(TransXml(a.responseXML));
alert(TransXml(a.responseXML));
}
};
new Ajax.Request(path+'SelectCapital',b)
}
[WebMethod]
public string SelectCapital()
{
string name = HttpContext.Current.Request["name"].ToString();
SqlConnection conn = new SqlConnection(DB.strConn);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from sheng where (city<>'0' and sheng='" + name + "')", conn);
SqlDataReader sdr = cmd.ExecuteReader();
string b = "";
while (sdr.Read())
{
b += "<option>" + sdr["city"].ToString() + "</option>";
}
return b;
}
$('capitalDiv').update(TransXml(a.responseXML));
alert(TransXml(a.responseXML));
[WebMethod]
public string SelectCapital()
{
StringBuilder sb = new StringBuilder("||start||");
string id = HttpContext.Current.Request.QueryString["id"];
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=root;database=Test"))
{
SqlDataAdapter da = new SqlDataAdapter("select * from city where pId=@PID", conn);
da.SelectCommand.Parameters.Add(new SqlParameter("@PID", id));
DataSet ds = new DataSet();
da.Fill(ds, "city");
if (ds.Tables.Contains("city"))
{
for (int i = 0; i < ds.Tables["city"].Rows.Count; i++)
{
sb.AppendFormat("<option>{0}</option>", ds.Tables["city"].Rows[i]["cName"]);
}
}
}
sb.Append("||end||");
return sb.ToString();
}
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
protected string options;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection conn = new SqlConnection("server=.;uid=xxx;pwd=xxx;database=Test"))
{
SqlDataAdapter sdr = new SqlDataAdapter("select * from province ",conn);
DataSet ds = new DataSet();
sdr.Fill(ds, "province");
StringBuilder sb = new StringBuilder();
if (ds.Tables.Contains("province"))
{
for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
{
sb.AppendFormat("<option value='{0}'>{1}</option>", ds.Tables["province"].Rows[i]["pId"], ds.Tables["province"].Rows[i]["pName"]);
}
}
options = sb.ToString();
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script src='prototype.js'></script>
<script>
var path='SelectWebService.asmx/';
function SelectCapital()
{
var b=
{
method:'get',
parameters:'id='+encodeURIComponent($F('province')),
onComplete:function(a)
{
var htmlStart='<select id="capital" name="capital"><option>城市</option>';
var htmlEnd='</select>';
var value=a.responseText;
var optionStr=value.substring(value.indexOf("||start||")+9,value.lastIndexOf("||end||"));
optionStr=optionStr.replace(new RegExp("<","gm"),'<').replace(new RegExp(">","gm"),'>');
$('capitalDiv').update(htmlStart+optionStr+htmlEnd);
}
};
new Ajax.Request(path+'SelectCapital',b)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="provinceDiv">
<select id="province" name="province" onchange="SelectCapital()">
<option>省份</option><% = options %>
</select>
</div>
<div id="capitalDiv"><select id="capital" name="capital">
<option>城市</option></select>
</div>
</form>
</body>
</html>