How to Auto-Refresh Grid View on Closing of Popup Menu


In today's article, you will learn how to Auto-Refresh a Grid View on the closing of a popup menu.
This article will take you many steps ahead of that article since this article will tell you how to Auto-Refresh a GridView on the closing of a popup menu that of a WebForm in which new entries were made.
Step 1
First of all, I created a database, "test", in which a table is created named "Popup". We will enter our data in this database through various controls that I used in the WebForm.
The fields created in the table are as follows:
  1. First Name
  2. Middle Name
  3. Last Name
  4. Gender
  5. Age
  6. City
  7. State
  8. Country
  9. Zip Code
Here Age and Zip Code are the int datatype and all others are nvarchar(50). We will make entries in this table at runtime using the Popup Form.
Step 2
Now we should move towards our .NET Application. In the ASP.NET I took two WebForms named "WebForm1" and "WebForm2". WebForm1 will be used to show the Grid View and it will be the base form of our application since on this form the Popup will be opened by clicking on the button provided.
My WebForm1 code is as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication26.WebForm1" %>  
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  4. <!DOCTYPE html>  
  6. <html xmlns="">  
  7. <head runat="server">  
  8.     <title></title>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  13. </asp:ScriptManager>  
  14. <asp:Button ID="Button1" runat="server" Text="Fill Form in Popup" />  
  15. <br />  
  16.         <br />  
  17.         <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">  
  18.             <FooterStyle BackColor="White" ForeColor="#000066" />  
  19.             <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />  
  20.             <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />  
  21.             <RowStyle ForeColor="#000066" />  
  22.             <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />  
  23.             <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  24.             <SortedAscendingHeaderStyle BackColor="#007DBB" />  
  25.             <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  26.             <SortedDescendingHeaderStyle BackColor="#00547E" />  
  27.         </asp:GridView>  
  29. <cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="Button1"  
  30.    BackgroundCssClass="Background">  
  31. </cc1:ModalPopupExtender>  
  32. <asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">  
  33.     <iframe style=" width: 380px; height: 360px;" id="irm1" src="WebForm2.aspx" runat="server"></iframe>  
  34. </asp:Panel>  
  35.     </form>  
  36. </body>  
  37. </html> 
As you can see that first I registered the Ajax Toolkit for my application. You can download the Ajax Toolkit either from here "AJAXTOOLKIT" or from other sites.
In the form Tag, I first used the ScriptManager Tag because I will use the Ajax Tools in my application, so before using the Ajax Tools you need to call the Script Manager.
Then I took a button that will be used to open the Popup Menu.
Then I took a GridView whose theme is set by clicking on the AutoFormat option of GridView.
Then I took a ModelPopupExtender in which you need to pass the Id of the Popup Control and Target Control that will open the Popup, now you will be wondering that in my previous article I had passed one more Id and that was of the control that will close the Popup, but here I haven't used such type of Id that's because in this application the Popup WebForm does have the close button, that button will close the Popup and will refresh the GridView as well.
Then a Panel is used with the iframe to open the WebForm2.
So until now,
our containers are ready and now the internal work is remaining.
Step 3
Now we will move towards the second WebForm that will be opened in the Popup.
On my second Webform I create a table in which some Labels and textboxes are used that will allow the user to make entries in them. I am providing you some part of the code so that you can understand how I created the complete form:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication26.WebForm2" %>  
  3. <!DOCTYPE html>  
  5. <html xmlns="">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <script src="jquery-1.9.1.js"></script>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <table>  
  14.     <tr>  
  15.     <td>  
  16.     <asp:Label ID="Label1" runat="server" CssClass="lbl" Text="First Name"></asp:Label>  
  17.     </td>  
  18.     <td>  
  19.     <asp:TextBox ID="TextBox1" runat="server" Font-Size="14px" ></asp:TextBox>  
  20.     </td>  
  21.     </tr>  
  22.     <tr>  
  23.     <td>  
  24.     <asp:Label ID="Label3" runat="server" CssClass="lbl" Text="Last Name"></asp:Label>  
  25.     </td>  
  26.     <td>  
  27.     <asp:TextBox ID="TextBox3" runat="server" Font-Size="14px" ></asp:TextBox>  
  28.     </td>  
  29.     </tr>  
  30.     <tr>  
  31.     <td>  
  32.     <asp:Label ID="Label9" runat="server" CssClass="lbl" Text="Zip Code"></asp:Label>  
  33.     </td>  
  34.     <td>  
  35.     <asp:TextBox ID="TextBox9" runat="server" Font-Size="14px" ></asp:TextBox>  
  36.     </td>  
  37.     </tr>  
  38.         <tr>  
  39.             <td>  
  40.                 <asp:Button runat="server" Text="Close Popup" ID="btn" OnClick="btn_Click" />  
  41.                 <script>  
  42.                     $("#btn").click(  
  43.                        function () {  
  44.                   = "WebForm1.aspx";  
  45.                        });  
  46.   </script>  
  47.             </td>  
  48.             <td>  
  49.                 <asp:Button runat="server" ID="savebtn" Text="Save Data" OnClick="savebtn_Click" />  
  50.             </td>  
  51.         </tr>  
  52.     </table>  
  53.     </form>  
  54. </body>  
  55. </html> 
Here I haven't provided you the complete code since that's of no use, you can make as much entries as you want. The complete code can be downloaded by downloading the Zip file provided at the start of this article.
But this code has some of our main function. Have a look at the end of the code. There I used two buttons, one is the close button and the other is the "Save" Button.
In the close button I used the following jQuery code:
  1. <script>  
  2.                     $("#btn").click(  
  3.                        function () {  
  4.                   = "WebForm1.aspx";  
  5.                        });  
  6. </script> 
This code is the main part of our article, this code uses the ID of the Button and on that ID a click function is called. On the click, a function "" will start working and will take you to the Form whose Id you have passed in the " ". This jQuery code will work on the URL of the page and will change the URL by the ID you had currently provided. To make this jQuery code work you need to add a jQuery 1.9.1 file to your application, you can fetch it from the code Zip file provided at the start. You might be thinking, why I didn't use some simple lines of code, like "Response.Redirct" or "Server.Transer"? You will get your answer at the end of this article.
Step 4
After this, I created a connection between the .NET and SQL. I hope you must have known how to create a connection between them, if not then simply go to the Server Explorer of your application and choose "Add New Connection", then provide the Server Name, User Name, and Password. Also, choose the database to which connectivity is to be done.
Now I am working on the click of the save button that I created in the last step.
  1. using System.Web.UI;  
  2. using System.Web.UI.WebControls;  
  3. using System.Data.SqlClient;  
  5. namespace WebApplication26  
  6. {  
  7.     public partial class WebForm2 : System.Web.UI.Page  
  8.     {  
  9.         protected void Page_Load(object sender, EventArgs e)  
  10.         {  
  12.         }  
  14.         SqlConnection x;  
  15.         SqlCommand y;  
  17.         protected void savebtn_Click(object sender, EventArgs e)  
  18.         {  
  19.             x = new SqlConnection(@"Data Source=.;Initial Catalog=test;User ID=sa;Password=password@123");  
  20.             x.Open();  
  21.             y = new SqlCommand("insert into Popup values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "'," + TextBox5.Text + ",'" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "'," + TextBox9.Text + ")", x);  
  22.             y.ExecuteNonQuery();  
  23.             x.Close();  
  24.         }  
  25.     }  

I have provided the code in the simplest way I can, here first the namespace for the SQL is passed in the beginning. Then SqlConnection and SqlCommand are passed as x and y.
On the button click, I have passed the connection string under the "x". Then the connection is opened. By using the Y I passed the Insert command of SQL, here all the TextBox Id's are passed in the ' " " ' except for the Textbox5 and Textbox9, that's because they will insert the Integer value.
Step 5
Now only one thing is remaining and that's connecting the GridView to the database so that the data can be shown at runtime. For that go to the .aspx.cs page of the WebForm1 and write this code on the page Load:
  1. using System.Web.UI.WebControls;  
  2. using System.Data.SqlClient;  
  3. using System.Data;  
  5. namespace WebApplication26  
  6. {  
  7.     public partial class WebForm1 : System.Web.UI.Page  
  8.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.             SqlConnection x = new SqlConnection(@"Data Source=.;Initial Catalog=test;User ID=sa;Password=password@123");  
  13.             x.Open();  
  14.             SqlDataAdapter z1 = new SqlDataAdapter("select * from Popup", x);  
  15.             DataSet ds = new DataSet();  
  16.             z1.Fill(ds, "Popup");  
  17.             GridView1.DataSource = ds.Tables["Popup"];  
  18.             GridView1.DataBind();  
  19.             x.Close();  
  20.         }  
  21.     }  

Here again, I have created a connection between the form and the SQL, but instead of a command, a DataAdapter is used to show the data.
Now our application is created and is ready for execution.
On running the code you might get a Web Page on which only one button will exist but if you enter a value in the database then you will get output like this:
auto refresh grid view1.jpg
Now if you click on the button then you will get an output window like this:
auto refresh grid view2.jpg
Now I will make some entries in this form and click on the "Save Data" button.
auto refresh grid view3.jpg
Since I will click on the "Close Popup" button, the Grid will be refreshed and will show the current entries.
auto refresh grid view4.jpg
Now I will provide you the answer to why we can't use "Response.redirect" or "Server.Transfer" if we have the close button on the Popup WebForm. I am changing the code of the close button to "Response.redirect" let's see what output we get:
auto refresh grid view5.jpg
What will happen is, the WebForm will be opened inside the Popup. That's why we can't use the "Response.redirect" or "Server.Transfer" here. Only = "WebForm1.aspx"; can work here.