databinding

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

Upload: isha

Post on 27-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Databinding. 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 to controls by hand. We Need Some Data. Student Database Schema ( visio document) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Databinding

DatabindingData tastes like chicken, if chicken was data.

Page 2: Databinding

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: Databinding

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: Databinding

Schema

Page 5: Databinding

Students

Page 6: Databinding

Professors

Page 7: Databinding

Courses

Page 8: Databinding

How Do We Get the Data?DataAccess.StudentDao studentDao

= new DataAccess.StudentDao();

List<DataObjects.Student> StudentList =

studentDao.SelectAll().ToList();

Page 9: Databinding

How Do We Get the Data?

Page 10: Databinding

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

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

Page 11: Databinding

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: Databinding

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: Databinding

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: Databinding

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: Databinding

How’s it look?Eh…

Page 16: Databinding

No Line breaksFirst try didn’t go so well

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

divStudents.Controls.Add(line);

Page 17: Databinding

How’s it look?

Page 18: Databinding

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: Databinding

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: Databinding

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: Databinding

How’s it Look?

Page 22: Databinding

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: Databinding

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: Databinding

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: Databinding

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

Page 26: Databinding

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.