200 likes | 354 Views
ASP and HTML. Anchor Tag. <a href= "http://www.yahoo.com/" > testAnchorTag </a> Demo: ASPNet/ASPNETProdListSelf.ASPX. Creating Links. <p> Click a link to view a product: </p> <% dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:sales2k.mdb"
E N D
Anchor Tag • <a href="http://www.yahoo.com/">testAnchorTag</a> • Demo: ASPNet/ASPNETProdListSelf.ASPX
Creating Links <p>Click a link to view a product:</p> <% dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from product;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True response.write ("<p><a href='http://dchaolaptop/aspnet/aspnetshowprod.aspx?" & "pid=" & objDataReader("pid") & "'>" & objDataReader("description") & "</a></p><br>") loop %>
Program that Response to links <body> <% dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from product where pid = '" dim ProdID as String ProdID=Request.QueryString("pid") strSQL=strSQL & ProdID & "';" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() objDataReader.Read response.write ("PID = " & objDataReader("pid") & "<br>") response.write ("Descrition = " & objDataReader("description") & "<br>") response.write ("Price = " & objDataReader("price")) %> <form method="POST" action="addToCart.aspx"> <p>Qty: <input type="text" name="qty" size="20"></p> <p><input type="submit" value="AddToCart" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> </body>
Links that Reference Itself <body> <p>Click a link to view a product:</p> <% dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from product;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True response.write ("<p><a href='http://dchaolaptop/aspnet/aspnetProdListSelf.aspx?" & "pid=" & objDataReader("pid") & "'>" & objDataReader("description") & "</a></p><br>") loop %> </body> Note: The anchor tag will load the referenced page, but does not trigger Post Back.
Code in the Same Page Response to Link sub Page_Load() dim ProdID as String ProdID=Request.QueryString("pid") if Not ProdID="" Then dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from product where pid = '" strSQL=strSQL & ProdID & "';" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() objDataReader.Read response.write ("PID = " & objDataReader("pid") & "<br>") response.write ("Descrition = " & objDataReader("description") & "<br>") response.write ("Price = " & objDataReader("price")) end if end sub
Creating Forms • Creating a form for each product in the product table. • Demo: ASPNet/ASPNetProdListForm.aspx
<% dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from product;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Do While objDataReader.Read() = True %> <form method="POST" action="addToCart.aspx" style="border-style: double"> <% response.write ("PID = " & objDataReader("pid") & "<br>") response.write ("Descrition = " & objDataReader("description") & "<br>") response.write ("Price = " & objDataReader("price")) %> <p>Qty: <input type="text" name="qty" size="20"></p> <p><input type="submit" value="AddToCart" name="B1"><input type="reset" value="Reset" name="B2"></p> <input type="hidden" name="HiddenPID" value=objDataReader("pid")> </form> <% loop %>
Picture File • Access: • OLE field • Picture file name • Creating links to picture files • Insert pictures in web page • IMG tag
Creating Links to Pictures <script runat="server"> sub Page_Load() dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select PicID, PicDescription, PicPath from PictureTable;" dim objComm as new OledbCommand(strSQL,objConn) objConn.open() dim objDataReader as oledbDataReader objDataReader=objComm.executeReader() do while objDataReader.Read=True response.write ("<p><a href='http://dchaolaptop/images/" & objDataReader("PicPath") & "'>" & objDataReader("PicDescription") & "</a></p><br>") loop objConn.close() end sub </script>
Insert Pictures <script runat="server"> sub Page_Load() response.write("<p align='center'><font size='5'><b>Available Pictures</b></font></p>") dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select PicID, PicDescription, PicPath from PictureTable;" dim objComm as new OledbCommand(strSQL,objConn) objConn.open() dim objDataReader as oledbDataReader objDataReader=objComm.executeReader() do while objDataReader.Read=True response.write ("<p><img border='0' src='http://dchaolaptop/images/" & objDataReader("PicPath") & "' width='198' height='151'></p>") loop objConn.close() end sub </script>
Creating Forms with Images • ASPNet/AspNetProdListFormPic.Aspx
ASP.Net Repeater Control • The Repeater is a basic templated data-bound list. It has no built-in layout or styles, so you must explicitly declare all HTML layout, formatting, and style tags within the control's templates. • The Repeater has no built-in editing support. • The Repeater uses template to control formatting and data binding.
Repeater Control’s Templates • Five types of templates: • HeaderTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, FooterTemplate • Note 1: A Repeater binds its ItemTemplate and AlternatingItemTemplate to a DataSource. The HeaderTemplate, FooterTemplate, and SeparatorTemplate are not data-bound. • Note 2: At a minimum, every Repeater must define an ItemTemplate.
Creating a Table Using Repeater • To create a table using templates, include the begin table tag <table> in the HeaderTemplate, a single table row tag <tr> and <td> in the ItemTemplate, and the end table tag (</table>) in the FooterTemplate.
Repeater Example Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OledbConnection(strConn) Dim strSQL As String = "select * from customer;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Repeater1.DataSource = objDataReader Repeater1.DataBind() End Sub
<form id="Form1" method="post" runat="server"> <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <%# container.dataItem("cname") %> </ItemTemplate> <AlternatingItemTemplate> <font color="#ff33ff#"> <%# container.dataItem("cname") %> </font> </AlternatingItemTemplate> <SeparatorTemplate> <hr> </SeparatorTemplate> </asp:Repeater> </form> Note: Databinding tag <%# Container.DataItem(“Cname”) %>
Creating a Table Using Repeater <asp:Repeater id="Repeater1" runat="server"> <HeaderTemplate> <table border=1> <tr bgcolor=LightGrey> <th>CID</th> <th>Cname</th> <th>City</th> <th>Rating</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# container.dataItem("cid") %></td> <td> <%# container.dataItem("cname") %></td> <td> <%# container.dataItem("city") %></td> <td> <%# container.dataItem("rating") %></td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr bgcolor=#ff00ff> <td> <%# container.dataItem("cid") %></td> <td> <%# container.dataItem("cname") %></td> <td> <%# container.dataItem("city") %></td> <td> <%# container.dataItem("rating") %></td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
Creating Links Using Repeater Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OledbConnection(strConn) Dim strSQL As String = "select * from product;" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() Repeater1.DataSource = objDataReader Repeater1.DataBind() objConn.Close() End Sub
<form id="Form1" method="post" runat="server"> <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <p><a href='http://dchaolaptop/aspnet/aspnetshowprod.aspx?pid=<%# container.dataItem("pid") %>'><%# container.dataItem("description") %></a></p><br> </ItemTemplate> </asp:Repeater> </form>