码迷,mamicode.com
首页 > Web开发 > 详细

.net DropDownList静态联动

时间:2016-05-16 14:34:20      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

1.前台

 1 <span id="spnClient" style="margin-left: 30px; margin-top: 10px">
 2 <lable>Client: </lable>
 3 <asp:DropDownList ID="ddlClient" DataValueField="ID" DataTextField="NAME" runat="server" />
 4 </span>
 5 
 6 <span id="spnProject" style="margin-left: 30px; margin-top: 10px">
 7 <lable>Project: </lable>
 8 <asp:DropDownList ID="ddlProject" DataValueField="ID" DataTextField="NAME" runat="server" />
 9 </span>
10 <span id="spnUser" style="display: none; margin-left: 30px; margin-top: 10px;">
11 <lable>User: </lable>
12 <asp:DropDownList ID="ddlUser" DataValueField="ID" DataTextField="USER_NAME" runat="server" />
13 </span>

2.绑定DataSource

 1        private void LoadDropDownList()
 2         {
 3             DataTable dtYear =SplendidCache.Years();
 4             ddlYEAR.DataSource = dtYear;
 5             ddlYEAR.DataBind();
 6             ddlYEAR.SelectedValue = DateTime.Now.Year.ToString();
 7 
 8             DataTable dtClient = SplendidCache.Client(true);
 9             ddlClient.DataSource = dtClient;
10             ddlClient.DataBind();
11 
12             DataTable dtProject = SplendidCache.Project();
13             ddlProject.DataSource = dtProject;
14             ddlProject.DataBind();
15 
16             DataTable dtUser = SplendidCache.AssignedUser();
17             ddlUser.DataSource = dtUser;
18             ddlUser.DataBind();
19 
20             BindProjects();
21             BindUsers();
22         }
 1         private void BindProjects()
 2         {
 3             DataTable dtProjects = new DataTable();
 4             DbProviderFactory dbf = DbProviderFactories.GetFactory();
 5             using (IDbConnection con = dbf.CreateConnection())
 6             {
 7                 con.Open();
 8                 using (IDbCommand cmd = con.CreateCommand())
 9                 {
10                     string sSQL = "SELECT ID,NAME,CLIENTID FROM Project WHERE DELETED=0    ORDER BY NAME " + ControlChars.CrLf;
11                     cmd.CommandText = sSQL;
12                     using (DbDataAdapter da = dbf.CreateDataAdapter())
13                     {
14                         ((IDbDataAdapter)da).SelectCommand = cmd;
15                         da.Fill(dtProjects);
16                         StringBuilder sbProjects = new StringBuilder();
17                         if (dtProjects != null && dtProjects.Rows.Count > 0)
18                         {
19                             sbProjects.Append("{\"project\":[");
20                             int i = 1;
21                             int count = dtProjects.Rows.Count;
22                             foreach (DataRow row in dtProjects.Rows)
23                             {
24                                 if (i == count)
25                                 {
26                                     sbProjects.Append("{\"client\":\"" + Sql.ToString(row["CLIENTID"]) + "\",\"id\":\"" + Sql.ToString(row["ID"]) + "\",\"name\":\"" + Sql.ToString(row["NAME"]).Replace("","") + "\"}");
27                                 }
28                                 else
29                                 {
30                                     sbProjects.Append("{\"client\":\"" + Sql.ToString(row["CLIENTID"]) + "\",\"id\":\"" + Sql.ToString(row["ID"]) + "\",\"name\":\"" + Sql.ToString(row["NAME"]).Replace("", "") + "\"},");
31                                 }
32                                 i++;
33                             }
34                             sbProjects.Append("]");
35                         }
36                         sbProjects.Append("}");
37                         m_sProjectsJson = sbProjects.ToString();
38                     }
39                 }
40             }
41         }
42         private void BindUsers()
43         {
44             DataTable dtUsers = new DataTable();
45             DbProviderFactory dbf = DbProviderFactories.GetFactory();
46             using (IDbConnection con = dbf.CreateConnection())
47             {
48                 con.Open();
49                 using (IDbCommand cmd = con.CreateCommand())
50                 {
51                     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;
52                     cmd.CommandText = sSQL;
53                     using (DbDataAdapter da = dbf.CreateDataAdapter())
54                     {
55                         ((IDbDataAdapter)da).SelectCommand = cmd;
56                         da.Fill(dtUsers);
57                         StringBuilder sbUsers = new StringBuilder();
58                         if (dtUsers != null && dtUsers.Rows.Count > 0)
59                         {
60                             sbUsers.Append("{\"user\":[");
61                             int i = 1;
62                             int count = dtUsers.Rows.Count;
63                             foreach (DataRow row in dtUsers.Rows)
64                             {
65                                 if (i == count)
66                                 {
67                                     sbUsers.Append("{\"project\":\"" + Sql.ToString(row["PROJECT_ID"]) + "\",\"id\":\"" + Sql.ToString(row["USER_ID"]) + "\",\"name\":\"" + Sql.ToString(row["USER_NAME"]) + "\"}");
68                                 }
69                                 else
70                                 {
71                                     sbUsers.Append("{\"project\":\"" + Sql.ToString(row["PROJECT_ID"]) + "\",\"id\":\"" + Sql.ToString(row["USER_ID"]) + "\",\"name\":\"" + Sql.ToString(row["USER_NAME"]) + "\"},");
72                                 }
73                                 i++;
74                             }
75                             sbUsers.Append("]");
76                         }
77                         sbUsers.Append("}");
78                         m_sUserJson = sbUsers.ToString();
79                     }
80                 }
81             }
82         }
 1    private static string m_sProjectsJson = "";
 2         public static string ProjectsJson
 3         {
 4             get { return m_sProjectsJson; }
 5         }
 6 
 7         private static string m_sUserJson = "";
 8         public static string UsersJson
 9         {
10             get { return m_sUserJson; }
11         }

3.javascript

1     $("#<%=ddlClient.ClientID%>").change(function () {
2             loadproject();
3         });
4         $("#<%=ddlProject.ClientID%>").change(function () {
5             loaduser();
6         });
 1        function loadproject() {
 2             var client = $("#<%=ddlClient.ClientID%>").val();
 3             var projectArray = jQuery.parseJSON(<%= ProjectsJson%>);
 4             var filtered = $.grep(projectArray.project,
 5                            function (item) {
 6                                if (client != "") {
 7                                    return item.client == client;
 8                                }
 9                                else {
10                                    return item;
11                                }
12                            }
13                        );
14             $("#<%=ddlProject.ClientID%>").empty();
15             $.each(filtered, function (i, item) {
16                 $("#<%=ddlProject.ClientID%>").append("<option value=‘" + item.id + "‘>" + item.name + "</option>");
17             });
18         }
 1    function loaduser() {
 2             var project = $("#<%=ddlProject.ClientID%>").val();
 3             var userArray = jQuery.parseJSON(<%= UsersJson%>);
 4             var filtered = $.grep(userArray.user,
 5                             function (item) {
 6                                 if (project != "") {
 7                                     return item.project == project;
 8                                 }
 9                                 else {
10                                     return item;
11                                 }
12                             }
13                         );
14             $("#<%=ddlUser.ClientID%>").empty();
15             $.each(filtered, function (i, item) {
16                 $("#<%=ddlUser.ClientID%>").append("<option value=‘" + item.id + "‘>" + item.name + "</option>");
17             });
18         }

 

.net DropDownList静态联动

标签:

原文地址:http://www.cnblogs.com/kangjing/p/5497784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!