270 likes | 430 Views
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)
E N D
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) • 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
How Do We Get the Data? DataAccess.StudentDaostudentDao = newDataAccess.StudentDao(); List<DataObjects.Student> StudentList = studentDao.SelectAll().ToList();
ASPX page <divid="divStudents"runat="server"> </div> • Not much, but at least we have a place to put data
Code behind file protectedvoidPage_Load(object sender, EventArgs e) { DataAccess.StudentDaostudentDao = newDataAccess.StudentDao(); List<DataObjects.Student> StudentList = studentDao.SelectAll().ToList(); BindData_TheHardWay(StudentList); }
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); } }
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
Code Behind File LabellblId = newLabel(); lblId.Text = "Id"; divStudents.Controls.Add(lblId); TextBoxtxtId = newTextBox(); txtId.Text = student.StudentId.ToString(); divStudents.Controls.Add(txtId);
How’s it look? • Eh…
No Line breaks • First try didn’t go so well Literal line = newLiteral() { Text = "<br />" }; divStudents.Controls.Add(line);
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
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>
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(); }
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)
Eval • In the Code Behind: publicstringPageData { get; set; } protectedvoidPage_Load(object sender, EventArgs e) { PageData = "this is a test"; Label1.DataBind(); }
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
Eval • In the ASPX file: <asp:LabelID="Label1"runat="server" Text='<%#DataBinder.Eval(Page,"PageData") %>'> </asp:Label>
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.