DataList控制項
實現查看詳細資料按鈕
1. 在普通項目範本中添加一個按鈕 將該按鈕的 commandName 屬性設定為”select”
2. 在DataList的ItemCommand 事件中 實現選擇行
代碼
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName=="select")
{
this.DataList1.SelectedIndex = e.Item.ItemIndex;
this.DataList1.DataBind();
}
}
3. 在選中項 模板中 顯示需要顯示的資訊
<SelectedItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"title") %>
發行日期:<%# DataBinder.Eval(Container.DataItem,"publishDate","{0:d}") %>
<%# DataBinder.Eval(Container.DataItem,"contentdescription") %>
</SelectedItemTemplate>
GirdView控制項
添加欄位頁面:
HyperLinkField 超連結項
主要屬性:
DataNavigateUrlFields 將要繫結資料源欄位名稱
DataNavigateUrlFormatString 將以get方式提交到目標頁面如:BookDetail.aspx?id={0}
串連後面的?id= 是提交地址 {0} 是預留位置
或者:
比如將要提交的頁面是一個有序的頁面它根據表中的ID欄位來命名頁面名稱
DataNavigateUrlFields 屬性為 BookName
DataNavigateUrlFormatString 屬性為 Books/{0}.aspx
RowDataBound事件 實現滑鼠移至上方當前行變色
代碼
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//如果當前行的類型為資料行
{
//將當前綁定的行添加一個屬性
//當滑鼠移至上方 設定背景為藍色 e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
//當滑鼠離開 恢複背景顏色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
}
}