Wednesday, 13 June 2012

ASP.NET & C# binding data to GridView databound control



The GridView control is the workhorse of ASP.NET Framework. It is one of the most feature-rich and complicated of all the ASP.NET controls. The GridView control enables you to display, select, sort, page, and edit data items such as database records.
The GridView control supersedes the DataGrid control included in the ASP.NET 1.x Framework. TheDataGrid control is still included in ASP.NET 4 for backward compatibility, but you should use the GridView instead because it is a more powerful control.
The GridView control includes a rich set of formatting properties that you can use to modify its appearance. I recommend that you don’t use most of these properties because using these properties results in bloated pages. Instead, I recommend that you use Cascading Style Sheets (CSS) to format the GridView control.
The GridView control includes a CssClass property. The control also exposes several Style objects that include the CssClass property:
  • AlternatingRowStyle Enables you to format every other row.
  • FooterStyle Enables you to format the footer row.
  • HeaderStyle Enables you to format the header row.
  • PagerStyle Enables you to format the pager row.
  • RowStyle Enables you to format each row.
  • SelectedRowStyle Enables you to format the selected row.





create a webpage with textbox with the caption match depending upon the value stored in the database the textbox filters your data stored in the database movies and display the data using GridView databound control.

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="textbox1" runat="server"/>
   
        <br />
    <asp:Button ID="Submitbtn" Text="Submit" OnClick="fetch" runat="server" />
    <hr />
     <asp:Label ID="lblmovies" runat="server"></asp:Label>
     <br />
    <asp:GridView ID="gridmovies" DataSourceID="srcmovies" runat="server"></asp:GridView>
  
    <asp:SqlDataSource
    ID="srcmovies"
    ConnectionString="Server=.\SQLExpress;
    AttachDbFilename=|DataDirectory|moviesdb.mdf;
    Integrated Security=True;User Instance=True"
    SelectCommand="SELECT id,movies,LeadActor FROM movies WHERE id=@Id" runat="server">
     <SelectParameters>
    <asp:ControlParameter Name="Id" Type="Int32" ControlID="textbox1"/>
    </SelectParameters>
    </asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>


Defualt.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void fetch(object sender, EventArgs e)
    {
        lblmovies.Text = textbox1.Text;
    }
}





1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete