ASP.NET利用JavaScript实现无刷新闪烁二级联动菜单

本文介绍了一个基于ASP.NET的Web窗体应用程序如何实现下拉列表的联动功能,通过C#代码展示了如何从数据库获取大类和小类信息,并在用户选择大类时动态更新小类选项。
News_Aclass表---大类表
ClassID Int 4
ClassName varchar 50

News_Anclass表---小类表
NclassID int 4
Nclass varchar 50
ClassID int 4

---------------------------------aspx------------------------------
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false" Inherits="HoverPortal.Modules.NewsManager.test" enableViewState="False"%>
None.gif
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
<HTML>
None.gif    
<HEAD>
None.gif        
<title>test</title>
None.gif        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
None.gif        
<meta name="CODE_LANGUAGE" Content="C#">
None.gif        
<meta name="vs_defaultClientScript" content="JavaScript">
None.gif        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
None.gif    
</HEAD>
None.gif    
<body>
None.gif        
<form id="Form1" method="post" runat="server">
None.gif            
<asp:DropDownList id="ClassID" runat="server" onChange="changelocation(document.Form1.ClassID.options[document.Form1.ClassID.selectedIndex].value)"></asp:DropDownList>
None.gif            
<asp:DropDownList id="NclassID" runat="server"></asp:DropDownList>
None.gif            
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
None.gif        
</form>
None.gif    
</body>
None.gif
</HTML>
None.gif
-----------------------------------cs------------------------------------------
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Web;
None.gif
using System.Web.SessionState;
None.gif
using System.Web.UI;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Data.SqlClient;
None.gif
using Microsoft.ApplicationBlocks.Data;
None.gif
None.gif
namespace HoverPortal.Modules.NewsManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// test 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class test : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList NclassID;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList ClassID;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            ReWriteScript();
InBlock.gif            
//if(!Page.IsPostBack )
InBlock.gif
            DataBinds();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void ReWriteScript()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int count=0;
InBlock.gif            
string scriptString="<Script Language=JavaScript>\n var onecount;";
InBlock.gif            scriptString 
+="\nonecount=0;";
InBlock.gif            scriptString 
+="\nsubcat=new Array();";
InBlock.gif            SqlDataReader rs
=this.GetNclass();
InBlock.gif            
while(rs.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                scriptString 
+="\n subcat["+count+"]=new Array("+"'"+rs["Nclass"]+"'"+","+"'"+rs["classID"]+"'"+","+"'"+rs["NclassID"]+"'"+");";
InBlock.gif                count
++;
ExpandedSubBlockEnd.gif            }

InBlock.gif            rs.Close();
InBlock.gif            scriptString 
+="\n onecount="+count+";";
InBlock.gif            scriptString 
+="\n function changelocation(locationid){";
InBlock.gif            scriptString 
+="\n document.Form1.NclassID.length = 0;";
InBlock.gif            scriptString 
+="\n var locationid=locationid;";
InBlock.gif            scriptString 
+="\n var i;";
InBlock.gif            scriptString 
+="\n for(i=0; i<onecount; i++){";
InBlock.gif            scriptString 
+="\n if(subcat[i][1]==locationid){";
InBlock.gif            scriptString 
+="\n document.Form1.NclassID.options[document.Form1.NclassID.length]=new Option(subcat[i][0],subcat[i][2]);";
InBlock.gif            scriptString 
+="\n }";
InBlock.gif            scriptString 
+="\n }";
InBlock.gif            scriptString 
+="\n }";
InBlock.gif            scriptString 
+="<";
InBlock.gif            scriptString 
+="/";
InBlock.gif            scriptString 
+="script>";
InBlock.gif            
if(!this.IsClientScriptBlockRegistered("clientScript"))            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.RegisterClientScriptBlock("clientScript",scriptString);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private SqlDataReader GetNclass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlDataReader NclassReader
=SqlHelper.ExecuteReader(SqlDbObject.Connection,CommandType.StoredProcedure,"sp_News_GetNClass");
InBlock.gif            
return NclassReader;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private DataSet Getclass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet Classds
=SqlHelper.ExecuteDataset(SqlDbObject.Connection,CommandType.StoredProcedure,"sp_News_GetClass");
InBlock.gif            
return Classds;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private DataSet Getnclass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet Classds
=SqlHelper.ExecuteDataset(SqlDbObject.Connection,CommandType.StoredProcedure,"sp_News_GetNClass");
InBlock.gif            
return Classds;
ExpandedSubBlockEnd.gif        }

InBlock.gifprivate DataSet Getnclass(int classid)
  {
   SqlParameter[] parameter={new SqlParameter("@ClassID",SqlDbType.Int,4)};
   parameter[0].Value=classid;
   using(DataSet ds=SqlHelper.ExecuteDataset(SqlDbObject.Connection,CommandType.StoredProcedure,"Sp_News_GetNclassBYclassID",parameter))
   {
    return ds;
   }
  }

InBlock.gif        
private void DataBinds()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt
=this.Getclass().Tables[0];  //获得大类列表

InBlock.gif            
this.ClassID.DataSource=dt;
InBlock.gif            
this.ClassID.DataTextField="ClassName";
InBlock.gif            
this.ClassID.DataValueField="ClassID";
InBlock.gif            
this.ClassID.DataBind();
InBlock.gif            DataTable dtn=Getnclass(int.Parse(this.ClassID.SelectedValue)).Tables[0]; //根据大类列表默认选中项读出所属的小类列表
InBlock.gif            
this.NclassID.DataSource=dtn;
InBlock.gif            
this.NclassID.DataTextField="Nclass";
InBlock.gif            
this.NclassID.DataValueField="NclassID";
InBlock.gif            
this.NclassID.DataBind();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Response.Write(Request.Params[
1]);
InBlock.gif            Response.Write(
"</br>");
InBlock.gif            Response.Write(Request.Params[
2]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/hzuIT/articles/498611.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值