C# dropdownlist 二级联动

第一个dropdownlist中selectindexchange中的代码怎么写才能把值绑定到第二个dropdownlist中,新手求详解。... 第一个dropdownlist中 selectindexchange中的代码怎么写才能把值绑定到第二个dropdownlist中,新手求详解。 展开
 我来答
百度网友3588a6a0dc
2015-10-20 · TA获得超过2.5万个赞
知道大有可为答主
回答量:5218
采纳率:86%
帮助的人:318万
展开全部
有两种方法:第一种是很简单的,就是第一级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 onclick="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
}
Storm代理
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I... 点击进入详情页
本回答由Storm代理提供
479701998
2013-08-25 · TA获得超过626个赞
知道小有建树答主
回答量:340
采纳率:25%
帮助的人:239万
展开全部

请问你用的dropdownlist是comboBox中的dropdownlist么?

如果是,代码如下:

this.comboBox2.Items.Add(this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString());
追问
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

}

就是这里面怎么写
追答

我没有找到dropdownlist控件啊 这个控件的名字是你自己改的把 控件是哪个?

你试一下

DropDownList2.Items.Add(
DropDownList1.Items[
DropDownList1.SelectedIndex].ToString());

把你的控件名字替换上去

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-25
展开全部
在selectindexchange中通过第一个dropdownlist的值去读取第二个dropdownlist的值,一般是第一个dropdownlist的Id作为第二个dropdownlist的父Id
追问
新手,怎么写?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
2013-08-25 · 超过21用户采纳过TA的回答
知道答主
回答量:171
采纳率:0%
帮助的人:54.9万
展开全部
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
直接写绑定代码及获取dropdownlist1的值
}
追问
我就是问怎么写啊
追答

这样写!

 proteted void DropDownList1_sekectedIndexChanged(object sender, Eventargs e)
{
 //获取Id     
BindListId(Convert.Toint32(DropDownList1.SelectedValue));
}  
private void BindListId(int p)

            {

                List<Product> ls = new List<Product>();

                string sql = string.Format("select * from Pro_type where ClassId = {0}", p);

                DataSet ds = DBHelper.GetDataSet(sql);

                foreach (DataRow dr in ds.Tables[0].Rows)

                {

                    Product pro = new Product();

                    pro.Id = Convert.ToInt32(dr["Id"]);

                    pro.Name = Convert.ToString(dr["Name"]);

                    ls.Add(pro);

                }

                DropDownList2.DataSource = ls;

                DropDownList2.DataTextField = "Name";

                DropDownList2.DataValueField = "Id";

                DropDownList2.DataBind();

                this.DataBind();

                this.DropDownList2.Items.Insert(0, new ListItem("---请选择---", "0"));
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式