databinding
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 PresentationTRANSCRIPT
DatabindingData tastes like chicken, if chicken was data.
DatabindingWhat 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 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
Schema
Students
Professors
Courses
How Do We Get the Data?DataAccess.StudentDao studentDao
= new DataAccess.StudentDao();
List<DataObjects.Student> StudentList =
studentDao.SelectAll().ToList();
How Do We Get the Data?
ASPX page<div id="divStudents" runat="server"></div>
Not much, but at least we have a place to put data
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);}
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); }}
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
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);
How’s it look?Eh…
No Line breaksFirst try didn’t go so well
Literal line = new Literal() { Text = "<br />" };
divStudents.Controls.Add(line);
How’s it look?
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
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>
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();}
How’s it Look?
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)
EvalIn the Code Behind:public string PageData { get; set; }
protected void Page_Load(object sender, EventArgs e){ PageData = "this is a test"; Label1.DataBind();}
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
EvalIn the ASPX file:<asp:Label ID="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.