1 / 26

Databinding

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)

isha
Download Presentation

Databinding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Databinding 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.StudentDaostudentDao = newDataAccess.StudentDao(); List<DataObjects.Student> StudentList = studentDao.SelectAll().ToList();

  9. How Do We Get the Data?

  10. ASPX page <divid="divStudents"runat="server"> </div> • Not much, but at least we have a place to put data

  11. Code behind file protectedvoidPage_Load(object sender, EventArgs e) { DataAccess.StudentDaostudentDao = newDataAccess.StudentDao(); List<DataObjects.Student> StudentList = studentDao.SelectAll().ToList(); BindData_TheHardWay(StudentList); }

  12. Code Behind File privatevoidBindData_TheHardWay(List<DataObjects.Student> StudentList) { foreach (DataObjects.Student student inStudentList) { LabellblId = newLabel(); lblId.Text = "Id"; divStudents.Controls.Add(lblId); TextBoxtxtId = newTextBox(); txtId.Text = student.StudentId.ToString(); divStudents.Controls.Add(txtId); LabellblFirstName = newLabel(); lblFirstName.Text = "First Name"; divStudents.Controls.Add(lblFirstName); TextBoxtxtFirstName = newTextBox(); txtFirstName.Text = student.FirstName; divStudents.Controls.Add(txtFirstName); LabellblLastName = newLabel(); lblLastName.Text = "Last Name"; divStudents.Controls.Add(lblLastName); TextBoxtxtLastName = newTextBox(); 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 LabellblId = newLabel(); lblId.Text = "Id"; divStudents.Controls.Add(lblId); TextBoxtxtId = newTextBox(); 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 = newLiteral() { Text = "<br />" }; 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 <divid="divStudents"runat="server"> <asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="false"> <Columns> <asp:BoundFieldDataField="StudentId"HeaderText="StudentId"/> <asp:BoundFieldDataField="FirstName"HeaderText="FirstName"/> <asp:BoundFieldDataField="LastName"HeaderText="LastName"/> </Columns> </asp:GridView> </div>

  20. Code Behind File protectedvoidPage_Load(object sender, EventArgs e) { DataAccess.StudentDaostudentDao = newDataAccess.StudentDao(); List<DataObjects.Student> StudentList = studentDao.SelectAll().ToList(); Databind_TheEasyWay(StudentList); } privatevoidDatabind_TheEasyWay(List<DataObjects.Student> 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: publicstringPageData { get; set; } protectedvoidPage_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:LabelID="Label1"runat="server" Text='<%#DataBinder.Eval(Page,"PageData") %>'> </asp:Label>

  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.

More Related