软件频道>程序开发>JavaVBVCDelphiC/C++Web开发微软专栏移动数据库程序人生软件工程|开发客
您现在的位置: 天极网 > 开发频道 > 用AjaxPro来完成二级连动
全文

用AjaxPro来完成二级连动

2008-07-09 08:27作者:chenguang79的专栏 - CSDNBlog出处:天极网责任编辑:nancy

  现在在实现二级连动或是三级连动方面,都是采用ajax来完成的。网上的例子也很多。 我采用的是AjaxPro来完的。 如何配置AjaxPro我就不说了。

  BigClassid 前台页面 ddlTwo.aspx

<script type="text/javascript"> 
function ShowCity(id) 

var result = chen.getCityList(id).value; 
var ddlcity = document.getElementById("ddlCity"); 
ddlcity.length=0; 
for(var i=0; i<result.Rows.length; i++) 

ddlcity.options.add(new Option(result.Rows[i].SmallClass,result.Rows[i].id)); 


</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<p> 
省份:<asp:DropDownList ID="ddlPro" runat="server"> 
</asp:DropDownList> 
市区:<asp:DropDownList ID="ddlCity" runat="server"> 
</asp:DropDownList> 
</p> 
<div> 

</div> 
</form> 
</body> 
<script type="text/javascript">
function ShowCity(id)
{
var result = chen.getCityList(id).value; 
var ddlcity = document.getElementById("ddlCity"); 
ddlcity.length=0; 
for(var i=0; i<result.Rows.length; i++)
{
ddlcity.options.add(new Option(result.Rows[i].SmallClass,result.Rows[i].id)); 
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<p>
省份:<asp:DropDownList ID="ddlPro" runat="server">
</asp:DropDownList>
市区:<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
</p>
<div>

</div>
</form>
</body>

  后台:ddlTwo.aspx.cs

[AjaxPro.AjaxNamespace("chen")] 
public partial class ddlTwo : System.Web.UI.Page 

protected void Page_Load(object sender, EventArgs e) 

AjaxPro.Utility.RegisterTypeForAjax(typeof(ddlTwo)); 

SqlConnection conn = new SqlConnection("server=.; uid=sa; pwd=chen123; database=C_News; "); 
conn.Open(); 
SqlCommand cmd = new SqlCommand("", conn); 
string strsql = "select * from C_BigClass"; 
cmd.CommandText = strsql; 
SqlDataAdapter da = new SqlDataAdapter(); 
da.SelectCommand = cmd; 
DataTable dt = new DataTable(); 
da.Fill(dt); 

this.ddlPro.DataSource = dt; 
this.ddlPro.DataValueField = "id"; 
this.ddlPro.DataTextField = "BigClass"; 
this.ddlPro.DataBind(); 

this.ddlPro.Attributes["onchange"] = "ShowCity(this.options[selectedIndex].value)"; 

conn.Close(); 


[AjaxPro.AjaxMethod] 
public DataTable getCityList(int id) 

Hashtable ht = new Hashtable(); 

SqlConnection conn = new SqlConnection("server=.; uid=sa; pwd=chen123; database=C_News; "); 
conn.Open(); 
SqlCommand cmd = new SqlCommand("", conn); 
string strsql = "select * from C_SmallClass where BigClassid="+id+""; 
cmd.CommandText = strsql; 
SqlDataAdapter da = new SqlDataAdapter(); 
da.SelectCommand = cmd; 
DataTable dt = new DataTable(); 
da.Fill(dt); 

return dt; 


  如果我们要进行三级或四级连动,其实很简单,只要在Page_load下面

  this.ddlPro.Attributes["onchange"] = "ShowCity(this.options[selectedIndex].value)";的下面为每个下拉框

  都加一个方法就行了。只不过多写几个public DataTable getCityList(int id)的程序。

相关搜索:
关注此文读者还看过
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众