Introduction
In this article we will learn about the GridView control and how to display data into a jQuery and AJAX model pop-up window. We can also learn how to edit and update the record from the model pop-up window and update it in the database from the GridView at runtime. In this article we can see these things in two different ways in two different examples.
- Display selected records using jQuery
- Display selected records using AJAX
Display selected records using jQuery
Use the following procedure to create a sample of creating a jQuery model pop-up window in a GridView event.
Step 1
Open the Visual Studio and select the project.
Step 2
Choose an empty template.
Step 3
Add New Item and choose a Web Form and give a nice name.
Step 4
Add jQuery to your project. Right-click on the project then choose Manage NuGet Packeges and download jQuery, the latest version.
Step 5
Add a GridView to the page and bind with some dummy data.
For the use of a DataTable we should add a using statement for the "System.Data" namespace.
Step 6
To display the data into a dialog box we should to use a jQueryUI dialog model as in the following:
Step 7
Now for some jQuery code for the click event. In GridView there is a button with an ID, for that ID we call the model pop-up a dialog box.
- $(document).on("click", "[id*=btn]", function () {
- $("#id").html($(".Id", $(this).closest("tr")).html());
- $("#name").html($(".Name", $(this).closest("tr")).html());
- $("#add").html($(".add", $(this).closest("tr")).html());
- $("#dialog").dialog({
- title: "View Details",
- buttons: {
- Ok: function () {
- $(this).dialog('close');
- }
- },
- modal: true
- });
- return false;
- });
Output :
When we click on the view button the jQuery dialog box will appear with all the row information.
Code:
- <html>
- <head runat="server">
- <title>Untitled Page</title>
- <script src="Scripts/jquery-2.1.1.min.js"></script>
- <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
- <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
- rel="stylesheet" type="text/css" />
- <script type="text/javascript">
- $(document).on("click", "[id*=btn]", function () {
- $("#id").html($(".Id", $(this).closest("tr")).html());
- $("#name").html($(".Name", $(this).closest("tr")).html());
- $("#add").html($(".add", $(this).closest("tr")).html());
- $("#dialog").dialog({
- title: "View Details",
- buttons: {
- Ok: function () {
- $(this).dialog('close');
- }
- },
- modal: true
- });
- return false;
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:GridView ID="GridView1" HeaderStyle-BackColor="Blue" HeaderStyle-ForeColor="White"
- runat="server" AutoGenerateColumns="false">
- <Columns>
- <asp:BoundField DataField="Id" ItemStyle-CssClass="Id" HeaderText="Id" ItemStyle-Width="30" />
- <asp:BoundField DataField="Name" ItemStyle-CssClass="Name" HeaderText="Name" ItemStyle-Width="150" />
- <asp:BoundField DataField="Address" ItemStyle-CssClass="add" HeaderText="Address"
- ItemStyle-Width="150" />
- <asp:TemplateField>
- <ItemTemplate>
- <asp:Button Text="View" ID="btn" runat="server" BackColor="AliceBlue" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- <div id="dialog" style="display: none">
- <b>Id:</b> <span id="id"></span>
- <br />
- <b>Name:</b> <span id="name"></span>
- <br />
- <b>Address:</b> <span id="add"></span>
- </div>
-
- </form>
- </body>
- </html>
Display selected records using AJAX
Display an entire row on a button click using the AJAX toolkit. To display the data in a model pop-up window we need to use the following basic procedure.
Step 1
In this step we need to follow all the previous 5 steps of adding a new project, adding new items and adding the web form. In addition to that we need to install the AJAX library in the project to use the AJAX controls in the web form.
Step 2
Download the Ajax Control Toolkit. link
Step 3
After the downloading of the Ajax toolkit, extract all the files.
Step 4
Add the DLL file in the Visual Studio. After adding you get AjaxExtensions.
Step 5
Add a GridView to the web form.
Step 6
Add a CSS file to your project. The CSS file is used for the pop-up window.
Step 7
Add ToolScriptManager into the web form and activate the SelectedIndexChanged Event for the GridView.
Step 8
Add the ModelpopupExtender to the web form. ModelpopupExtender does the pop-up for any panel window.
Step 9
Add a panel that is treated like a pop-up window.
Step 10
Now bind a DataTable having some dummy data into the grid view.
- if (!this.IsPostBack)
- {
- DataTable dt = new DataTable();
- dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Address") });
- dt.Rows.Add(1, "Rajeev", "Hazaribagh");
- dt.Rows.Add(2, "Ranjan", "New Delhi");
- dt.Rows.Add(3, "Prerana Tiawri", "Noida");
- dt.Rows.Add(4, "Kumar Rohit", "Pune");
- GridView1.DataSource = dt;
- GridView1.DataBind();
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
Step 11
Open the model pop-up inside the onSelectedIndexChanged event.
- lblId.Text = GridView1.SelectedRow.Cells[0].Text;
- lblName.Text = GridView1.SelectedRow.Cells[1].Text;
- lblCountry.Text = (GridView1.SelectedRow.FindControl("lblAdd") as Label).Text;
- mp.Show();
Output:
After clicking the view button we will get the selected row data inside the pop-up window.
Summary
In this article we will see how to pop-up the selected row data using Ajax or jQuery, we will see by both. Thanks for reading this article.