programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Website by URL, How to Play YouTube Videos in by using Jquery , Jquery Change div background on mouseover in , JQuery UI Datepicker (Calendar) etc.

Tuesday, August 19, 2014

How to bind data to textbox inside Gridview in using C#

Textbox inside Gridview in ASP.NET:

In this post we describe that how to get textbox control into gridview control and then bind this textbox. As we know that for solving this we use templatefield in gridview. In this tutorial we also give Example that refers how to get (make) templatefiled in gridview. And also consider many examples related to gridview contro. Some important gridview and textbox related post listed here:

Gridview related posts :

How to bind data to Textbox:

For binding textbox in gridview we take a sql table with data. On the C# code page create connection to the sql for get table data.  The name of table is list of state which contain name of state and one Id.

Then get the data and bind textbox.

Textbox binding:

<asp:TextBox ID="label1" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>

Here ID is the sql table column name.

Textbox with gridview Example:

Code of Exampleoftextboxbind.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile=" Exampleoftextboxbind.aspx.cs" Inherits="Exampleoftemplate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Bind data to Textbox inside Gridview in ASP.NET</title>
    <form id="form1" runat="server">
    <div style="" align="center">
        <br />       
            <h3>How to bind data to textbox inside Gridview in ASP.NET: </h3>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false"
            Height="258px" Width="559px" >
         <asp:TemplateField HeaderText="SNo" ItemStyle-HorizontalAlign="Center">
                                <%#Container.DataItemIndex+1 %>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
         <asp:TemplateField HeaderText="State ID" ItemStyle-HorizontalAlign="Center">
                                <asp:TextBox ID="label1" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
         <asp:TemplateField HeaderText="Name of State" ItemStyle-HorizontalAlign="Center">
                                <asp:TextBox ID="label2" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>

            <HeaderStyle BackColor="#990000" BorderColor="#CC33FF" BorderStyle="Dotted"
                BorderWidth="2px" ForeColor="#009933" />

Exampleoftextboxbind.aspx.cs page code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Exampleoftextboxbind : System.Web.UI.Page
    string connection;
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            connection = @"Data Source=PARIJAT-PC\PARIJAT;Initial Catalog=Database1;Integrated Security=True;Pooling=False";// create connection....
            BindData();//  call function here........

    private void BindData() //make function .....

            string sql = "select StateID as ID,StateName as Name from States";//sql Query for fetching data from table
            SqlConnection conn = new SqlConnection(connection);
            SqlCommand command = new SqlCommand(sql, conn);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable datatable = new DataTable();

            grd.DataSource = datatable;  //bind grid view control here

        catch(Exception ex)

Simple copy code and past on page and run

Other related Post:

No comments:

Post a Comment