Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data...

26
Databinding Data tastes like chicken, if chicken was data.

description

We Need Some Data Student Database Schema (visio document) The focus here is NOT ON THE DATABASE If you want to learn the details of how a database works and how to use one, come see our database seminar

Transcript of Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data...

Page 1: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

DatabindingData tastes like chicken, if chicken was data.

Page 2: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

DatabindingWhat is it?

Associating a set of data with a control

Why use it?Its much easier than associating data to

controls by hand

Page 3: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

We Need Some DataStudent Database

Schema (visio document)The focus here is NOT ON THE DATABASE

If you want to learn the details of how a database works and how to use one, come see our database seminar

Page 4: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Schema

Page 5: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Students

Page 6: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Professors

Page 7: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Courses

Page 8: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

How Do We Get the Data?DataAccess.StudentDao studentDao

= new DataAccess.StudentDao();

List<DataObjects.Student> StudentList =

studentDao.SelectAll().ToList();

Page 9: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

How Do We Get the Data?

Page 10: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

ASPX page<div id="divStudents" runat="server"></div>

Not much, but at least we have a place to put data

Page 11: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Code behind fileprotected void Page_Load(object sender, EventArgs e){ DataAccess.StudentDao studentDao

= new DataAccess.StudentDao(); List<DataObjects.Student> StudentList

= studentDao.SelectAll().ToList();

BindData_TheHardWay(StudentList);}

Page 12: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Code Behind Fileprivate void BindData_TheHardWay(List<DataObjects.Student> StudentList){ foreach (DataObjects.Student student in StudentList) { Label lblId = new Label(); lblId.Text = "Id"; divStudents.Controls.Add(lblId);

TextBox txtId = new TextBox(); txtId.Text = student.StudentId.ToString(); divStudents.Controls.Add(txtId); Label lblFirstName = new Label(); lblFirstName.Text = "First Name"; divStudents.Controls.Add(lblFirstName);

TextBox txtFirstName = new TextBox(); txtFirstName.Text = student.FirstName; divStudents.Controls.Add(txtFirstName);

Label lblLastName = new Label(); lblLastName.Text = "Last Name"; divStudents.Controls.Add(lblLastName);

TextBox txtLastName = new TextBox(); txtLastName.Text = student.LastName; divStudents.Controls.Add(txtLastName); }}

Page 13: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Create Code Behind FileFor every student

For every field you want to show

Create new control for each propertySet the text to the value you want to showAdd that control to the page

Page 14: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Code Behind FileLabel lblId = new Label();lblId.Text = "Id";divStudents.Controls.Add(lblId);

TextBox txtId = new TextBox();txtId.Text = student.StudentId.ToString();divStudents.Controls.Add(txtId);

Page 15: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

How’s it look?Eh…

Page 16: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

No Line breaksFirst try didn’t go so well

Literal line = new Literal() { Text = "<br />" };

divStudents.Controls.Add(line);

Page 17: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

How’s it look?

Page 18: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Data BindingPrevious example gives you complete control

over the controls on the pagePlenty of room for errorTime consuming

Let’s try DataBinding to a Gridview

Page 19: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

ASPX File<div id="divStudents" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="StudentId"

HeaderText="StudentId" /> <asp:BoundField DataField="FirstName"

HeaderText="FirstName" /> <asp:BoundField DataField="LastName"

HeaderText="LastName" /> </Columns> </asp:GridView></div>

Page 20: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Code Behind Fileprotected void Page_Load(object sender, EventArgs e){ DataAccess.StudentDao studentDao

= new DataAccess.StudentDao(); List<DataObjects.Student> StudentList

= studentDao.SelectAll().ToList();

Databind_TheEasyWay(StudentList);}

private void Databind_TheEasyWay(List<DataObjects.Student> StudentList){ GridView1.DataSource = StudentList; GridView1.DataBind();}

Page 21: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

How’s it Look?

Page 22: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

EvalEval is used to bind to an UI item that is

setup to be read-only It is used for late-bound data (not known

from start)

Page 23: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

EvalIn the Code Behind:public string PageData { get; set; }

protected void Page_Load(object sender, EventArgs e){ PageData = "this is a test"; Label1.DataBind();}

Page 24: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

EvalThe page has a public property that we fill

with dataLabels aren’t automatically databound

elements, so we have to call DataBind()Controls like DataList, GridView, Repeater call

this method automatically

Page 25: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

EvalIn the ASPX file:<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Page,"PageData") %>'> </asp:Label>

Page 26: Data tastes like chicken, if chicken was data.. Databinding What is it? Associating a set of data with a control Why use it? Its much easier than associating.

Eval(From MSDN): Because this method performs

late-bound evaluation, using reflection at run time, it can cause performance noticeably slow compared to standard ASP.NET data-binding syntax.