c_sharp_exp_12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment 12: Develop an ASP.

NET Application to
implement data binding in repeater control.
datagrid.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridform.aspx.cs"
Inherits="repeaterORIG.gridform" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h2>Data Binding in Data Grid</h2>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="number" HeaderText="Number" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>

datagrid.aspx.cs
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;

namespace repeaterORIG
{
public partial class gridform : System.Web.UI.Page
{
string cs = @"Data Source=LAPTOP-V00V4FNF\SQLEXPRESS;Initial
Catalog=players;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
using(SqlConnection s = new SqlConnection(cs))
{
s.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from
cricketers", s);
DataTable dt = new DataTable();
sqlda.Fill(dt);
gv.DataSource = dt;
gv.DataBind();
}
}
}
}

repeater.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="bd_rc.aspx.cs"
Inherits="repeaterORIG.bd_rc" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<h2>Data Binding in Repeaters</h2>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>
Name
</th>
<th>
Id
</th>
</tr>

</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server"
Text='<%#Eval("name") %>'></asp:Label>

</td>
<td>
<asp:Label ID="Label2" runat="server"
Text='<%#Eval("number") %>'></asp:Label>
</td>
<tr/>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

</center>
</div>
</form>
</body>
</html>
repeater.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace repeaterORIG
{
public partial class bd_rc : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string mainconn =
ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
SqlConnection conn = new SqlConnection(mainconn);
string sqlquery = "select * from cricketers";
SqlCommand sqlCommand = new SqlCommand(sqlquery, conn);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
DataTable dt = new DataTable();
adapter.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
conn.Close();
}
}
}

Output:

You might also like