DropDownList三级联动的取值问题

izhouwei 2004-11-20 07:20:34
html代码:
TreeDropDownList.aspx

<%@ Page language="c#" Codebehind="TreeDropDownList.aspx.cs" AutoEventWireup="false" Inherits="TowDropDownList.TowDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TreeDropDownList</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">

var Wantcount;
var Groupcount;

Groupcount=0;

Group = new Array();
<asp:Literal id="Want" runat="server"></asp:Literal>

function changelocation1(locationid)
{
document.submit.DirectionList1.length = 0;
document.submit.WantList1.length = 0;
var locationid=locationid;
var i;
var flag;
var j;
for (i=0;i<Groupcount; i++)
{
if (Group[i][0] == locationid)
{
flag = true;
for (j =0;j<document.submit.DirectionList1.length;j++)
if (document.submit.DirectionList1[j].value == Group[i][1])
{
flag = false;
break;
}
if (flag)
document.submit.DirectionList1.options[document.submit.DirectionList1.length]
= new Option(Group[i][1], Group[i][1]);
}
}
}


function changelocation11(locationid, direction)
{
document.submit.WantList1.length = 0;
var locationid=locationid;
var i;
var flag;
var j;
for (i=0;i<Groupcount;i++)
{
if (Group[i][0] == locationid && Group[i][1] == direction)
{

flag = true;
for (j=0;j<document.submit.WantList1.length;j++)
if (document.submit.WantList1[j].value == Group[i][2])
{
flag = false;
break;
}
if (flag)
document.submit.WantList1.options[document.submit.WantList1.length]
= new Option(Group[i][2], Group[i][2]);
}
}
}
//function changelocation11(direction1)
//{
//document.submit.reason.value=direction1;
//}
</script>
</HEAD>
<BODY ms_positioning="GridLayout">
<form id="submit" method="post" runat="server">
<TABLE style="Z-INDEX: 101; LEFT: 184px; POSITION: absolute; TOP: 144px">
<TR>
<TD style="WIDTH: 115px; HEIGHT: 17px" align="center">求职意向:</TD>
<TD style="HEIGHT: 17px"><asp:dropdownlist id="SourceList1" runat="server" Width="100px" onchange="changelocation1(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value);changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)"
DataValueField="v_source" DataTextField="v_source"></asp:dropdownlist><asp:dropdownlist id="DirectionList1" runat="server" Width="100px" onchange="changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)"
DataValueField="v_GroupName"></asp:dropdownlist><asp:dropdownlist id="WantList1" runat="server" Width="100px" DataValueField="v_title"></asp:dropdownlist></TD>
</TR>
<tr>
<td><asp:Label Runat="server" ID="reason" Width="40"></asp:Label></td>
<td><asp:Label Runat="server" ID="jiafen" Width="40"></asp:Label></td>
</tr>
</TABLE>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 256px; POSITION: absolute; TOP: 264px" runat="server"
Text="Button"></asp:Button>
</form>
<script language="javascript">
changelocation1(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value);
changelocation11(document.submit.SourceList1.options[document.submit.SourceList1.selectedIndex].value,document.submit.DirectionList1.options[document.submit.DirectionList1.selectedIndex].value)
// changelocation12(document.submit.WantList1.options[document.submit.WantList1.selectedIndex].value)
</script>
</BODY>
</HTML>

cs代码:
TreeDropDownList.cs


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TowDropDownList
{
/// <summary>
/// TowDropDownList 的摘要说明。
/// </summary>
public class TowDropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Want;
protected System.Web.UI.WebControls.DropDownList SourceList1;
protected System.Web.UI.WebControls.DropDownList DirectionList1;
protected System.Web.UI.WebControls.DropDownList WantList1;
protected System.Web.UI.WebControls.Label reason;
protected System.Web.UI.WebControls.Label jiafen;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Literal Group;

private void Page_Load(object sender, System.EventArgs e)
{
//if (!IsPostBack)
//{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("DataSource.xml"));
for(int i = 0;i <ds.Tables[0].Rows.Count ;i++)
{
DataRow dr = ds.Tables[0].Rows[i];
Want.Text += String.Format("Group[Groupcount++] = new Array(\"{0}\",\"{1}\",\"{2}\");\n", dr["v_Source"],dr["v_GroupName"].ToString(), dr["v_title"].ToString());
if (SourceList1.Items.FindByText(dr["v_Source"].ToString()) == null)
{
SourceList1.Items.Add(dr["v_Source"].ToString());
}
}
//}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
//if(!Page.IsPostBack)
Response.Write("zh"+WantList1.SelectedValue.ToString()+"zhou"+"<BR>");
//Response.Write(DirectionList1.);
}
}
}



xml代码:
DataSource.xml
<?xml version="1.0" encoding="utf-8" ?>
<DataSource>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>软件</v_GroupName>
<v_title>程序员</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>软件</v_GroupName>
<v_title>系统分析员</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>软件</v_GroupName>
<v_title>项目经理</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>硬件</v_GroupName>
<v_title>网络工程师</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>硬件</v_GroupName>
<v_title>计算机维护</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>数据库</v_GroupName>
<v_title>数据库开发</v_title>
</Data>
<Data>
<v_Source>计算机类</v_Source>
<v_GroupName>数据库</v_GroupName>
<v_title>数据库管理</v_title>
</Data>
<Data>
<v_Source>行政/管理类</v_Source>
<v_GroupName>行政</v_GroupName>
<v_title>文秘</v_title>
</Data>
<Data>
<v_Source>行政/管理类</v_Source>
<v_GroupName>行政</v_GroupName>
<v_title>助理</v_title>
</Data>
<Data>
<v_Source>行政/管理类</v_Source>
<v_GroupName>管理</v_GroupName>
<v_title>总经理</v_title>
</Data>
<Data>
<v_Source>行政/管理类</v_Source>
<v_GroupName>管理</v_GroupName>
<v_title>部门经理</v_title>
</Data>
<Data>
<v_Source>行政/管理类</v_Source>
<v_GroupName>管理</v_GroupName>
<v_title>总监</v_title>
</Data>
</DataSource>


上面这个联动可以实现,但是我加了一个提交按钮,怎么才可以将这3个下拉框中的所选的值给取出来!~
...全文
372 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjdabc 2004-12-04
  • 打赏
  • 举报
回复
这是一个操作数据库的例子:

<TR><TD style="WIDTH: 206px" align="right"><FONT face="宋体">章:</FONT></TD>
<TD><FONT face="宋体"><asp:dropdownlist id="zhang" runat="server" Width="190px" AutoPostBack="True"></asp:dropdownlist></FONT></TD>
</TR>

<TR><TD style="WIDTH: 206px" align="right">目:</TD>
<TD><FONT face="宋体"><asp:dropdownlist id="mu" runat="server" Width="270px" AutoPostBack="True"></asp:dropdownlist></FONT></TD>
</TR>
<TR><TD style="WIDTH: 206px" align="right">节:</TD>
<TD><FONT face="宋体"><asp:dropdownlist id="jie" runat="server" Width="240px"></asp:dropdownlist></FONT></TD></TR>

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
init_zhang();
zhang_change(Convert.ToInt32(zhang.Items[0].Value));
mu_change(Convert.ToInt32(mu.Items[0].Value));
}
}
private void init_zhang()
{
string sql="select mc+space(3)+zhang as xmmc,id from base_zhang order by id";
SqlCommand cmd=new SqlCommand(sql,my_connection);
my_connection.Open();
zhang.DataSource=cmd.ExecuteReader();
zhang.DataTextField="xmmc";
zhang.DataValueField="id";
zhang.DataBind();
my_connection.Close();
}
private void zhang_SelectedIndexChanged(object sender, System.EventArgs e)
{
zhang_change(Convert.ToInt32(zhang.SelectedItem.Value));
}

private void zhang_change(int zhang_id)
{
string sql="select xmbh+space(3)+xmmc as xmmc,id from base_mu where zhang_id="+zhang_id+" order by xmbh";
SqlCommand cmd=new SqlCommand(sql,my_connection);
my_connection.Open();
mu.DataSource=cmd.ExecuteReader();
mu.DataTextField="xmmc";
mu.DataValueField="id";
mu.DataBind();
my_connection.Close();
mu_change(Convert.ToInt32(mu.Items[0].Value));
}

private void mu_SelectedIndexChanged(object sender, System.EventArgs e)
{
mu_change(Convert.ToInt32(mu.SelectedItem.Value));
}

private void mu_change(int mu_id)
{
string sql="select xmbh+space(3)+xmmc as xmmc,id from base_jie where mu_id="+mu_id+" order by xmbh";
SqlCommand cmd=new SqlCommand(sql,my_connection);
my_connection.Open();
jie.DataSource=cmd.ExecuteReader();
jie.DataTextField="xmmc";
jie.DataValueField="id";
jie.DataBind();
my_connection.Close();
}
minghui000 2004-12-04
  • 打赏
  • 举报
回复
up
dtljf 2004-12-03
  • 打赏
  • 举报
回复
我也遇到与你相同的问题,已解决.
可以通过Request.Form["你的DropDownList控件名"]取到当前值.
shootarrow 2004-12-03
  • 打赏
  • 举报
回复
学习
dtljf 2004-12-03
  • 打赏
  • 举报
回复
特别关注
tianjue0921 2004-11-26
  • 打赏
  • 举报
回复
学习
xxqqpp 2004-11-26
  • 打赏
  • 举报
回复
学习
tkss 2004-11-26
  • 打赏
  • 举报
回复
minghui000 2004-11-22
  • 打赏
  • 举报
回复
up + 接分 + 留个位置学习 !
supersyd 2004-11-21
  • 打赏
  • 举报
回复
我认为 html 控件在后台代码是访问不到的。只有 post 后才可以request.Form[]
得到其值
自然框架 2004-11-21
  • 打赏
  • 举报
回复
http://www.jyklzz.net/newsm/default.htm
到这里看一下,有你想要的东西
nchen123 2004-11-21
  • 打赏
  • 举报
回复
注意要设置 DropDownList 的 AutoPostBack 属性为 true.
nchen123 2004-11-21
  • 打赏
  • 举报
回复
.net 中做多级 <select> 的联动建议使用 postback 的方法写。 就用几个 DropDownList 的 SelectedIndexChanged 事件里写就是了。 虽然效果稍微差了一点, 但我觉得没必要把自己搞得很累。
xtmyname 2004-11-21
  • 打赏
  • 举报
回复
.net中可以把html控件作为服务器控件使用。。
izhouwei 2004-11-21
  • 打赏
  • 举报
回复
还有其他人有看法吗??能告诉我吗?
viena 2004-11-20
  • 打赏
  • 举报
回复
up

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧