Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Data tastes like chicken, if chicken was data.

2 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

3 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

4 Schema

5 Students

6 Professors

7 Courses

8 How Do We Get the Data? DataAccess.StudentDao studentDao = new DataAccess.StudentDao(); List StudentList = studentDao.SelectAll().ToList();

9 How Do We Get the Data?

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

11 Code behind file protected void Page_Load(object sender, EventArgs e) { DataAccess.StudentDao studentDao = new DataAccess.StudentDao(); List StudentList = studentDao.SelectAll().ToList(); BindData_TheHardWay(StudentList); }

12 Code Behind File private void BindData_TheHardWay(List 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); }

13 Create Code Behind File For every student For every field you want to show Create new control for each property Set the text to the value you want to show Add that control to the page

14 Code Behind File Label lblId = new Label(); lblId.Text = "Id"; divStudents.Controls.Add(lblId); TextBox txtId = new TextBox(); txtId.Text = student.StudentId.ToString(); divStudents.Controls.Add(txtId);

15 How’s it look? Eh…

16 No Line breaks First try didn’t go so well Literal line = new Literal() { Text = " " }; divStudents.Controls.Add(line);

17 How’s it look?

18 Data Binding Previous example gives you complete control over the controls on the page Plenty of room for error Time consuming Let’s try DataBinding to a Gridview

19 ASPX File

20 Code Behind File protected void Page_Load(object sender, EventArgs e) { DataAccess.StudentDao studentDao = new DataAccess.StudentDao(); List StudentList = studentDao.SelectAll().ToList(); Databind_TheEasyWay(StudentList); } private void Databind_TheEasyWay(List StudentList) { GridView1.DataSource = StudentList; GridView1.DataBind(); }

21 How’s it Look?

22 Eval Eval 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)

23 Eval In the Code Behind: public string PageData { get; set; } protected void Page_Load(object sender, EventArgs e) { PageData = "this is a test"; Label1.DataBind(); }

24 Eval The page has a public property that we fill with data Labels aren’t automatically databound elements, so we have to call DataBind() Controls like DataList, GridView, Repeater call this method automatically

25 Eval In the ASPX file: <asp:Label ID="Label1" runat="server" Text=' '>

26 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.


Download ppt "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."

Similar presentations


Ads by Google