SHOW:
|
|
- or go back to the newest paste.
1 | #Region "Page Load" | |
2 | Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load | |
3 | pnlSelectOrg.Visible = True | |
4 | pnlEditOrg.Visible = False | |
5 | pnlOrgUsers.Visible = False | |
6 | pnlAddUser.Visible = False | |
7 | ||
8 | - | ' Fill the DropDownList |
8 | + | |
9 | BindOrgs() | |
10 | End If | |
11 | ||
12 | If ddlOrgs.SelectedItem.Text = "Please select an organization to edit" Then | |
13 | lnkEditOrg.Enabled = False | |
14 | End If | |
15 | - | End Sub |
15 | + | End Sub |
16 | ||
17 | Private Sub BindOrgs() | |
18 | If ddlOrgs.Items.Count = 0 Then | |
19 | ' We don't want to have an organization loaded from default so put a dummy item in the drop down list | |
20 | ddlOrgs.Items.Add("Please select an organization to edit") | |
21 | Else | |
22 | ddlOrgs.Items.Clear() | |
23 | ddlOrgs.Items.Add("Please select an organization to edit") | |
24 | End If | |
25 | ||
26 | ' Since binding the drop down list to the dataset is going to remove our dummy item, add items in a loop from | |
27 | ' the dataset | |
28 | ||
29 | Dim sql As String = "Select * from Organizations" | |
30 | dsOrg = d.GetDataSet(sql, "text") | |
31 | For i As Integer = 0 To dsOrg.Tables(0).Rows.Count - 1 | |
32 | ddlOrgs.Items.Add(dsOrg.Tables(0).Rows(i)("OrganizationName")) | |
33 | ddlOrgs.Items(i + 1).Value = dsOrg.Tables(0).Rows(i)("OrganizationID") | |
34 | Next | |
35 | End Sub | |
36 | #End Region | |
37 | ||
38 | #Region "Organizations" | |
39 | Protected Sub ddlOrgs_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrgs.SelectedIndexChanged | |
40 | Dim targetOrg As Integer = CInt(ddlOrgs.SelectedItem.Value) | |
41 | Dim sql As String = _ | |
42 | "select * from Users as a " & _ | |
43 | "inner join OrgUsers as b on a.UserID = b.UserID where b.OrgID = " & ddlOrgs.SelectedItem.Value | |
44 | Dim drUsers As SqlDataReader = d.GetReader(sql) | |
45 | grdOrgUsers.DataSource = drUsers | |
46 | grdOrgUsers.DataBind() | |
47 | drUsers.Close() | |
48 | ||
49 | pnlOrgUsers.Visible = True | |
50 | GetOrgModules(ddlOrgs.SelectedItem.Value) | |
51 | pnlOrgModules.Visible = True | |
52 | ||
53 | lnkEditOrg.Enabled = True | |
54 | chkModules.Enabled = False | |
55 | End Sub | |
56 | ||
57 | Protected Sub lnkEditOrg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkEditOrg.Click | |
58 | txtModOrgName.Text = ddlOrgs.SelectedItem.Text | |
59 | pnlSelectOrg.Visible = False | |
60 | pnlEditOrg.Visible = True | |
61 | GetOrgModules(ddlOrgs.SelectedItem.Value) | |
62 | chkModules.Visible = True | |
63 | chkModules.Enabled = True | |
64 | End Sub | |
65 | ||
66 | Protected Sub lnkSaveModOrg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSaveModOrg.Click | |
67 | ' Get the list of Modules selected for this organization | |
68 | Dim ModuleList As New List(Of Integer) | |
69 | For i As Integer = 0 To chkModules.Items.Count - 1 | |
70 | If chkModules.Items(i).Selected Then | |
71 | ModuleList.Add(chkModules.Items(i).Value) | |
72 | End If | |
73 | Next | |
74 | ||
75 | ' Update the organization name (new/edit) | |
76 | If ddlOrgs.SelectedItem.Text = "Please select an organization to edit" Then | |
77 | lblError.Text = "Please select an organization to edit" | |
78 | ElseIf txtModOrgName.Text = ddlOrgs.SelectedItem.Text Then | |
79 | AddUpdateOrg(txtModOrgName.Text, "Update", ddlOrgs.SelectedItem.Value) | |
80 | Else | |
81 | AddUpdateOrg(txtModOrgName.Text, "Add") | |
82 | End If | |
83 | ||
84 | BindOrgs() | |
85 | chkModules.Enabled = False | |
86 | pnlOrgUsers.Visible = True | |
87 | End Sub | |
88 | ||
89 | Private Sub AddUpdateOrg(ByVal OrganizationName As String, ByVal Action As String, Optional ByVal Target As Integer = Nothing) | |
90 | ' Get the list of Modules selected for this organization | |
91 | Dim ModuleList As New List(Of Integer) | |
92 | For i As Integer = 0 To chkModules.Items.Count - 1 | |
93 | If chkModules.Items(i).Selected Then | |
94 | ModuleList.Add(chkModules.Items(i).Value) | |
95 | End If | |
96 | Next | |
97 | ||
98 | Dim sql As String = Nothing | |
99 | ||
100 | Select Case Action | |
101 | Case "Add" | |
102 | sql = "insert into Organizations(OrganizationName) values ('" & OrganizationName & "')" | |
103 | ||
104 | Case "Update" | |
105 | sql = "update Organizations set OrganizationName = '" & OrganizationName & "') " & _ | |
106 | "where OrganizationID = " & Target | |
107 | ||
108 | End Select | |
109 | ||
110 | Dim dr As SqlDataReader = d.GetReader("select * from OrgModules where OrgID = " & Target) | |
111 | If dr.HasRows Then | |
112 | dr.Close() | |
113 | UpdateModules(Target, ModuleList, "update") | |
114 | Else | |
115 | dr.Close() | |
116 | UpdateModules(Target, ModuleList, "insert") | |
117 | End If | |
118 | d.DoCommand(sql) | |
119 | End Sub | |
120 | ||
121 | Private Sub UpdateModules(ByVal OrganizationID As Integer, ByVal ModuleList As List(Of Integer), ByVal Action As String) | |
122 | If Action = "insert" Then | |
123 | For i As Integer = 0 To ModuleList.Count - 1 | |
124 | Dim sql As String = "insert into OrgModules(OrgID, ModuleID) values(" & OrganizationID & ", " & ModuleList.Item(i) & ")" | |
125 | d.DoCommand(sql) | |
126 | Next | |
127 | Else | |
128 | For i As Integer = 0 To ModuleList.Count - 1 | |
129 | Dim sql As String = "update OrgModules set ModuleID = " & ModuleList.Item(i) & " where OrgID = " & OrganizationID | |
130 | d.DoCommand(sql) | |
131 | Next | |
132 | End If | |
133 | ||
134 | GetOrgModules(OrganizationID) | |
135 | End Sub | |
136 | ||
137 | Protected Sub lnkCancelModOrg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCancelModOrg.Click | |
138 | pnlSelectOrg.Visible = True | |
139 | pnlOrgUsers.Visible = True | |
140 | pnlEditOrg.Visible = False | |
141 | End Sub | |
142 | ||
143 | Protected Sub lnkNewOrg_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNewOrg.Click | |
144 | pnlEditOrg.Visible = True | |
145 | pnlSelectOrg.Visible = False | |
146 | End Sub | |
147 | ||
148 | Private Sub GetOrgModules(ByVal OrganizationID As Integer) | |
149 | ' Clear all currently selected modules for this Organization | |
150 | Dim sql As String = "delete * from OrgModules where OrgID = " & OrganizationID | |
151 | d.DoCommand(sql) | |
152 | ||
153 | sql = "select ModuleID, ModuleName from Modules" | |
154 | Dim dr As SqlDataReader = d.GetReader(sql) | |
155 | ||
156 | chkModules.DataSource = dr | |
157 | chkModules.DataTextField = "ModuleName" | |
158 | chkModules.DataValueField = "ModuleID" | |
159 | chkModules.DataBind() | |
160 | dr.Close() | |
161 | ||
162 | sql = "select a.ModuleID, b.ModuleName from OrgModules as a" & vbCrLf & _ | |
163 | "inner join Modules as b on a.ModuleID = b.ModuleID" & vbCrLf & _ | |
164 | "where a.OrgID = " & OrganizationID | |
165 | Dim drMod = d.GetReader(sql) | |
166 | If drMod.HasRows Then | |
167 | chkModules.ClearSelection() | |
168 | While drMod.Read | |
169 | For i As Integer = 0 To chkModules.Items.Count - 1 | |
170 | If chkModules.Items(i).Value = drMod.GetValue(0) Then | |
171 | chkModules.Items(i).Selected = True | |
172 | Exit For | |
173 | End If | |
174 | Next | |
175 | End While | |
176 | drMod.Close() | |
177 | Else | |
178 | chkModules.Visible = False | |
179 | End If | |
180 | End Sub | |
181 | #End Region | |
182 | ||
183 |