Here I am making a dependency of a child window on a parent and a parent window on a child. In other words when I open a child window (Modal window) then I will read some values from the parent window and will pass it on a modal window. And after performing operations on the child window, in ther words modal window, I will refresh the parent window with an updated database.

The following is my scenario.

I have an Employee Table and I am showing and adding a new employee from a screen. When adding a new employee I need to select a Manager (Project Leader) and project. And suppose I want to add a new project under the selected manager then I am opening a modal window and adding a new manager.

The following are my 2 tables:

1. Employee Table

table
Figure 1.

The following is the script of the Employee table:

  1. CREATETABLE [dbo].[Employee](
  2. [ID] [int] IDENTITY(1,1)NOTNULL,
  3. [Name] [varchar](50)NULL,
  4. [Email] [varchar](500)NULL,
  5. [Country] [varchar](50)NULL,
  6. [ProjectID] [int] NULL,
  7. [ManagerName] [varchar](50)NULL,
  8. CONSTRAINT [PK_Employee] PRIMARYKEYCLUSTERED
  9. (
  10. [ID] ASC
  11. )WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
  12. )ON [PRIMARY]
  13. GO
2. Project Table

Project Table
Figure 2.

The following is the script of the Project table:
  1. CREATETABLE [dbo].[Project](
  2. [ProjectID] [int] IDENTITY(1,1)NOTNULL,
  3. [ProjectName] [varchar](50)NULL,
  4. [ProjectLeader] [varchar](50)NULL,
  5. CONSTRAINT [PK_Project] PRIMARYKEYCLUSTERED
  6. (
  7. [ProjectID] ASC
  8. )WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
  9. )ON [PRIMARY]
  10. GO
Now create a new Visual Studio project and add a page to the Add New Employee and show all the Emoloyees.

Here add a jQuery reference with the following aspx code:
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ManageEmployee.aspx.cs"Inherits="jQueryPopUp.ManageEmployee"EnableEventValidation="false"%>
  2. <!DOCTYPEhtml>
  3. <html
  4. xmlns="http://www.w3.org/1999/xhtml">
  5. <headrunat="server">
  6. <title>jQuery Modal PopUp</title>
  7. <scriptsrc="Scripts/jquery-2.1.4.min.js">
  8. </script>
  9. <linkhref="R-ModalPopUp.css"rel="stylesheet"/>
  10. <scriptsrc="Scripts/jquery-ui.min.js">
  11. </script>
  12. <styletype="text/css">
  13. .auto-style1 {
  14. width: auto;
  15. position: relative;
  16. left: 20px;
  17. width: 100%;
  18. }
  19. </style>
  20. <scripttype="text/javascript">
  21. $(document).ready(function () {
  22. $("#ddlProject").append($("
  23. <option></option>").val('0').html("--Select Project--"));
  24. //Filling Manager Drop Down List
  25. $.ajax({
  26. type: "POST",
  27. contentType: "application/json; charset=utf-8",
  28. url: "ManageEmployee.aspx/BindManager",
  29. data: "{}",
  30. dataType: "json",
  31. success: function (data) {
  32. $("#ddlManager").append($("
  33. <option></option>").val('0').html("--Select Manager--"));
  34. $.each(data.d, function (key, value) {
  35. $("#ddlManager").append($("
  36. <option></option>").val(value.ManagerName).html(value.ManagerName));
  37. });
  38. },
  39. error: function (result) {
  40. alert("Error");
  41. }
  42. });
  43. $('#ddlManager').change(function () {
  44. varSelectedText = $(this).find(":selected").text();
  45. varSelectedValue = $(this).val();
  46. varJSONObject = { "ManagerID": SelectedText };
  47. varjsonData = JSON.stringify(JSONObject);
  48. $.ajax({
  49. type: "POST",
  50. contentType: "application/json; charset=utf-8",
  51. url: "ManageEmployee.aspx/BindManagerProject",
  52. data: jsonData,
  53. dataType: "json",
  54. success: function (data) {
  55. $('#ddlProject').empty();
  56. $("#ddlProject").append($("
  57. <option></option>").val('0').html("--Select Project--"));
  58. $.each(data.d, function (key, value) {
  59. $("#ddlProject").append($("
  60. <option></option>").val(value.ProjectID).html(value.ProjectName));
  61. });
  62. },
  63. error: function (result) {
  64. alert("Error");
  65. }
  66. });
  67. });
  68. $('#gvData').empty();
  69. $.ajax({
  70. type: "POST",
  71. contentType: "application/json; charset=utf-8",
  72. url: "ManageEmployee.aspx/BindEmployees",
  73. data: "{}",
  74. dataType: "json",
  75. success: function (result) {
  76. $("#gvData").append("
  77. <tr style='background-color:red; color:white; font-weight:bold;'>
  78. <td style='text-align:left;'>ID</td>
  79. <td style='text-align:left;'>Name</td>
  80. <td style='text-align:left;'>Email</td>
  81. <td style='text-align:left;'>Country</td>
  82. <td style='text-align:left;'>Project name</td>
  83. <td style='text-align:left;'>Manager Name</td>
  84. </tr>");
  85. for (var i = 0; i
  86. <result.d.length; i++) {
  87. if (i % 2 == 0) {
  88. $("#gvData").append("
  89. <tr style='background-color:#F5FBEF; font-family:Verdana; font-size:10pt ;'>
  90. <td style='text-align:left;'>" + result.d[i].ID + "</td>
  91. <td style='text-align:left;'>" + result.d[i].Name + "</td>
  92. <td style='text-align:left;'>" + result.d[i].Email + "</td>
  93. <td style='text-align:left;'>" + result.d[i].Country + "</td>
  94. <td style='text-align:left;'>" + result.d[i].ProjectID + "</td>
  95. <td style='text-align:left;'>" + result.d[i].ManagerName + "</td>
  96. </tr>");
  97. }
  98. else {
  99. $("#gvData").append("
  100. <tr style='background-color:skyblue; font-family:Verdana; font-size:10pt ;'>
  101. <td style='text-align:left;'>" + result.d[i].ID + "</td>
  102. <td style='text-align:left;'>" + result.d[i].Name + "</td>
  103. <td style='text-align:left;'>" + result.d[i].Email + "</td>
  104. <td style='text-align:left;'>" + result.d[i].Country + "</td>
  105. <td style='text-align:left;'>" + result.d[i].ProjectID + "</td>
  106. <td style='text-align:left;'>" + result.d[i].ManagerName + "</td>
  107. </tr>");
  108. }
  109. }
  110. },
  111. error: function (result) {
  112. alert("Error");
  113. }
  114. });
  115. $('#btnAddProject').click(function () {
  116. var id = '#dialog';
  117. varManagerName = $('#ddlManager').val();
  118. varManagerText = $('#ddlManager :selected').text();
  119. $("#lblManagerName").text(ManagerName);
  120. varmaskHeight = $(document).height();
  121. varmaskWidth = $(document).width();
  122. $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
  123. //transition effect
  124. $('#mask').fadeIn(1000);
  125. $('#mask').fadeTo("slow", 0.8);
  126. //Get the window height and width
  127. varwinH = $(window).height();
  128. varwinW = $(window).width();
  129. //Set the popup window to center
  130. $(id).css('top', winH / 2 - $(id).height() / 2);
  131. $(id).css('left', winW / 2 - $(id).width() / 2);
  132. //transition effect
  133. $(id).fadeIn(2000);
  134. returnfalse;
  135. //if close button is clicked
  136. });
  137. $('.window .close').click(function (e) {
  138. e.preventDefault();
  139. $('#mask').hide();
  140. $('.window').hide();
  141. });
  142. $('#btnUpdate').click(function (e) {
  143. var Manager = $('#lblManagerName').text();
  144. var Project = $('#txtProjectName').val();
  145. varJSONObject = { "Manager": Manager, "Project": Project };
  146. varjsonData = JSON.stringify(JSONObject);
  147. $.ajax({
  148. type: "POST",
  149. contentType: "application/json; charset=utf-8",
  150. url: "ManageEmployee.aspx/AddProjectWithManager",
  151. data: jsonData,
  152. dataType: "json",
  153. success: function (data) {
  154. },
  155. error: function (result) {
  156. alert("Error");
  157. }
  158. });
  159. varJSONObject = { "ManagerID": $("#lblManagerName").text() };
  160. varjsonData = JSON.stringify(JSONObject);
  161. $.ajax({
  162. type: "POST",
  163. contentType: "application/json; charset=utf-8",
  164. url: "ManageEmployee.aspx/BindManagerProject",
  165. data: jsonData,
  166. dataType: "json",
  167. success: function (data) {
  168. $('#ddlProject').empty();
  169. $("#ddlProject").append($("
  170. <option></option>").val('0').html("--Select Project--"));
  171. $.each(data.d, function (key, value) {
  172. $("#ddlProject").append($("
  173. <option></option>").val(value.ProjectID).html(value.ProjectName));
  174. });
  175. },
  176. error: function (result) {
  177. alert("Error");
  178. }
  179. });
  180. e.preventDefault();
  181. $('#mask').hide();
  182. $('.window').hide();
  183. });
  184. //Add Employee
  185. $('#btnAddEmployee').click(function (e) {
  186. debugger;
  187. varempName = $('#txtName').val();
  188. var email = $('#txtEmail').val();
  189. var country = $('#ddlCountry').val();
  190. var project = $('#ddlProject').val();
  191. var manager = $('#ddlManager').val();
  192. varJSONObject = { "Name": empName, "Email": email, "Country": country, "Project": project, "Manager": manager };
  193. varjsonData = JSON.stringify(JSONObject);
  194. $.ajax({
  195. type: "POST",
  196. contentType: "application/json; charset=utf-8",
  197. url: "ManageEmployee.aspx/AddNewEmployee",
  198. data: jsonData,
  199. dataType: "json",
  200. success: function (data) {
  201. },
  202. error: function (result) {
  203. alert("Error");
  204. }
  205. });
  206. $('#gvData').empty();
  207. $.ajax({
  208. type: "POST",
  209. contentType: "application/json; charset=utf-8",
  210. url: "ManageEmployee.aspx/BindEmployees",
  211. data: "{}",
  212. dataType: "json",
  213. success: function (result) {
  214. for (var i = 0; i
  215. <result.d.length; i++) {
  216. $("#gvData").append("
  217. <tr>
  218. <td>" + result.d[i].ID + "</td>
  219. <td>" + result.d[i].Name + "</td>
  220. <td>" + result.d[i].Email + "</td>
  221. <td>" + result.d[i].Country + "</td>
  222. <td>" + result.d[i].ProjectID + "</td>
  223. <td>" + result.d[i].ManagerName + "</td>
  224. </tr>");
  225. }
  226. },
  227. error: function (result) {
  228. alert("Error");
  229. }
  230. });
  231. });
  232. });
  233. </script>
  234. </head>
  235. <body>
  236. <formid="form1"runat="server">
  237. <div>
  238. <tablestyle="width: 100%; text-align: center; border: solid5pxred; background-color: yellow; vertical-align: top;">
  239. <tr>
  240. <td>
  241. <div>
  242. <fieldsetstyle="width: 99%;">
  243. <legendstyle="font-size: 20pt; color: red; font-family: Verdana">jQuery Modal Popup Show with (Parent - Child window) Value dependency
  244. </legend>
  245. <tablestyle="width: 100%;">
  246. <tr>
  247. <tdstyle="vertical-align: top; width: 20%;">
  248. <tablestyle="background-color: skyblue; width: 100%; text-align: left;">
  249. <tr>
  250. <tdcolspan="2"style="background-color: #DF0101; color: white; border: 1pxsolidred; font-weight:bold; padding-left: 5px;">Add New Employee
  251. </td>
  252. </tr>
  253. <tr>
  254. <tdstyle="text-align: left; padding-left: 5px;">
  255. <asp:LabelID="lblName"runat="server"Text="Name:"Width="80px">
  256. </asp:Label>
  257. </td>
  258. <tdstyle="text-align: left;">
  259. <asp:TextBoxID="txtName"runat="server">
  260. </asp:TextBox>
  261. </td>
  262. </tr>
  263. <tr>
  264. <tdstyle="text-align: left; padding-left: 5px;">
  265. <asp:LabelID="Label1"runat="server"Text="Email:"Width="80px">
  266. </asp:Label>
  267. </td>
  268. <tdstyle="text-align: left;">
  269. <asp:TextBoxID="txtEmail"runat="server">
  270. </asp:TextBox>
  271. </td>
  272. </tr>
  273. <tr>
  274. <tdstyle="text-align: left; padding-left: 5px;">
  275. <asp:LabelID="Label2"runat="server"Text="Country:"Width="80px">
  276. </asp:Label>
  277. </td>
  278. <tdstyle="text-align: left;">
  279. <asp:DropDownListID="ddlCountry"runat="server">
  280. <asp:ListItemText="India"Value="India">
  281. </asp:ListItem>
  282. <asp:ListItemText="USA"Value="USA">
  283. </asp:ListItem>
  284. <asp:ListItemText="South Africa"Value="South Africa">
  285. </asp:ListItem>
  286. <asp:ListItemText="Singapore"Value="Singapore">
  287. </asp:ListItem>
  288. </asp:DropDownList>
  289. </td>
  290. </tr>
  291. <tr>
  292. <tdstyle="text-align: left; padding-left: 5px;">
  293. <asp:LabelID="Label3"runat="server"Text="Manager:"Width="80px">
  294. </asp:Label>
  295. </td>
  296. <tdstyle="text-align: left;">
  297. <asp:DropDownListID="ddlManager"runat="server">
  298. </asp:DropDownList>
  299. </td>
  300. </tr>
  301. <tr>
  302. <tdstyle="text-align: left; padding-left: 5px;">
  303. <asp:LabelID="Label4"runat="server"Text="Project:"Width="80px">
  304. </asp:Label>
  305. </td>
  306. <tdstyle="text-align: left;">
  307. <asp:DropDownListID="ddlProject"runat="server">
  308. </asp:DropDownList>
  309. <br/>
  310. <inputid="btnAddProject"type="submit"value="Add Project"style="width: 100px;"class="btnbtn-info"/>
  311. </td>
  312. </tr>
  313. <tr>
  314. <td></td>
  315. <td>
  316. <inputid="btnAddEmployee"type="submit"value="Add Employee"style="width: 140px;"class="btnbtn-info"/>
  317. </td>
  318. </tr>
  319. </table>
  320. </td>
  321. <tdstyle="vertical-align: top; background-color: green; text-align: center;">
  322. <asp:GridViewID="gvData"runat="server"CellPadding="4"ShowHeaderWhenEmpty="True"BackColor="White"BorderColor="#CC9966"BorderStyle="None"Width="100%"BorderWidth="1px">
  323. <FooterStyleBackColor="#FFFFCC"ForeColor="#330099"/>
  324. <HeaderStyleBackColor="#990000"Font-Bold="True"ForeColor="#FFFFCC"/>
  325. <PagerStyleBackColor="#FFFFCC"ForeColor="#330099"HorizontalAlign="Center"/>
  326. <RowStyleBackColor="White"ForeColor="#330099"/>
  327. <SelectedRowStyleBackColor="#FFCC66"Font-Bold="True"ForeColor="#663399"/>
  328. <SortedAscendingCellStyleBackColor="#FEFCEB"/>
  329. <SortedAscendingHeaderStyleBackColor="#AF0101"/>
  330. <SortedDescendingCellStyleBackColor="#F6F0C0"/>
  331. <SortedDescendingHeaderStyleBackColor="#7E0000"/>
  332. </asp:GridView>
  333. </td>
  334. </tr>
  335. </table>
  336. </fieldset>
  337. </div>
  338. </td>
  339. </tr>
  340. </table>
  341. </div>
  342. <divid="boxes">
  343. <divid="mask">
  344. <divid="dialog"class="window">
  345. <divid="headerBorder">
  346. Add New Project #
  347. <divid="close"class="close">[X]
  348. </div>
  349. </div>
  350. <tableclass="auto-style1">
  351. <tr>
  352. <tdstyle="text-align: left;">Manager Name:
  353. </td>
  354. <td>
  355. <asp:LabelID="lblManagerName"runat="server">
  356. </asp:Label>
  357. </td>
  358. </tr>
  359. <tr>
  360. <tdstyle="text-align: left;">Project Name:
  361. </td>
  362. <td>
  363. <inputtype="text"style="width: 300px;"id="txtProjectName"/>
  364. </td>
  365. </tr>
  366. <tr>
  367. <td></td>
  368. </tr>
  369. <tr>
  370. <td></td>
  371. <td>
  372. <buttonid="btnUpdate">Submit
  373. </button>
  374. <inputtype="reset"/>
  375. </td>
  376. </tr>
  377. </table>
  378. </div>
  379. </div>
  380. </div>
  381. </form>
  382. </body>
  383. </html>
The following is the aspx.cs code:
  1. using System;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Data;
  4. usingSystem.Data.SqlClient;
  5. usingSystem.Linq;
  6. usingSystem.Web;
  7. usingSystem.Web.Services;
  8. usingSystem.Web.UI;
  9. usingSystem.Web.UI.WebControls;
  10. namespacejQueryPopUp {
  11. publicpartialclassManageEmployee: System.Web.UI.Page {
  12. protectedvoidPage_Load(object sender, EventArgs e) {
  13. if (!Page.IsPostBack) {
  14. BindGridWithDummyRow();
  15. }
  16. }
  17. publicvoidBindGridWithDummyRow() {
  18. DataTabledt = newDataTable();
  19. dt.Columns.Add("ID");
  20. dt.Columns.Add("Name");
  21. dt.Columns.Add("Email");
  22. dt.Columns.Add("Country");
  23. dt.Columns.Add("ProjectID");
  24. dt.Columns.Add("ManagerName");
  25. gvData.DataSource = dt;
  26. gvData.DataBind();
  27. }
  28. [WebMethod]
  29. publicstaticEmployee[] BindEmployees() {
  30. stringconnectionString = @
  31. "Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  32. DataTabledt = newDataTable();
  33. List < Employee > employeeList = newList < Employee > ();
  34. using(SqlConnection con = newSqlConnection(connectionString)) {
  35. using(SqlCommand command = newSqlCommand("select e.ID, e.Name,e.Email,e.Country,p.ProjectName,e.ManagerName from Employee as e Inner join project as p on e.ProjectID=p.ProjectID", con)) {
  36. con.Open();
  37. SqlDataAdapter da = newSqlDataAdapter(command);
  38. da.Fill(dt);
  39. foreach(DataRowdtrowindt.Rows) {
  40. Employeeemployee = newEmployee();
  41. employee.ID = Convert.ToInt32(dtrow["ID"].ToString());
  42. employee.Name = dtrow["Name"].ToString();
  43. employee.Email = dtrow["Email"].ToString();
  44. employee.Country = dtrow["Country"].ToString();
  45. employee.ProjectID = dtrow["ProjectName"].ToString();
  46. employee.ManagerName = dtrow["ManagerName"].ToString();
  47. employeeList.Add(employee);
  48. }
  49. }
  50. }
  51. returnemployeeList.ToArray();
  52. }
  53. [WebMethod]
  54. publicstaticEmployee[] BindManager() {
  55. stringconnectionString = @
  56. "Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  57. DataTabledt = newDataTable();
  58. List < Employee > employeeList = newList < Employee > ();
  59. using(SqlConnection con = newSqlConnection(connectionString)) {
  60. using(SqlCommand command = newSqlCommand("SELECT DISTINCT ProjectLeader from Project", con)) {
  61. con.Open();
  62. SqlDataAdapter da = newSqlDataAdapter(command);
  63. da.Fill(dt);
  64. foreach(DataRowdtrowindt.Rows) {
  65. Employeeemployee = newEmployee();
  66. employee.ManagerName = dtrow["ProjectLeader"].ToString();
  67. employeeList.Add(employee);
  68. }
  69. }
  70. }
  71. returnemployeeList.ToArray();
  72. }
  73. [WebMethod]
  74. publicstaticEmployee[] BindManagerProject(stringManagerID) {
  75. stringconnectionString = @
  76. "Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  77. DataTabledt = newDataTable();
  78. List < Employee > employeeList = newList < Employee > ();
  79. using(SqlConnection con = newSqlConnection(connectionString)) {
  80. using(SqlCommand command = newSqlCommand("SELECT ProjectID, ProjectName from Project WHERE ProjectLeader='" + ManagerID + "'", con)) {
  81. con.Open();
  82. SqlDataAdapter da = newSqlDataAdapter(command);
  83. da.Fill(dt);
  84. foreach(DataRowdtrowindt.Rows) {
  85. Employeeemployee = newEmployee();
  86. employee.ProjectID = dtrow["ProjectID"].ToString();
  87. employee.ProjectName = dtrow["ProjectName"].ToString();
  88. employeeList.Add(employee);
  89. }
  90. }
  91. }
  92. returnemployeeList.ToArray();
  93. }
  94. [WebMethod]
  95. publicstaticvoidAddProjectWithManager(string Manager, string Project) {
  96. stringconnectionString = @
  97. "Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  98. DataTabledt = newDataTable();
  99. List < Employee > employeeList = newList < Employee > ();
  100. using(SqlConnection con = newSqlConnection(connectionString)) {
  101. using(SqlCommand command = newSqlCommand("INSERT INTO Project (ProjectName, ProjectLeader) VALUES ('" + Project + "' , '" + Manager + "')", con)) {
  102. con.Open();
  103. SqlDataAdapter da = newSqlDataAdapter(command);
  104. da.Fill(dt);
  105. }
  106. }
  107. }
  108. [WebMethod]
  109. publicstaticvoidAddNewEmployee(string Name, string Email, string Country, string Project, string Manager) {
  110. stringconnectionString = @
  111. "Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  112. DataTabledt = newDataTable();
  113. List < Employee > employeeList = newList < Employee > ();
  114. using(SqlConnection con = newSqlConnection(connectionString)) {
  115. using(SqlCommand command = newSqlCommand("INSERT INTO Employee (Name, Email, Country, ProjectID, ManagerName) VALUES ('" + Name + "' , '" + Email + "' , '" + Country + "' , '" + Project + "' , '" + Manager + "')", con)) {
  116. con.Open();
  117. SqlDataAdapter da = newSqlDataAdapter(command);
  118. da.Fill(dt);
  119. }
  120. }
  121. }
  122. }
  123. }
The following is the Employee class:
  1. using System;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. namespacejQueryPopUp {
  6. publicclassEmployee {
  7. publicint ID {
  8. get;
  9. set;
  10. }
  11. publicstring Name {
  12. get;
  13. set;
  14. }
  15. publicstring Email {
  16. get;
  17. set;
  18. }
  19. publicstring Country {
  20. get;
  21. set;
  22. }
  23. publicstringProjectID {
  24. get;
  25. set;
  26. }
  27. publicstringProjectName {
  28. get;
  29. set;
  30. }
  31. publicstringManagerName {
  32. get;
  33. set;
  34. }
  35. }
  36. }
Now run your application.

Here this screen will show the entire employee list and you can add a new employee from here:

employee from
Figure 3.

Now when adding a new employee we need to select a manager as in the following:

adding new employee
Figure 4.

On selecting a manager a Project drop down will fill:

manager
Figure 5.

If you want to add a new project under the selected manager then click on the Add Project button. You will see a child window showing the value from the parent window, Manager Name.

add new project
Figure 6.

Now add a new project and you will see this new project is showing in the project drop down. In other words after adding a project I am refresing the parent window with the current values in the database as in the following:

select project
Figure 7.

Now add an employee and it will show in the grid:

output
Figure 8.