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

Tuesday, August 19, 2014

Bind Data to asp.net textbox control in inside of gridview Using C# Example

Bind Data to asp.net textbox control in inside of gridview:



In this post we describe how to bind textbox control which is use to inside the grid.In the past post we learn how to bind dropdownlist and how to use dropdownlist with in grid. And also give a example of tooltip on control. In this tutorial blog we give many Examples rerated to grid view these are :

Gridview Related Post:

Example of C# for Bind Data


Now we consider one more gridview example in which we consider textbox with in grid and bind it by sql data table.

Bind Data to asp.net textbox:

Consider a table which has two columns StateID and StateName. Make sql connection and fetch data form sql server.

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 align="center">
        <br />       
            <h3>Bind Data to asp.net textbox control in inside of gridview Example </h3>
        <asp:GridView ID="GridView1" 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="lblStateID" 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="lblName" runat="server" Text='<%# Eval("StateName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        </asp:TemplateField>
                      

        </Columns>
            <HeaderStyle BackColor="#3333FF" BorderColor="#FF99FF" BorderStyle="Groove"
                BorderWidth="1px" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

C# Code for textbox control binding 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);

            GridView1.DataSource = datatable;  //bind grid .........here                              GridView1.DataBind();

        }
        catch
        {
            throw;
        }
    }
   
}

Other Asp.net Related Post:


No comments:

Post a Comment