1、
資料繫結
combox-->BingdSource-->ObjectDataSource
//串連資料庫
**後台代碼:
T_UserTableAdapter adpter=T_UserTableAdpter();
adpater.調用T_User表的自訂方法
//設定初始資料:
AppenddateBource="true";
2、Repeater 動態顯示資料表中所有資料:(相當於一個for語句我覺得)
#代表綁定資料
(1、模板:<ItemTemplate>主鍵:<%#Eval("Id")%> </ItemTemplate>
(2、其他模板(隔行顯示):<AlternatingTemplate></AlternatingTemplate>
(3、還有頭部尾部模板<HeaderItemplate>還有<FooterItemplate>
(4、可以在<%aaa(Eval("a")) %>中調用服務端函數
protected string aaa(object url){
return ResolveClientUrl("/../"+url);
}
(5、常見的類型語句:
**1:e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem
指觸發的類型為DadaList中的基本行或內容行 :用來判斷項是控制項內容中的項或交插項
**2:e.Item.ItemType == ListItemType.Header
指觸發的類型為DadaList中的標題列的內容:用來判斷項是不是控制項的表頭
**3搜尋資料:
ListItemType 枚舉 [C#]請參見
System.Web.UI.WebControls 命名空間 | DataGridItem.ItemType | DataListItem.ItemType | RepeaterItem.ItemType
命名空間: System.Web.UI.WebControls
ListItemType 枚舉表示可以包含在清單控制項中的不同項,例如 DataGrid、DataList 和 Repeater。典型的清單控制項由包含此枚舉所表示的元素的儲存格組成。
成員
成員名稱 說明
AlternatingItem 交替(從零開始的偶數索引)儲存格中的項。它是資料繫結的。
EditItem 清單控制項中當前處於編輯模式的項。它是資料繫結的。
Footer 清單控制項的頁尾。它不是資料繫結的。
Header 清單控制項的頁首。它不是資料繫結的。
Item 清單控制項中的項。它是資料繫結的。
Pager 頁導航,顯示定位到與 DataGrid 控制項關聯的不同頁的控制項。它不是資料繫結的。
SelectedItem 清單控制項中的選定項。它是資料繫結的。
Separator 清單控制項中項之間的分隔字元。它不是資料繫結的。
3、Repeater事件之ItemDataBound事件
(1、OnItemDataBound事件(綁定中擷取同一行相關資訊)
DataRowView MyRow= (DataRowView)e.Item.DataItem;
MyRow["這裡是相關欄位名稱"]
(2、後台代碼:擷取資料庫表中的列
//取出綁定行的資訊
DataRowView view=(DataRowView)e.Item.DataItem;
//強串連
var row=(項目名稱/檔案夾名/DAL/DataSetPersons/T_PersonsRow)view.Row;
(3、後台代碼:母片中的ID不能直接取
//獲得ID
TextBox txtId=(TextBox)e.Item.FindControl("id");
//根據ID進行操作
txtId.BackColor=Color.Red;
(4、aspx中:href='<%#Eval("URL")%>'
這樣在瀏覽器中點擊是不會到指定頁面的,應當改成
href='http://<%#Eval("URL")%>'
(5、aspx中:支援格式
<%#Eval("Date","yyyy-mm-dd")%>
4、Repeater事件之ItemCommand事件
** e.CommandName 名字
** e.CommandArgument 參數
**後台代碼擷取前台傳過來的參數例如ID: long id=Convert.ToInt64(e.CommandArgument);
**後台代碼:Repeater.DataBing//強制重新整理資料
(1、OnClientClick="return confirm("您真的要操作嗎?");"
(2、看aspx中id的類型,可以先頂一個id,然後在aspx.cs中查看它是什麼類型
//如下用法:
HtmlTableRow tr=(HtmlTableRow)e.Item.FindControl("id");
//自訂屬性::css具體的實現在頁面中,後台只是引用
**後台代碼:tr.Attributes["class"]="css中的.id";
(3、拋出錯誤: throw new Exception("自己定義的錯誤類型");
5、執行個體:
move_pic.aspx
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="move_pic.aspx.cs" Inherits="Move_Pic.move_pic" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head id="Head1" runat="server"> 7 <title></title> 8 <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> 9 10 </head>11 12 <body>13 <form id="form1" runat="server">14 <div>15 16 <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 17 DeleteMethod="Delete" InsertMethod="Insert" 18 OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 19 TypeName="Move_Pic.DAL.DataSet1TableAdapters.T_userInfoTableAdapter" 20 UpdateMethod="Update">21 <DeleteParameters>22 <asp:Parameter Name="Original_sUserName" Type="String" />23 </DeleteParameters>24 <InsertParameters>25 <asp:Parameter Name="sUserName" Type="String" />26 <asp:Parameter Name="sPassWord" Type="String" />27 <asp:Parameter Name="Email" Type="String" />28 <asp:Parameter Name="Age" Type="Int32" />29 <asp:Parameter Name="Pic_url" Type="String" />30 </InsertParameters>31 <UpdateParameters>32 <asp:Parameter Name="sPassWord" Type="String" />33 <asp:Parameter Name="Email" Type="String" />34 <asp:Parameter Name="Age" Type="Int32" />35 <asp:Parameter Name="Pic_url" Type="String" />36 <asp:Parameter Name="Original_sUserName" Type="String" />37 </UpdateParameters>38 </asp:ObjectDataSource>39 40 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">41 <HeaderTemplate>歡迎選購<table id="tableId"><tr></HeaderTemplate>42 <ItemTemplate><td>43 <img a1='<%#Eval("sUserName")%>' a2='<%#Eval("Age") %>'width="100",44 height="150" src='image/<%#Eval("Pic_url") %>' /></td>45 </ItemTemplate>46 <FooterTemplate></tr></table>歡迎下次光臨</FooterTemplate>47 </asp:Repeater>48 49 <script type="text/javascript">50 /*$("img").mousemove(function (e) {51 $("#sUserName").text($(this).attr("a1"));52 $("#Age").text($(this).attr("a2"));53 $("#divNone").css("top", e.pageY).css("left", e.pageX).show("slow");54 });*/55 //動態增長56 /* $(function () {57 $("#img").click(function () {58 //$(this).animate({ "width": "+=300", "height": "+=300" });59 $(this).animate({"width":"300","height":"300"});60 //$(this).attr("width", "300").attr("height", "300");61 });62 });*/63 $("#tableId img").mouseenter(function () {64 $(this).animate({ "width": "300", "height": "300" },"slow");65 //$(this).siblings().animate({ "width": "150", "height": "300" });66 //將自己排除在外67 $("#tableId img").not($(this)).animate({ "width": "100", "height": "150" }, "slow");68 });69 </script>70 71 </div>72 <div style="display:none" id="divNone">73 <p>姓名:<span id="sUserName"></span></p>74 <p>年齡:<span id="Age"></span></p>75 </div>76 77 </form>78 </body>79 80 </html>
資料庫表設計如下: