十七.GridView
一.基礎部分
分頁:只有用SqlDataSource作爲數據源的時候纔有
在用ObjectDataSource的時候需要另外寫代碼
另外該控件的“排序”和“分頁”都打勾的時候,先進行整體排序,再分頁;
當啓用“分頁”時,還需再其屬性面板指定以下屬性:
AllowPaging
PageSize
PageIndex:當前索引
PageCount:這個會自動結算出來,得到總頁數
1.BoundField字段:
DataFormatString:
{0:c}:貨幣
{0:N}:數字
{0:yy-mm-dd}:日期
在模版列中,會用到<%# Bind(...)%>和<%# eval_r(...)%>
其中Bind是雙向數據綁定的,不能單獨使用,一般用於textbox等的Text屬性,並且要用單引號,比如 標籤內屬性Text='<%# Bind("Id")%>'
而Eval是單向數據綁定,可單獨使用,常用於進行格式化,比如:Text='<%# eval_r("publishDate","{0:yy-mm-dd}")%>'
另外在進行三元表達式的時候需要進行轉型,否則會沒效果,如下:
Text='<%# eval_r("gender").ToString()=="1"?"男":"女"%>'
又比如:
<asp:Label ID="Label2" runat="server" Text='<%# eval_r("gender").ToString()=="True"?"男 ":"女" %>'></asp:Label> //Gender取值後要進行轉換.ToString()
2.ButtonField------------>GridView的RowCommand事件
CommandName:區分是哪個按鈕-->e.CommandName,用於獲取命令名
另外如果有“列”不想被修改,可以設置其readonly屬性true
3.HyperLinkField
屬性:
DataNavigatorUrlFormatString:設置url:detail.aspx?id={0}
DataVavigatorUrlFields = "id"------------------------------------↑
DataTextField:顯示字段
4.string text = gvHr.Rows[index].cells[0].Text
相當於 GridViewRow gr = gvHr.Rows[index];
string text = gr.Cells[0].Text;
5.一般gridview中的主鍵不顯示出來,存放在DataKeyNames屬性中,用DataKeys來獲取;
另外如果把某列的visible屬性設爲false,那麼該列將不會往返於服務器和客戶端
eg:
gvHr.DataKeyNames = new string[]{"Id","Name"};
gvHr.DataBind();
單個時取值: gvHr.DataKeys[index].Value.ToString();
多個時取值: gvHr.DataKeys[index].Values["主鍵字段1"].ToString();
6.光棒效果
在RowDataBound事件中寫:
if(e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attribute.Add("onmouseover","currentColor=this.style.backgroundColor;this.style.backgroundColor='red'"); //red用單引號
e.Row.Attribute.Add("onmouseout","this.style.backgroundColor=currentColor;")
}
二.進階
1.GridView取得行和列的“單元格”的語法:
GvId.Rows[index].Cells[index].Text;
或GridViewRow gvr = gvId.Rows[index];
string text = gvr.Cells[index].Text; //都是通過Text屬性來取值
2.隱式存儲主鍵:
存:gvId.DataKeyNames = new string[]{"主鍵字段名稱1","主鍵字段名稱2"......};
GvId.DataBind();
PS:這裏的主鍵字段名稱1是實體類中的屬性名,對應數據庫中主鍵字段
取:gvId.DataKeys[index].Value.ToString();
gvId.DataKeys[index].Values["名稱1"].ToString();//有的時候這樣子會報錯
GvId.DataKeys[index]["名稱1"].Value.ToString(); //有的時候得這樣取
3.在gridView中如何將取出來的0,1轉換爲中文,比如性別用"男女"表示
通過模板列來實現:
<itemTemplate>中通過表達式來實現
<asp:Label runat="server " ID="gender" Text='<%# eval_r("sex").ToString()=="True"?"男":"女"%>' ></asp:Label>
</itemTemplate>
4.在模板列中可以調用服務器端的方法:
假設在當前頁面的後置代碼中有這麼個方法:
public string GetDeptName(stringid){....}
在頁面模板列中調用:
<itemTemplate>中通過表達式來實現
<asp:Label runat="server " ID="gender"
Text='<%# GetDeptName(eval_r("id").ToString())%>' ></asp:Label>
</itemTemplate>
這裏需要注意下傳的參數eval_r("id").ToString(),好像這裏無論怎麼轉型,傳到後置代碼中GetDeptName()方法中的參數都是object類型,因爲之前做的一個Vip價格方法,方法在定義的時候直接用object來定義形參,然後在方法內部再進行轉型,比如:public string GetVipPrice(object price,object discount)
5.按鈕事件:
a) RowCommand事件:不獲得行號,即SelectedIndex屬性永遠是 -1
b) SelectedIndexChanging選擇事件:
e.NewSelectedIndex:默認值是-1
例子:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{//選擇事件
//默認值爲-1
this.GridView1.SelectedIndex = e.NewSelectedIndex;
//顯示主鍵(顯式)
int index = e.NewSelectedIndex;
string key = this.GridView1.Rows[index].Cells[0].Text;
Response.Redirect("show.aspx?id="+key);
//無主鍵顯式(隱式)[前提:設置過DataKeyNames]
string index = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
//凡是gridView做的一般都是這樣子取主鍵的
}
c) RowEditing編輯事件:
一般只在代碼中寫:this.gvId.EditIndex = e.NewEditIndex;
Bind();//自定義Bind綁定方法
d) RowCancellingEdit取消事件:
一般值在代碼中寫:this.gvId.EditIndex = -1;
Bind();
e) RowUpdating更新事件:
string key = this.gvId.DataKeys[e.RowIndex].Value.ToString();
//獲得值,非模板列
1. string value = (this.gvId.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
2. string value = (this.gvId.Rows[e.RowIndex].FindControl("控件id") as TextBox).Text
這裏的2種方法是從單元格中查找所需的控件,依次將他轉換爲相應的控件來取值;注意這裏的Controls[0]和FindControl方法,有的時候會找不到控件而返回一個null導致爲初始化的錯誤產生
PS:一般不報錯,但是取到的值不是更新後的值時,一般就是Load事件了,IsPostBack屬性要判斷下
f) RowDeleting刪除事件:
方法跟上面的事件差不多,只要獲得主鍵,然後調用bll層的刪除方法即可
上面的LinkButton是手動在模板列中添加的控件,若要實現上面的刪除功能,還需要在頁面源碼中給gridView添加上模板列(CommandField裏面的刪除功能)
注意選中的地方,除了CommandName要用Delete外(會自動對應到Delete事件),還需要手動添加CommandArgument屬性
最後在RowDeleting事件中獲得該值:
LinkButton lnb = gvId.Rows[e.RowIndex].FindControl("lnkBtnDel") as LinkButton;
string id = lnb.CommandArgument.ToString();
多選刪除功能,只有一個總刪除按鈕時,用到checkbox
思路:
首先給刪除按鈕添加點擊事件,同上:
this.btnDel.Attributes.Add("onclick","return confirm('確認刪除?')");
然後給【刪除按鈕添加事件】btnDel_Click(object sender,EventArgs e)內添加
foreach(GridViewRow gr in this.gvId.Rows)
{
CheckBox chk = gr.FindControl("chbSelect") as CheckBox;
if(chk.Checked)
{
//獲取主鍵
string key = this.gvId.DataKeys[gr.RowIndex].Value.ToString();
//執行刪除操作
StringBuilder sbSql = new StringBuilder();
sbSql.AppendLine("DELETE FROM [users] WHERE");
sbSql.AppendLine(" [userid]=@userid");
SqlParameter[] param = new SqlParameter[]{
new sqlParameter("@userid",key)
};
DBHelper.ExecuteCommand(sbSql.ToString(), param);
}
}
//重新綁定
Bind();
- (2012-09-08 13:09:19)
- (2012-09-08 13:09:05)
- (2012-09-08 13:04:14)
- (2012-09-08 12:54:54)
- (2012-09-08 12:52:01)
- (2012-09-08 12:48:53)
- (2012-09-08 12:46:40)
- (2012-09-03 20:54:24)
- (2012-09-02 12:48:05)
- (2012-07-26 15:42:32)