asp中的省市二级联动(2008-11-27 22:26:05)
标签:it分类:asp sql server下的自建表 province(省份),city(城市),如下:
城市表city
联动代码如下:
<%
dim tmpid '定义一个临时变量用来记住省id
tmpid=0
set rs_Province=server.CreateObject("ADODB.recordset")
sql="select * from province order by ProvinceOrder"
rs_Province.open sql,conn,1,1
while not rs_Province.eof
tmpid=rs_Province("province_id")
%>
<%
rs_Province.movenext
wend
rs_Province.close
set rs_Province=nothing
%>
<%
set rs_City=server.createobject("adodb.recordset")
sql="select * from city where province_id="&tmpid&" order by CityOrder"
rs_City.open sql,conn,1,1
while not rs_City.eof
%>
<%
rs_City.movenext
wend
rs_City.close
set rs_City=nothing
%>
2009-09-09 22:09:40 asp二级联动同个表
分类:(asp编程) | 评论:(0) | 浏览:(137) | 标签: 二级联动同个表
数据库结构
程序代码
id anclassid anclass nclass
0 0 一年级一年五
班
1 1 二年级二年二
班
2 0 一年级一年二
班
3 1 二年级二年一
班
4 1 二年级二年三
班
5 2 三年级三年一
班
6 2 三年级三年二
班
7 2 三年级三年三班
<%
dim conn,constr
constr="driver={microsoft access driver (*.mdb)};uid=admin;pwd=数据库密码;dbq="& server.mappath("class.mdb")
set conn=server.createobject("adodb.connection")
conn.open constr
set rs=server.createobject("adodb.recordset")
rs.open "select * from school order by anclassid",conn,1, 1%>
var onecount;
onecount=0;
subcat = new Array();
<%
count = 0
do while not rs.eof
%>
subcat[<%=count%>] = new Array("<%= trim(rs("anclassid"))%>"," <%= rs("anclass")%>","<%= rs("nclass")%>");
<%
count count = count + 1
rs.movenext
loop
rs.close
%>
onecount=<%=count%>;
function changelocation(locationid)
{
document.form.Nclassid.length = 0;
var locationid locationid=locationid;
var i;
for (i=0;i { if (subcat[i][0] == locationid) { document.form.Nclassid.options[document.form. Nclassid.length] = new Option(subcat[i][2], subcat[i][2]); //这里图像的内容已[ 2 ] } } } <% set rs=server.createobject("adodb.recordset") rs.open "select distinct anclassid,anclass from school orde r by anclassid",conn,1,1 if rs.eof and rs.bof then response.write "请先添加栏目。" response.end else %>
<% dim selclass
selclass=rs("anclassid")
rs.movenext
do while not rs.eof
%>
<%
rs.movenext
loop
end if
rs.close
%>
<%rs.open "select * from school where anclassid="&selclas s ,conn,1,1
if not(rs.eof and rs.bof) then
%>
<% rs.movenext
do while not rs.eof%>
<% rs.movenext
loop
end if
rs.close
set rs = nothing
%>
https://www.sodocs.net/doc/be9622740.html, DataGrid控件分页代码
2008-08-21 18:23
先看实现的效果:
代码:
<% @ Page Language="C#" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.OleDb" %>
private int recordCount;
public void Page_Load(Object src,EventArgs e)
{
string strComm = "select * from news";
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;Data Source="; strConnection += Server.MapPath("app_data/data.mdb");
OleDbConnection MyConnection = new OleDbConnection(strConnection); OleDbDataAdapter MyComm = new OleDbDataAdapter(strComm, MyConnection);
DataSet MyDataSet = new DataSet();
MyComm.Fill(MyDataSet,"UserList");
recordCount = MyDataSet.Tables[0].Rows.Count;
DataGrid1.DataSource = MyDataSet.Tables["UserList"].DefaultView;
if(!Page.IsPostBack)
{
DataGrid1.DataBind();
pagecount.Text = DataGrid1.PageCount.ToString();
nowpage.Text = "1";
pagesize.Text = DataGrid1.PageSize.ToString();
_recordCount.Text = recordCount.ToString();
}
}
private void change_page(object sender, System.EventArgs e) {
LinkButton btn = (LinkButton)sender;
int PageIndex = Int32.Parse(nowpage.Text) - 1;
int _pagecount = Int32.Parse(pagecount.Text);
switch(https://www.sodocs.net/doc/be9622740.html,mandName)
{
case "First":
PageIndex = 0;
break;
case "Prev":
PageIndex = PageIndex - 1;
break;
case "Next":
PageIndex = PageIndex + 1;
break;
case "Last":
PageIndex = _pagecount-1;
break;
}
if(PageIndex < 0)
PageIndex = 0;
else if (PageIndex > _pagecount-1)
PageIndex = _pagecount-1;
DataGrid1.CurrentPageIndex = PageIndex;
DataGrid1.DataBind();
pagecount.Text = DataGrid1.PageCount.ToString(); nowpage.Text = (DataGrid1.CurrentPageIndex + 1).ToString(); pagesize.Text = DataGrid1.PageSize.ToString();
_recordCount.Text = recordCount.ToString();
}
public void DataGrid1_PageIndexChanged(object
sender,DataGridPageChangedEventArgs e)
{
//startIndex = e.NewPageIndex*DataGrid1.PageSize;
DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid1.DataBind();
}
DataGrid自带分页实现
2008-08-05 16:45
webForm.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="PageGridView.aspx.cs" Inherits="PageGridView" %>
cs.文件
using System;
using System.Collections;
using https://www.sodocs.net/doc/be9622740.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class PageGridView : System.Web.UI.Page
{
private int recordCount;
private int pageCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataGridDataBind();
}
}
//绑定数据
private void DataGridDataBind()
{
DataSet ds = GetCustomersData();
recordCount = ds.Tables[0].Rows.Count;
//获取当前的页数
pageCount = (int)Math.Ceiling(recordCount * 1.0 / PageSize); //避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错
if (recordCount == 0)
{
this.DataGrid1.CurrentPageIndex = 0;
}
else if (this.DataGrid1.CurrentPageIndex >= pageCount) {
this.DataGrid1.CurrentPageIndex = pageCount - 1; }
this.DataGrid1.DataSource = ds;
this.DataGrid1.DataBind();
NavigationStateChange();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 https://www.sodocs.net/doc/be9622740.html, Web 窗体设计器所必需的。 //
InitializeComponent();
base.OnInit(e);
}
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.LBtnFirst.Click += new
System.EventHandler(this.LBtnNavigation_Click);
this.LBtnPrev.Click += new
System.EventHandler(this.LBtnNavigation_Click);
this.LBtnNext.Click += new
System.EventHandler(this.LBtnNavigation_Click);
this.LBtnLast.Click += new
System.EventHandler(this.LBtnNavigation_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LBtnNavigation_Click(object sender, System.EventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch (https://www.sodocs.net/doc/be9622740.html,mandName)
{
case "First":
PageIndex = 0;
break;
case "Prev"://if( PageIndex > 0 )
PageIndex = PageIndex - 1;
break;
case "Next"://if( PageIndex < PageCount -1)
PageIndex = PageIndex + 1;
break;
case "Last":
PageIndex = PageCount - 1;
break;
}
DataGridDataBind();
}
//数据绑定
public static DataSet GetCustomersData()
{
SqlConnection conn = new
SqlConnection("server=.;database=NorthWind;uid=sa;pwd=sa;");
string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
SqlCommand comm = new SqlCommand(sqlStr, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
///
/// 控制导航按钮或数字的状态
///
public void NavigationStateChange()
{
if (PageCount <= 1)//( RecordCount <= PageSize )//小于等于一页 {
this.LBtnFirst.Enabled = false;
this.LBtnPrev.Enabled = false;
this.LBtnNext.Enabled = false;
this.LBtnLast.Enabled = false;
}
else //有多页
{
if (PageIndex == 0)//当前为第一页
{
this.LBtnFirst.Enabled = false;
this.LBtnPrev.Enabled = false;
this.LBtnNext.Enabled = true;
this.LBtnLast.Enabled = true;
}
else if (PageIndex == PageCount - 1)//当前为最后页
{
this.LBtnFirst.Enabled = true;
this.LBtnPrev.Enabled = true;
this.LBtnNext.Enabled = false;
this.LBtnLast.Enabled = false;
}
else //中间页
{
this.LBtnFirst.Enabled = true;
this.LBtnPrev.Enabled = true;
this.LBtnNext.Enabled = true;
this.LBtnLast.Enabled = true;
}
}
if (RecordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页
this.LtlPageCount.Text = "0";
else
this.LtlPageCount.Text = PageCount.ToString();
if (RecordCount == 0)
this.LtlPageIndex.Text = "0";
else
this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有页数的情况下前台显示页数加1
this.LtlPageSize.Text = PageSize.ToString();
this.LtlRecordCount.Text = RecordCount.ToString();
}
// 总页数
public int PageCount
{
get { return this.DataGrid1.PageCount; }
}
//页大小
public int PageSize
{
get { return this.DataGrid1.PageSize; }
}
//页索引,从零开始
public int PageIndex
{
get { return this.DataGrid1.CurrentPageIndex; }
set { this.DataGrid1.CurrentPageIndex = value; }
}
// 纪录总数
public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
}
DataGrid快速分页
2009-07-27 11:06
cs:
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 System.Data.SqlClient;
public partial class user_shiyan2 : System.Web.UI.Page
{
private static int intRecordCount; //记录总数
private static int intPageCount; //总页数 private static int intCurrentPageIndex; //当前页码
private DataView dv;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//将当前页面置为1,即显示第一页的记录
intCurrentPageIndex = 1;
//设置属性
// DataGrid1.AllowPaging = "True";
DataGrid1.AllowPaging = true;
// DataGrid1AllowCustomPaging = "True";
DataGrid1.AllowCustomPaging = true;
//绑定数据(Bind()是一个自定义方法)
this.Bind(intCurrentPageIndex);
}
}
private void Bind(int intCurrentPageIndex)
{
string strConn =
ConfigurationSettings.AppSettings["Str_sql"].ToString();
//每次只取出一页(即10条)数据的SQL语句
string strSql = "select top 10 * from attorn where attorn_id not in (select top " + ( intCurrentPageIndex - 1)*10 + " attorn_id from attorn)";
if (TextBox1.Text != null && TextBox1.Text != "")
{
strSql = "select top 10 * from attorn where attorn_id not in (select top " + (intCurrentPageIndex - 1) * 10 + " attorn_id from attorn where [User_name] like '%" + TextBox1.Text + "%') and [User_name] like '%" + TextBox1.Text + "%'";
}
//得到总记录数的SQL语句
string strSqlCount = "select count(*) from attorn ";
if(TextBox1.Text != null && TextBox1.Text != "")
{
strSqlCount = "select count(*) from attorn
where [User_name] like '%" + TextBox1.Text + "%'";
}
//建立与数据的连接
SqlConnection dbConnection = new SqlConnection(strConn);
dbConnection.Open();
//创建适配器的实例,并填充数据
SqlDataAdapter dsAdapter = new
SqlDataAdapter(strSql,dbConnection);
DataSet ds = new DataSet();
dsAdapter.Fill(ds);
dv = ds.Tables[0].DefaultView;
//取得总记录数
SqlCommand cmd = new SqlCommand(strSqlCount,dbConnection);
intRecordCount = (Int32) cmd.ExecuteScalar();
//关闭连接
dbConnection.Close();
//计算总页数
double dblRecordCount;
dblRecordCount = System.Convert.ToDouble(intRecordCount);
intPageCount = (Int32)Math.Ceiling(dblRecordCount / 10);
Label1.Text = intCurrentPageIndex.ToString();
Label2.Text = intPageCount.ToString();
//绑定数据
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
protected void LinkButton1_Click(object sender, EventArgs e) {
intCurrentPageIndex = 1;
//重新绑定数据
this.Bind(intCurrentPageIndex);
}
protected void LinkButton2_Click(object sender, EventArgs e) {
intCurrentPageIndex = intCurrentPageIndex - 1;
if (intCurrentPageIndex <= 0)
{
intCurrentPageIndex = 1;
}
//重新绑定数据
this.Bind(intCurrentPageIndex);
}
protected void LinkButton3_Click(object sender, EventArgs e) {
intCurrentPageIndex = intCurrentPageIndex + 1;
if(intCurrentPageIndex > intPageCount)
{
intCurrentPageIndex = intPageCount;
}
//重新绑定数据
this.Bind(intCurrentPageIndex);
}
protected void LinkButton4_Click(object sender, EventArgs e) {
intCurrentPageIndex = intPageCount;
//重新绑定数据
this.Bind(intCurrentPageIndex);
}
protected void Button1_Click(object sender, EventArgs e)
{
intCurrentPageIndex = 1;
//重新绑定数据
this.Bind(intCurrentPageIndex);
}
}
aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="shiyan2.aspx.cs" Inherits="user_shiyan2" %>
CheckBox放到DataGrid中对应CheckBox的列的页眉上(header).我们给这个模板列的题头上添加一个CheckBox控件利用它来完成和1中相同的工作,只是过程稍微有些不同.首先我们需要一个DataGrid来表现我们的程序,该DataGrid在Html页上的代码如下:
// 只显示主要的下面的不写了……在grdClient中有绑定的详细列
我们给HerderTemple添加了一个chkAllServer其中Server说明它是调用服务器端事件的.我们为了给这个控件添加事件必须在创建DataGridItem的时候给它添加事件代码如下:
private void grdServer_ItemCreated(object sender, System.Web.UI.WebControls.DataGridIte mEventArgs e) {
if(e.Item.ItemType == ListItemType.Header){
CheckBox chk = (CheckBox)e.Item.FindControl("chkAllServer");