Sep
18
2006
Codebehind databinding - howto part 1
Posted by admin under
In practice
While I certainly have my opinion on designtime databinding (read more in this blog post) I do understand that the "easy" way of databinding ASP.NET offers sure is convinient. And - since I talk alot about doing things the other way, I felt I also have to back it up with an example.
This example will be really easy. One form containing a gridview - listing all customers in a database.

The other form is reached from the Edit or New customer links:

So - lets start with the database. I will be using SQL Server and one single table - customer:

We will access the database only through stored procedures so here's a list of them:
create proc cust_ListCustomers
as
select * from cust_customer
CREATE proc cust_OpenCust(@id int)
as
select * from cust_customer where id=@id
CREATE PROCEDURE cust_SaveCustomer(@id int, @name varchar(50)) AS
if @id=-1
BEGIN
insert into cust_Customer(custname) select @name
select @@identity
END
ELSE
BEGIN
update cust_Customer set custname=@name where id=@id
select @id
END
GO
The cust_SaveCustomer stored procedure is described in detail in this article.
So lets start with the grid code:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" >
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Customer">
<ItemTemplate>
<asp:Label id="lblCustName" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:HyperLink id="hlEdit" runat="server" Text="Edit"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The important things to notice are AutoGenerateColumns="false" and OnRowDataBound="GridView1_RowDataBound"
We will describe our columns ourselves - and as you can see I am using <asp:TemplateField>:s. I prefer using templates as much as I can, while it in these examples might look at little overkill since we are just using a label and a hyperlink in each of the columns, but overall you gain a lot more control in more complex scenarios - and I prefer trying doing things the same way all the time.
Next: look at the code, Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["MainConn"].ToString(),
CommandType.StoredProcedure, "cust_ListCustomers").Tables[0];
GridView1.DataBind();
}
}
We are using sqlhelper.cs from Microsoft Application Blocks (v1) and calling the stored procedure cust_ListCustomers. We receive a dataset and the first datatable from it we feed as datasource to the grid.
Now, the GridView1_RowDataBound code. This function is called once for each datarow (actually DataRowView:s, since behind the scenes it's the DefaultView of the datatable being bound to the grid).
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = e.Row.DataItem as DataRowView;
Label lblCustName = e.Row.FindControl("lblCustName") as Label;
if (lblCustName != null)
lblCustName.Text = row["custname"].ToString();
HyperLink hlEdit = e.Row.FindControl("hlEdit") as HyperLink;
if (hlEdit != null)
hlEdit.NavigateUrl = "editcust.aspx?id=" + row["id"].ToString();
}
}
By using FindControl for the e.Row object we can retrieve the controls from the template and when we have them we can simply set the any properties we'd like to.
This article will be continued - we will do the edit/new as well, for now please download the attached file - it does contain that functionality as well.