260 likes | 382 Views
Using XML with ASP and the ASP:Repeater. Please use speaker notes for additional information!. <EMPLOYEE> <FIRST_NAME>Carl</FIRST_NAME> <LAST_NAME>Hersey</LAST_NAME> <DEPT>Foreign</DEPT> <JOB>Consultant</JOB> <SALARY>60000</SALARY> </EMPLOYEE>
E N D
Using XML with ASPand the ASP:Repeater Please use speaker notes for additional information!
<EMPLOYEE> <FIRST_NAME>Carl</FIRST_NAME> <LAST_NAME>Hersey</LAST_NAME> <DEPT>Foreign</DEPT> <JOB>Consultant</JOB> <SALARY>60000</SALARY> </EMPLOYEE> </PERSONNEL> <?xml version="1.0" ?> <!-- Data for Payroll --> <PERSONNEL> <EMPLOYEE> <FIRST_NAME>Stephen</FIRST_NAME> <LAST_NAME>Daniels</LAST_NAME> <DEPT>Sports</DEPT> <JOB>Reporter</JOB> <SALARY>50000</SALARY> </EMPLOYEE> <EMPLOYEE> <FIRST_NAME>Al</FIRST_NAME> <LAST_NAME>Richards</LAST_NAME> <DEPT>Engineering</DEPT> <JOB>Consultant</JOB> <SALARY>95000</SALARY> </EMPLOYEE> <EMPLOYEE> <FIRST_NAME>Jennifer</FIRST_NAME> <LAST_NAME>Ames</LAST_NAME> <DEPT>Foreign</DEPT> <JOB>Consultant</JOB> <SALARY>55000</SALARY> </EMPLOYEE> <EMPLOYEE> <FIRST_NAME>Sarah</FIRST_NAME> <LAST_NAME>Grant</LAST_NAME> <DEPT>Scientific</DEPT> <JOB>Web</JOB> <SALARY>50000</SALARY> </EMPLOYEE> Continued...
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim payrollData=New DataSet payrollData.ReadXml(MapPath("payroll.xml")) payrollSet.DataSource=payrollData payrollSet.DataBind() end if end sub </script> <html> This is imported to allow us to work with DataSet objects. This creates a DataSet that I named payrollData for the XML file. When the page is loaded, the XML file is loaded into the DataSet to make it available This establishes the DataSource and binds the DataSet to the Repeater control that you will see on the next page. <asp:Repeater id="payrollSet" runat="server"> Code continued on the next page...
<body> <form runat="server"> <h2>Employee List</h2> <asp:Repeater id="payrollSet" runat="server"> <HeaderTemplate> <table border="2" width="75%"> <tr> <th>Last Name</th> <th>First Name</th> <th>Dept</th> <th>Job</th> <th>Salary</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("LAST_NAME")%></td> <td><%#Container.DataItem("FIRST_NAME")%></td> <td><%#Container.DataItem("DEPT")%></td> <td><%#Container.DataItem("JOB")%></td> <td><%#Container.DataItem("SALARY")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html> The asp:Repeater is used with payrollSet (refer to previous slide). The <HeaderTemplete> is done only once and it is done first. The contents of the <ItemTemplate> are repeated. As you can see looking at the output, each element in the XML is repeated. DataItems under Employee in the XML are repeated. Like <HeaderTemplate>, the <FooterTemplate> is rendered once - the table is ended. Finally, the closing of asp:Repeater, and the form.
<SeparatorTemplate> <tr> <td colspan=5><img src="column02.gif"></td> </tr> </SeparatorTemplate>
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim payrollData=New DataSet payrollData.ReadXml(MapPath("payroll.xml")) payrollSet.DataSource=payrollData payrollSet.DataBind() end if end sub </script> <html> <body> <form runat="server"> <h2>Employee List</h2> <asp:Repeater id="payrollSet" runat="server"> <HeaderTemplate> <table border="2" width="60%"> <tr> <th>Last Name</th> <th>First Name</th> <th>Dept</th> <th>Job</th> <th>Salary</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("LAST_NAME")%></td> <td><%#Container.DataItem("FIRST_NAME")%></td> <td><%#Container.DataItem("DEPT")%></td> <td><%#Container.DataItem("JOB")%></td> <td><%#Container.DataItem("SALARY")%></td> </tr> </ItemTemplate> <SeparatorTemplate> <tr> <td colspan=5><img src="column02.gif"></td> </tr> </SeparatorTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html> Continue...
<AlternatingItemTemplate> <tr bgcolor=red> <td><b><%#Container.DataItem("LAST_NAME")%></b></td> <td><b><%#Container.DataItem("FIRST_NAME")%></b></td> <td><b><%#Container.DataItem("DEPT")%></b></td> <td><b><%#Container.DataItem("JOB")%></b></td> <td><b><%#Container.DataItem("SALARY")%></b></td> </tr> </AlternatingItemTemplate>
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim payrollData=New DataSet payrollData.ReadXml(MapPath("payroll.xml")) payrollSet.DataSource=payrollData payrollSet.DataBind() end if end sub </script> <html> <body> <form runat="server"> <h2>Employee List</h2> <asp:Repeater id="payrollSet" runat="server"> <HeaderTemplate> <table border="2" width="60%"> <tr> <th>Last Name</th> <th>First Name</th> <th>Dept</th> <th>Job</th> <th>Salary</th> </tr> </HeaderTemplate>
<ItemTemplate> <tr bgcolor=blue> <td><b><%#Container.DataItem("LAST_NAME")%></b></td> <td><b><%#Container.DataItem("FIRST_NAME")%></b></td> <td><b><%#Container.DataItem("DEPT")%></b></td> <td><b><%#Container.DataItem("JOB")%></b></td> <td><b><%#Container.DataItem("SALARY")%></b></td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr bgcolor=red> <td><b><%#Container.DataItem("LAST_NAME")%></b></td> <td><b><%#Container.DataItem("FIRST_NAME")%></b></td> <td><b><%#Container.DataItem("DEPT")%></b></td> <td><b><%#Container.DataItem("JOB")%></b></td> <td><b><%#Container.DataItem("SALARY")%></b></td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
<?xml version="1.0" ?> <!-- Data for BCC Departments --> <bcc> <option> <optionname>Programming</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Webmaster</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Networking</optionname> <optionadv>Arruda</optionadv> </option> <option> <optionname>Business Information</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Multimedia/Internet</optionname> <optionadv>Vieira</optionadv> </option> <option> <optionname>Computer Science</optionname> <optionadv>Ferreira</optionadv> </option> <option> <optionname>Information Systems</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Accounting</optionname> <optionadv>Garand</optionadv> </option> <option> <optionname>Marketing Management</optionname> <optionadv>Leonard</optionadv> </option> <option> <optionname>Retail Management</optionname> <optionadv>Dickens</optionadv> </option> <option> <optionname>Business Administration</optionname> <optionadv>Leonard</optionadv> </option> </bcc>
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim optionData=New DataSet optionData.ReadXml(MapPath("bccopt.xml")) optionSet.DataSource=optionData optionSet.DataBind() end if end sub </script> <html> <body> <form runat="server"> <h2>Option List</h2> <asp:Repeater id="optionSet" runat="server"> <HeaderTemplate> <table border="2" width="60%"> <tr> <th>Option Name</th> <th>Option Advisor</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("optionname")%></td> <td><%#Container.DataItem("optionadv")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
<PERSONNEL> <EMPLOYEE> <FIRST_NAME>Stephen</FIRST_NAME> <LAST_NAME>Daniels</LAST_NAME> <DEPT>Sports</DEPT> <JOB>Reporter</JOB> <SALARY>50000</SALARY> </EMPLOYEE> <EMPLOYEE> <FIRST_NAME>Al</FIRST_NAME> <LAST_NAME>Richards</LAST_NAME> <DEPT>Engineering</DEPT> <JOB>Consultant</JOB> <SALARY>95000</SALARY> </EMPLOYEE> The root is PERSONNEL which is made up of EMPLOYEE which has multiple occurrences and is basically the record. You can then think of the record as containing five fields: FIRST_NAME, LAST_NAME, DEPT, JOB and SALARY. The root is bcc which is made up of option which has multiple occurrences and is basically the record. You can then think of the record as containing two fields: optionname and optionadv. <bcc> <option> <optionname>Programming</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Webmaster</optionname> <optionadv>Grocer</optionadv> </option>
<?xml version="1.0" ?> <!-- Data for BCC Departments --> <bcc> <department> <deptname>Computer Information Systems</deptname> <optiontype> <typename>Career</typename> <option> <optionname>Programming</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Webmaster</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Networking</optionname> <optionadv>Arruda</optionadv> </option> <option> <optionname>Business Information</optionname> <optionadv>Grocer</optionadv> </option> <option> <optionname>Multimedia/Internet</optionname> <optionadv>Vieira</optionadv> </option> </optiontype> <optiontype> <typename>Transfer</typename>
<option> <optionname>Computer Science</optionname> <optionadv>Ferreira</optionadv> </option> <option> <optionname>Information Systems</optionname> <optionadv>Grocer</optionadv> </option> </optiontype> </department> <department> <deptname>Business</deptname> <optiontype> <typename>Career</typename> <option> <optionname>Accounting</optionname> <optionadv>Garand</optionadv> </option> <option> <optionname>Marketing Management</optionname> <optionadv>Leonard</optionadv> </option> <option> <optionname>Retail Management</optionname> <optionadv>Dickens</optionadv> </option> </optiontype> <optiontype> <typename>Transfer</typename> <option> <optionname>Business Administration</optionname> <optionadv>Leonard</optionadv> </option> </optiontype> </department> </bcc>
As you can see, three tables were set up - one to deal with department, one to deal with optiontype and one to deal with option. <bcc> <department> <deptname>Computer Information Systems</deptname> <optiontype> <typename>Career</typename> <option> <optionname>Programming</optionname> <optionadv>Grocer</optionadv> </option> The relationships are expressed with nested set to true. Option is nested under optiontype and optiontype is nested under department. Computer Information Systems is the first deptname within department (id is 0) and Business is the second deptname with department (id is 1).
The optiontype Table shows that Career is under the department with the id=0 and is optiontype=0. The next occurrence is Transfer which is still under department with id=0 and it has optiontype=0. The next occurrence of Career is with department id=1 and under optiontype it is 2 etc. The option Table shown here shows the optiontype id and then the optionname and option adviser. Note that the first group was under Computer Information Systems and Career and their were 5 options. You see this with optiontype having id=0 on the first 5. Computer Information Systems and Transfer had only two - you can also see that.
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim optionData=New DataSet optionData.ReadXml(MapPath("bccoptions.xml")) dgrTables.DataSource=optionData.Tables dgrTables.DataBind() dgrRelations.DataSource=optionData.Relations dgrRelations.DataBind() dgrDept.DataSource=optionData.Tables("department") dgrDept.DataBind() dgrOptTyp.DataSource=optionData.Tables("optiontype") dgrOptTyp.DataBind() dgrOptn.DataSource=optionData.Tables("option") dgrOptn.DataBind() end if end sub </script> Defines a new DataSet named optionData which is related to the physical bccoptions.xml for reading. Establishes the Tables collection of the optionData dataset as the data source for the dgrTables datagrid and binds the data. Establishes the “option” table in the Tables collection of the optionData dataset as the data source for the dgrOptn datagrid within the tables collection of the DataSet and bind the data.
<html> <body> <form runat="server"> <h2>Tables</h2> <asp:datagrid id="dgrTables" runat="server"> </asp:datagrid> <br> <br> <h2>Relationships</h2> <br> <asp:datagrid id="dgrRelations" runat="server"> </asp:datagrid> <br> <br> <h2>department Table</h2> <br> <asp:datagrid id="dgrDept" runat="server"> </asp:datagrid> <br> <br> <h2>optiontype Table</h2> <br> <asp:datagrid id="dgrOptTyp" runat="server"> </asp:datagrid> <br> <br> <h2>option Table</h2> <br> <asp:datagrid id="dgrOptn" runat="server"> </asp:datagrid> </form> </body></html>
Here we recognize bcc. Here we can see optiontype which includes typename and options. Here we can see department which includes deptname and optiontype. Here we can see the option which contains optionname and optionadv.
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim optionData=New DataSet optionData.ReadXml(MapPath("bccoptions.xml")) xmpResults.InnerHtml = "" xmpResults.InnerHtml = optionData.getXmlSchema() & vbCrlf & vbCrlf xmpResultd.InnerHtml = "" xmpResultd.InnerHtml = optionData.getXml() & vbCrlf & vbCrlf end if end sub </script> <html> <body> <form runat="server"> <h2>Option List</h2> <xmp id="xmpResults" runat="server" /> <xmp id="xmpResultd" runat="server" /> </form> </body> </html>
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim optionData=New DataSet optionData.ReadXml(MapPath("bccoptions.xml")) optionSet.DataSource=optionData.Tables("option") optionSet.DataBind() end if end sub </script> Since there are three tables involved in the structure of bccoptions.xml, I need to specify which table in the Tables collection I want to use for this program. I am specifying the option table.
On the previous slide (the beginning of this program), I defined optionSet as: optionSet.DataSource=optionData.Tables("option") <html> <body> <form runat="server"> <h2>Option List</h2> <asp:Repeater id="optionSet" runat="server"> <HeaderTemplate> <table border="2" width="60%"> <tr> <th>Option Name</th> <th>Option Advisor</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("optionname")%></td> <td><%#Container.DataItem("optionadv")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>