积极答复者
请教,二级联动,怎样赋出示值??

问题
-
<%@ Page Language="C#" MasterPageFile="../ProjectInfo/MasterPage.master" EnableEventValidation="false" AutoEventWireup="true"
CodeFile="test.aspx.cs" Inherits="ProjectInfo_test" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="contentCenter" runat="Server">
<script language="JavaScript" type="text/javascript">
<!--
//以XML求取数据
function XmlPost(obj)
{
var svalue = obj.value;
var webFileUrl = "test.aspx?brc_id="+svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
{
document.all("<%= DropDownList2.ClientID %>").length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
{
document.all("<%= DropDownList2.ClientID %>").options.add(new Option(piArray[i].toString()));
}
}
else
{
alert(result);
}
}
//-->
</script>
<asp:DropDownList ID="DropDownList1" Width="100px" runat="server">
</asp:DropDownList>
<select id="DropDownList2" style=" width:100px;" runat="server"></select
<asp:DropDownList ID="DropDownList3" Width="100px" runat="server">
</asp:DropDownList>//
<asp:Button ID="Button3" runat="server" Text="Button" OnClick="btn_click" /></asp:Content>
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 DB_Operation;public partial class ProjectInfo_test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//在此读数据库
string province;
province=ds.["table"].rows[0]["province"].tostring();
/////////比如province="山东";area="烟台";我怎样才能在页面初始化把值赋进去???请孟兄指点迷津!
string brc_id = this.Request.QueryString["brc_id"];
if (brc_id + "a" != "a")
{
this.down2_bind(brc_id);
}
if (!this.IsPostBack)
{
this.down1_bind();
}}
private void down2_bind(string brc_id)
{
string mystr = "";
string sql = "Select AreaCity From Com_Area where AreaProvince= '" + brc_id + "'";
DBConnection db = new DBConnection();
DataSet ds = db.getDataSet(sql, "table");
DataTable mytab = ds.Tables["table"];
// DataTable mytab = this.get_dt(sql);if (mytab.Rows.Count != 0)
{
for (int i = 0; i < mytab.Rows.Count; i++)
{
mystr += "," + mytab.Rows[i][0].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
/// <summary>
/// 绑定第一个下拉框
/// </summary>
private void down1_bind()
{
string sql = "select brc_id,brc_name from asm_branch where brc_level = '1'";
sql = "Select distinct AreaProvince From Com_Area";
DBConnection db = new DBConnection();
DataSet ds = db.getDataSet(sql, "table");
DataTable mytab = ds.Tables["table"];
this.DropDownList1.DataSource = mytab;
this.DropDownList1.DataValueField = "AreaProvince";
this.DropDownList1.DataTextField = "AreaProvince";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange", "XmlPost(this);");
}
protected void btn_click(object obj, EventArgs e)
{
string str1, str2;
str1 = DropDownList1.SelectedValue.ToString();
str2 = Request.Form[DropDownList2.name].ToString
// str2=Request.Form[0].}
}
答案
-
以下代码包含了2种方法的实现,为了简化,数据采用了自定义的数据,采用数据库道理相同。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" EnableEventValidation="false" %> <!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 id="Head1" runat="server"> <title></title> <script type="text/javascript"> function xmlPost() { var xmlhttp = null; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest() } else { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } var d1 = document.getElementById('<%=DropDownList1.ClientID %>') var d2 = document.getElementById('<%=DropDownList2.ClientID %>') var url = "Default8.aspx?d1=" + d1.value; xmlhttp.open("GET", url, true) xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { r = xmlhttp.responseText.split(","); for (i = 0; i < r.length; i++) { d2.options.length = 0; d2.options[d2.options.length] = new Option(r[i],r[i]) } } } } xmlhttp.send(null); } </script> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="false"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="false"> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Button" /> </form> </body> </html>
CS代码using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default8 : Page { protected void Page_Load(object sender, EventArgs e) { //例子数据 DropDownList1.Attributes.Add("onchange", "xmlPost();return false;"); string[] city = { "beijing", "shanghai", "henan" }; System.Collections.Hashtable ht = new System.Collections.Hashtable(); ht.Add("beijing", "海淀,昌平,密云"); ht.Add("shanghai", "浦东,杨浦,青浦"); ht.Add("henan", "郑州,周口,洛阳"); if (String.IsNullOrEmpty(Request.QueryString["d1"])) { DropDownList1.DataSource = city; DropDownList1.DataBind(); DropDownList2.Items.Clear(); //DropDownList2.DataSource = ((string)ht["beijing"]).Split(','); //DropDownList2.DataBind(); //如果采取服务器端初始化,请去掉上面2行的注释,给下面2行增加注释 ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(Page.GetType(), "key", "xmlPost()", true); } else { Response.ClearContent(); Response.Write((string)ht[Request.QueryString["d1"].ToString()]); Response.End(); } } }
孟宪会- 已标记为答案 KeFang Chen 2009年3月19日 6:26
全部回复
-
以下代码包含了2种方法的实现,为了简化,数据采用了自定义的数据,采用数据库道理相同。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" EnableEventValidation="false" %> <!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 id="Head1" runat="server"> <title></title> <script type="text/javascript"> function xmlPost() { var xmlhttp = null; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest() } else { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } var d1 = document.getElementById('<%=DropDownList1.ClientID %>') var d2 = document.getElementById('<%=DropDownList2.ClientID %>') var url = "Default8.aspx?d1=" + d1.value; xmlhttp.open("GET", url, true) xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { r = xmlhttp.responseText.split(","); for (i = 0; i < r.length; i++) { d2.options.length = 0; d2.options[d2.options.length] = new Option(r[i],r[i]) } } } } xmlhttp.send(null); } </script> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="false"> </asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="false"> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Button" /> </form> </body> </html>
CS代码using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default8 : Page { protected void Page_Load(object sender, EventArgs e) { //例子数据 DropDownList1.Attributes.Add("onchange", "xmlPost();return false;"); string[] city = { "beijing", "shanghai", "henan" }; System.Collections.Hashtable ht = new System.Collections.Hashtable(); ht.Add("beijing", "海淀,昌平,密云"); ht.Add("shanghai", "浦东,杨浦,青浦"); ht.Add("henan", "郑州,周口,洛阳"); if (String.IsNullOrEmpty(Request.QueryString["d1"])) { DropDownList1.DataSource = city; DropDownList1.DataBind(); DropDownList2.Items.Clear(); //DropDownList2.DataSource = ((string)ht["beijing"]).Split(','); //DropDownList2.DataBind(); //如果采取服务器端初始化,请去掉上面2行的注释,给下面2行增加注释 ClientScriptManager cs = Page.ClientScript; cs.RegisterStartupScript(Page.GetType(), "key", "xmlPost()", true); } else { Response.ClearContent(); Response.Write((string)ht[Request.QueryString["d1"].ToString()]); Response.End(); } } }
孟宪会- 已标记为答案 KeFang Chen 2009年3月19日 6:26
-
if (strsalesman != "")
{//初始化
string namecmd = "Select ProvinceName From Com_PrinceHome where PrinceHome='" + strsalesunit + "'";
DBConnection namedr = new DBConnection();
DataSet nameds = namedr.getDataSet(namecmd, "table");
string temp;
temp = nameds.Tables["table"].Rows[0]["ProvinceName"].ToString().Trim();
string[] arr = temp.Split(new char[] { ',' });
int num = arr.Length;
SalesMan.Items.Clear();
for (int i = 0; i < num; i++)
{
SalesMan.Items.Insert(i, new ListItem(arr[i], ""));
}
SalesMan.DataBind();
// SalesMan.Items.FindByValue(strsalesman).Selected = true;孟兄,我用这种方式,为什么报错,我还少什么语句?请指点,谢谢!