C#dropdownlist的二级联动怎么做啊?

有两种方法:第一种是很简单的,就是第一级dropdownlist是个服务器控件,后台重新加载二级dropdownlist内容;
第二种就是js无刷新联动:代码发给你,自己看吧,半小时应该能看懂……
无刷新2级菜单联动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>.net2.0回调实现二级无刷新联动</title>
<script type="text/javascript">
<!--
function test(arg)
{
if(arg!="button")
{
var thisArray=arg.split(',');
document.getElementById("DropDownList2").length=0;
for(var i = 0; i<thisArray.length ;i++)
{
document.getElementById("DropDownList2").options.add(new Option(thisArray[i].toString(),thisArray[i].toString()));
}
}
else
{
document.getElementById("text").innerHTML = "你选择的大类是:"+document.getElementById("DropDownList1").value+" 小类是:"+document.getElementById("DropDownList2").value;
}
}
//-->
</script>
<style>
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text='请选择'></asp:ListItem>
<asp:ListItem Value=1 Text=1></asp:ListItem>
<asp:ListItem Value=2 Text=2></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text='请选择'></asp:ListItem>
</asp:DropDownList>
<input type=button οnclick="callserver('3')" value="提交" />
<span id="text"></span>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{

protected void Page_Load(object sender, EventArgs e)
{
string str = Page.ClientScript.GetCallbackEventReference(this,"arg","test",null,false);///获取一个对客户端函数的引用
string script = "function callserver(arg){"+str+"}";///组织一个callserver函数
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "key", script, true);///注册客户端脚本
DropDownList1.Attributes.Add("onchange", "callserver(document.getElementById('DropDownList1').value)");///为DropDownList1添加客户端的onchange事件,触发callserver函数
}
#region ICallbackEventHandler 成员
string selecttext = null;
public string GetCallbackResult()
{
StringBuilder sb = new StringBuilder();

if (selecttext == "1")
{
sb.Append("1-1,1-2,1-3");
}
else if (selecttext == "2")
{
sb.Append("2-1,2-2,2-3");
}
else
{
sb.Append("button");
}

return sb.ToString();///给客户端函数test返回一个参数sb
}

public void RaiseCallbackEvent(string eventArgument)
{
selecttext = eventArgument;///获得客户端提交的参数赋给变量selecttext
}
#endregion
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值