Here I will explain how to display GridView selected row cell values in TextBoxes outside the GridView using jQuery.
- Go to Start, All Programs and open Microsoft Visual Studio 2013.
- Now, click on File, then select New Project and click on Visual C#. Then select ASP.NET Web Application, Empty and press OK.
- Provide the web application name and location as you wish. I named my web application DisplaySelectedGridviewRowValuesInTextboxes.
- Now the project will open. Right-click on the web application name, add a New Item and select Web Form, then click on Add.
- Add the following code between the <form> tags of the WebForm1.aspx page:
- <div align="center">
- <table border="1">
- <tr>
- <td>EmpId</td>
- <td>
- <asp:TextBox ID="txtEmpId" runat="server" />
- </td>
- </tr>
- <tr>
- <td>EmpName</td>
- <td>
- <asp:TextBox ID="txtEmpName" runat="server" />
- </td>
- </tr>
- <tr>
- <td>EmpSalary</td>
- <td>
- <asp:TextBox ID="txtEmpSalary" runat="server" />
- </td>
- </tr>
- <tr>
- <td>Department</td>
- <td>
- <asp:TextBox ID="txtDept" runat="server" />
- </td>
- </tr>
- </table>
- <br />
- <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" runat="server" AutoGenerateColumns="false">
- <Columns>
- <asp:BoundField DataField="EmpId" ItemStyle-CssClass="EmpId" HeaderText="EmpId"/>
- <asp:BoundField DataField="EmpName" ItemStyle-CssClass="EmpName" HeaderText="EmpName" />
- <asp:BoundField DataField="EmpSalary" ItemStyle-CssClass="EmpSalary" HeaderText="EmpSalary" />
- <asp:BoundField DataField="Dept" ItemStyle-CssClass="Dept" HeaderText="Department" />
- <asp:TemplateField>
- <ItemTemplate>
- <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- </div>
- Add the following code to the page load event of the WebForm1.aspx.cs page.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("EmpId");
- dt.Columns.Add("EmpName");
- dt.Columns.Add("EmpSalary");
- dt.Columns.Add("Dept");
- dt.Rows.Add(1, "JACKSON", 75000, "RESEARCH");
- dt.Rows.Add(2, "JOHNSON", 18000, "ACCOUNTING");
- dt.Rows.Add(3, "GRANT", 32000, "SALES");
- dt.Rows.Add(4, "ADAMS", 34000, "OPERATIONS");
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- }
- Add a <script> tag between the <head> tags of the WebForm1.aspx page. You need to provide a reference for jQuery as in the following code snippet.
- <head runat="server">
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
- </head>
- Add another <script> tag between the <head> tags and write the following code:
- <script type="text/javascript">
- $("[id*=lnkSelect]").live("click", function () {
- $("[id*=txtEmpId]").val($(".EmpId", $(this).closest("tr")).html());
- $("[id*=txtEmpName]").val($(".EmpName", $(this).closest("tr")).html());
- $("[id*=txtEmpSalary]").val($(".EmpSalary", $(this).closest("tr")).html());
- $("[id*=txtDept]").val($(".Dept", $(this).closest("tr")).html());
- return false;
- });
- </script>
- Now run the application and you can see it in your browser.
- Now I am selecting EmpId 3, see the Empid 3 row cell values will be populated in the corresponding textboxes.