无刷新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>");
}
}
}
}
<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>");
}
}
}
}
相关文章推荐
- 下拉选项 2级 联动-- 服务器端(刷新)和ajax(不刷新)
- asp.net实现DropDownList、ListBox无刷新三级联动的两种方法
- c#结合js实现无刷新省市联动菜单
- 51aspx省市区县(含全国完整数据库)Ajax无刷新三级联动
- 省市2级联动
- Ajax局部刷新实现三级联动
- DropDownList无刷新联动在aspx中的简单实现
- 认知物联网时代,17000家企业用Watson刷新购物体验
- javascript 实现 对XML文件 2级/3级联动操作
- 亲身体验北京航空航天大学被美国贸易制裁的神奇经历
- ajax实现无刷新省市联动
- jQuery Pjax – 页面无刷新加载,优化用户体验
- [准备/原创]:亲身体验多核/超线程
- Javascript 实现无刷新联动菜单(select)的方法
- ajax实现无刷新两级联动DropDownList
- 实现无刷新DropdownList联动效果
- 亲身体验 DDOS(拒绝服务)攻击硬防DIY
- 使用AjaxPro开发四级无刷新联动下拉框
- 无刷新的四级联动下拉框实例(AjaxPro)
- Ajax实现无刷新三联动下拉框