Example of C# for Bind Data to asp.net Textbox inside gridview control:

C# code for Sql Data binding  to asp.net text box inside Gridview:



In this post we give an example for taking control with in control. And then bind second control by Sql data base. For this take a gridview and a textbox control in this grid template. In This asp.netprogramming tutorial we give many other grid view related posts, here we listed some important post of related to gridview :-

Gridview Related Post:

Asp.net textbox inside gridview:

Now today we give one more Example related to textbox control and gridview control in asp.net. Fetch the Data From Sql database and bind textbox.

.Aspx page Code for textbox binding Example:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>textbox control in inside of gridview</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="">
        <br />       
            <h3>C# code for Sql Data binding to asp.net textbox inside gridview: </h3>
        <asp:GridView ID="grd" runat="server" AutoGenerateColumns="false"
            Height="258px" Width="559px" >
        <Columns>
         <asp:TemplateField HeaderText="SNo" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <%#Container.DataItemIndex+1 %>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="State ID" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label ID="labelStateID" runat="server" Text='<%# Eval("StateID") %>'></asp:Label>
                               
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
         <asp:TemplateField HeaderText="Name of State" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                               
                                <asp:Label ID="labelName" runat="server" Text='<%# Eval("StateName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                      

        </Columns>
            <HeaderStyle BackColor="#990000" BorderColor="#CC33FF" BorderStyle="Dotted"
                BorderWidth="2px" ForeColor="#009933" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
bind textbox inside of gridview in asp.net
Bind textbox inside of gridview in asp.net

.Aspx.cs page C# code for textbox control Example:

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 Exampleoftemplate : System.Web.UI.Page
{
    string connectionstring;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            connectionstring = @"Data Source=PARIJAT-PC\PARIJAT;Initial Catalog=Database1;Integrated Security=True;Pooling=False";// create connection....
            BindData();// make function .......and call here........
        }
    }
    private void BindData()
    {
        try
        {
            string sql = "select StateID,StateName from States";//sql Query
            SqlConnection connection = new SqlConnection(connectionstring);
            SqlCommand command = new SqlCommand(sql, connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataTable datatable = new DataTable();
            adapter.Fill(datatable);
            grd.DataSource = datatable;  //bind grid .........here .....................
            grd.DataBind();
        }
        catch
        {
            throw;
        }
    }   
}



Other asp.net programming related Post:


Comments

Post a Comment

Popular posts from this blog