1.前台

 <span id="spnClient" style="margin-left: 30px; margin-top: 10px">
<lable>Client: </lable>
<asp:DropDownList ID="ddlClient" DataValueField="ID" DataTextField="NAME" runat="server" />
</span> <span id="spnProject" style="margin-left: 30px; margin-top: 10px">
<lable>Project: </lable>
<asp:DropDownList ID="ddlProject" DataValueField="ID" DataTextField="NAME" runat="server" />
</span>
<span id="spnUser" style="display: none; margin-left: 30px; margin-top: 10px;">
<lable>User: </lable>
<asp:DropDownList ID="ddlUser" DataValueField="ID" DataTextField="USER_NAME" runat="server" />
</span>

2.绑定DataSource

        private void LoadDropDownList()
{
DataTable dtYear =SplendidCache.Years();
ddlYEAR.DataSource = dtYear;
ddlYEAR.DataBind();
ddlYEAR.SelectedValue = DateTime.Now.Year.ToString(); DataTable dtClient = SplendidCache.Client(true);
ddlClient.DataSource = dtClient;
ddlClient.DataBind(); DataTable dtProject = SplendidCache.Project();
ddlProject.DataSource = dtProject;
ddlProject.DataBind(); DataTable dtUser = SplendidCache.AssignedUser();
ddlUser.DataSource = dtUser;
ddlUser.DataBind(); BindProjects();
BindUsers();
}
         private void BindProjects()
{
DataTable dtProjects = new DataTable();
DbProviderFactory dbf = DbProviderFactories.GetFactory();
using (IDbConnection con = dbf.CreateConnection())
{
con.Open();
using (IDbCommand cmd = con.CreateCommand())
{
string sSQL = "SELECT ID,NAME,CLIENTID FROM Project WHERE DELETED=0 ORDER BY NAME " + ControlChars.CrLf;
cmd.CommandText = sSQL;
using (DbDataAdapter da = dbf.CreateDataAdapter())
{
((IDbDataAdapter)da).SelectCommand = cmd;
da.Fill(dtProjects);
StringBuilder sbProjects = new StringBuilder();
if (dtProjects != null && dtProjects.Rows.Count > )
{
sbProjects.Append("{\"project\":[");
int i = ;
int count = dtProjects.Rows.Count;
foreach (DataRow row in dtProjects.Rows)
{
if (i == count)
{
sbProjects.Append("{\"client\":\"" + Sql.ToString(row["CLIENTID"]) + "\",\"id\":\"" + Sql.ToString(row["ID"]) + "\",\"name\":\"" + Sql.ToString(row["NAME"]).Replace("'","‘") + "\"}");
}
else
{
sbProjects.Append("{\"client\":\"" + Sql.ToString(row["CLIENTID"]) + "\",\"id\":\"" + Sql.ToString(row["ID"]) + "\",\"name\":\"" + Sql.ToString(row["NAME"]).Replace("'", "‘") + "\"},");
}
i++;
}
sbProjects.Append("]");
}
sbProjects.Append("}");
m_sProjectsJson = sbProjects.ToString();
}
}
}
}
private void BindUsers()
{
DataTable dtUsers = new DataTable();
DbProviderFactory dbf = DbProviderFactories.GetFactory();
using (IDbConnection con = dbf.CreateConnection())
{
con.Open();
using (IDbCommand cmd = con.CreateCommand())
{
string sSQL = "SELECT DISTINCT USER_ID,USER_NAME,PROJECT_ID FROM vwPROJECT_USERS WHERE USER_ID IS NOT NULL GROUP BY USER_ID,USER_NAME,PROJECT_ID ORDER BY USER_NAME" + ControlChars.CrLf;
cmd.CommandText = sSQL;
using (DbDataAdapter da = dbf.CreateDataAdapter())
{
((IDbDataAdapter)da).SelectCommand = cmd;
da.Fill(dtUsers);
StringBuilder sbUsers = new StringBuilder();
if (dtUsers != null && dtUsers.Rows.Count > )
{
sbUsers.Append("{\"user\":[");
int i = ;
int count = dtUsers.Rows.Count;
foreach (DataRow row in dtUsers.Rows)
{
if (i == count)
{
sbUsers.Append("{\"project\":\"" + Sql.ToString(row["PROJECT_ID"]) + "\",\"id\":\"" + Sql.ToString(row["USER_ID"]) + "\",\"name\":\"" + Sql.ToString(row["USER_NAME"]) + "\"}");
}
else
{
sbUsers.Append("{\"project\":\"" + Sql.ToString(row["PROJECT_ID"]) + "\",\"id\":\"" + Sql.ToString(row["USER_ID"]) + "\",\"name\":\"" + Sql.ToString(row["USER_NAME"]) + "\"},");
}
i++;
}
sbUsers.Append("]");
}
sbUsers.Append("}");
m_sUserJson = sbUsers.ToString();
}
}
}
}
    private static string m_sProjectsJson = "";
public static string ProjectsJson
{
get { return m_sProjectsJson; }
} private static string m_sUserJson = "";
public static string UsersJson
{
get { return m_sUserJson; }
}

3.javascript

     $("#<%=ddlClient.ClientID%>").change(function () {
loadproject();
});
$("#<%=ddlProject.ClientID%>").change(function () {
loaduser();
});
        function loadproject() {
var client = $("#<%=ddlClient.ClientID%>").val();
var projectArray = jQuery.parseJSON('<%= ProjectsJson%>');
var filtered = $.grep(projectArray.project,
function (item) {
if (client != "") {
return item.client == client;
}
else {
return item;
}
}
);
$("#<%=ddlProject.ClientID%>").empty();
$.each(filtered, function (i, item) {
$("#<%=ddlProject.ClientID%>").append("<option value='" + item.id + "'>" + item.name + "</option>");
});
}
    function loaduser() {
var project = $("#<%=ddlProject.ClientID%>").val();
var userArray = jQuery.parseJSON('<%= UsersJson%>');
var filtered = $.grep(userArray.user,
function (item) {
if (project != "") {
return item.project == project;
}
else {
return item;
}
}
);
$("#<%=ddlUser.ClientID%>").empty();
$.each(filtered, function (i, item) {
$("#<%=ddlUser.ClientID%>").append("<option value='" + item.id + "'>" + item.name + "</option>");
});
}

.net DropDownList静态联动的更多相关文章

  1. DropDownList四级联动

    前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_dro ...

  2. ASP .NET DropDownList多级联动事件

    思路 假如有三级省.市.区,先加载出所有省选择省之后,加载出该省所有市选择市之后,加载出该市所有区重新选择省,则清空市和区重新选择市,则清空区想好数据结构,不同的数据结构做法不同 例子 数据结构 pu ...

  3. dropdownlist 二级联动

    protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GradeBind(); } } //绑定 ...

  4. MVC学习系列7--下拉框的联动

    [使用场景:两个DropDownList的联动,选择其中一个DropDownList,然后加载数据到另外的一个DropDownList上] 这里,我打算实现的需求是:有两个DropDownList,一 ...

  5. 用jquery+Asp.Net实现省市二级联动

    页面html: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ddlAjax. ...

  6. MVC学习系列4--@helper辅助方法和用户自定义HTML方法

    在HTML Helper,帮助类的帮助下,我们可以动态的创建HTML控件.HTML帮助类是在视图中,用来呈现HTML内容的.HTML帮助类是一个方法,它返回的是string类型的值. HTML帮助类, ...

  7. WebForm使用JQuery实现DropDownList无刷新联动

    目录(?)[-] 1  JS代码 2 页面相关控件用的是平台封装的控件普通DropDownList也可以 3 后台C代码 注意事项   原来用的微软封装的Ajax控件UpdatePannel和Scri ...

  8. 用DropDownList实现的省市级三级联动

    这是一个用DropDownList 实现的省市级三级联动,记录一下········ <asp:ScriptManager ID="ScriptManager1" runat= ...

  9. MVC编辑状态两个DropDownList联动

    前几天使用jQuery在MVC应用程序中,实现了<jQuery实现两个DropDownList联动(MVC)>http://www.cnblogs.com/insus/p/3414480. ...

随机推荐

  1. SQLite -- 分页查询

    原文:http://blog.csdn.net/lu1024188315/article/details/51734514 参考:http://www.runoob.com/sqlite/sqlite ...

  2. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  3. 动态添加LinearLayout的高度

    WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); int width ...

  4. TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之一

    Marionette牵线木偶,Backbone是脊骨的意思,Marionette是基于Backbone做扩展库,可以理解为把脊骨骨架绑线扯着变成牵线木偶动起来哈哈,使backbone更易使用呵呵! 构 ...

  5. 与IE奋战的血泪史

    IE6下font-size会撑高元素,也就是说IE6下元素的最小高度为font-size的高度(蛋疼) IE6不支持两个class 例如 .a.b,类名不支持下划线开头 通过js设置样式带下划线的样式 ...

  6. 如何保存微信的小视频 How to keep WeChat 'Sights'

    微信小视频非常方便,但很难将其下载到本地电脑长期保存.网上有介绍方法,如百度经验上办法,但目前看来它可能只适用安卓系统,而且或已失效(可能由于版本更新).对Windows Phone无效,而对于更加封 ...

  7. Android入门(七):Spinner下拉式菜单组件

    对于手机和平板电脑的应用程序来说,打字是非常不方便的操作方式,比较好的方式就是列出一组选项让用户挑选,这样就可以避免打字的麻烦.使用Spinner下拉菜单组件需要完成以下几个步骤: 1.建立选项列表, ...

  8. A*算法——启发式搜索

    A*算法 本质还是搜索:加了优化而已 关于这个优化,听到两种说法: 1.剪枝 通过判断预计最少还要几步,加强版剪枝 比如说一个经典剪枝: 如果 步数≥已知最小值 则 剪枝 升级| V 如果 步数+最少 ...

  9. Python获取当前日期及时间

    import time def GetNowTime(): return time.strftime("%Y%m%d%H%M%S",time.localtime(time.time ...

  10. StringUtils中的常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...