您的位置:首页 > 其它

无刷新2级联动(亲身体验)

2006-01-13 15:45 288 查看
<%@ Page Inherits="www.MyCodeBehind" Src="add2.cs" %>
<html>
<head>
<title>部門主管管理</title>
<script LANGUAGE="javascript">
subcity = new Array(); //定义一个数组,存放所有区县信息
<asp:Literal id="ltlsubcity" runat="server"/>
//这里使用了literal控件输出所有区县信息
function mychange(provalue)
{
//proValue参数传入客户选中的省份编号
document.myform.DropDownList3.length = 0; //将city下拉框先清空
document.myform.DropDownList3.options[0] = new Option('請選擇主管所屬部門','');//先添加第0行
var i;
for (i=0;i < subcity.length; i++) // subcity.length表示数组长度,为5
{
if (subcity[i][0] == provalue) //判断是否属于所选省份
{
//下面语句将在city下拉框最下边添加一个新项
document.myform.DropDownList3.options[document.myform.DropDownList3.length] = new Option(subcity[i][1],subcity[i][2]);
}
}
}
</script>
</head>
<link rel="stylesheet" href="Style.css" type="text/css">
<body topmargin=30 >
<%# DateTime.Now.Date.ToShortDateString()%>
<center>

<form enctype="multipart/form-data" runat="server" id="myform">

<table width="100%" border="0" align="center">
<tr><td colspan="2" align=center><font size=4>增加部門主管</font></td></tr>

<tr>
<td align=right>主管所屬公司:</td><td align=left>
<asp:DropDownList id="DropDownList2" width="150" runat="server">
<asp:ListItem Value="" Text="請選擇主管所屬公司"/>
</asp:DropDownList>
</td>
</tr>

<tr>
<td align=right>主管所屬部門:</td><td align=left>
<asp:DropDownList id="DropDownList3" width="150" runat="server">
<asp:ListItem Value="" Text="請選擇主管所屬部門"/>
</asp:DropDownList>
</td>
</tr>

<tr>
<td align=right>部門主管名稱:</td><td align=left><asp:textbox id="managername" width="145" runat="server"/></td>
</tr>

<tr>
<td colspan="2" align=center><input type=button id="Button1" value="增加" OnServerClick="Button1_Click" runat="server"></td>
</tr>
</table>
<table>
<tr>
<td colspan="1">
<div align="center"> <asp:Label id="Label1" ForeColor="Red" runat="server"/>
</div>
</td>
</tr>
</table>
<tr>
<td align=right></td><td align=left><asp:textbox id="tb" type="hidden" width="1" runat="server" style="text-align:center;border:1px solid #ffffff"/></td>
</tr>
</form>
</center>
</body>
</html>
------------------------------------------
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace www
{
public class MyCodeBehind : Page
{
public System.Web.UI.WebControls.DropDownList upower;
public System.Web.UI.WebControls.DropDownList DropDownList2,DropDownList3;
public DataGrid MyList;
protected System.Web.UI.WebControls.Button Button3,Button4;
public System.Web.UI.WebControls.TextBox managername,departmentname;
public System.Web.UI.WebControls.Label Label1;
public HtmlInputFile File1;
public HtmlInputButton Button1;
public DataRow dr2;
public TextBox TextBox3,tb;
public Literal ltlsubcity;

public void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
string nowDSN=ConfigurationSettings.AppSettings["car1"];
OleDbConnection conn =new OleDbConnection(nowDSN);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * From lw_company", conn);
DataSet ds = new DataSet();
adp.Fill(ds, "lw_company");
for (int I=0;I<=ds.Tables["lw_company"].Rows.Count-1;I++)
{
DataRow dr = ds.Tables["lw_company"].Rows[I];
DropDownList2.Items.Add(new ListItem(dr["companyname"].ToString(), dr["companycode"].ToString()));
}

adp = new OleDbDataAdapter("Select * From lw_department", conn);
adp.Fill(ds, "lw_department");
for (int I=0;I<=ds.Tables["lw_department"].Rows.Count-1;I++)
{
DataRow dr = ds.Tables["lw_department"].Rows[I];
ltlsubcity.Text += "subcity[" + I + "] = new Array(\"" + dr["companycode"].ToString() + "\",\"" + dr["departmentname"].ToString() + "\",\"" + dr["departmentcode"].ToString() + "\");";
} DropDownList2.Attributes.Add("onchange","javascript:mychange(this.options[this.selectedIndex].value);");
DropDownList3.Attributes.Add("onChange","javascript:document.myform.tb.value=this.options[this.selectedIndex].text;"); //这里是让DropDownList3的值显示到<asp:textbox id="tb"里面,那么tb.Text.ToString()就可以取出DropDownList3的值。
Page.DataBind();
}
DataBind();
}

public void Button1_Click(Object Source, EventArgs e)
{
if (managername.Text=="")
{
Response.Write ("<script>window.alert('部門主管名稱不能空,按確定返回')</script>");
//Label1.Text="部門主管名稱不能空";
}
else
{
string nowDSN=ConfigurationSettings.AppSettings["car1"];
OleDbConnection myConnection =new OleDbConnection(nowDSN);
OleDbCommand myCommand=new OleDbCommand("insert into lw_manager (company,department,managername)values('"+DropDownList2.SelectedItem.Text+"','"+tb.Text.ToString()+"','"+managername.Text.ToString()+"')" , myConnection);
myCommand.Connection=myConnection;
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Response.Write("<script language='javascript'>window.opener.location.href = window.opener.location.href;window.close();</script>");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: