Posted on 2006-08-23 18:52
大大毛 閱讀(581)
評論(0) 編輯 收藏 所屬分類:
ASP.NET
???Repeater控件,共有五個模板,編輯該控件的模板只能在ASP源碼中進行
??????1.ItemTemplate,正常顯示項,如果定義了交替項模板,則僅代表奇數(shù)Item(從1開始算)
??????2.AlternatingItemTemplate,交替項,代表偶數(shù)Item
??????3.HeaderTemplate,頁眉,在控件的最前
??????4.FooterTemplate,頁腳,在控件的最后
??????5.SeparatorTemplate,分隔項,位于各Item之間,如使用<br/>實現(xiàn)各項換行
???適用于:
??????替代只讀顯示容器內(nèi)容的循環(huán),如將一個ResultSet顯示成一張表。
??????HeaderTemplate中使用<table><tr>,ItemTemplate/AlternatingItemTemplate中使用<td></td>,SeparatorTemplate中使用</tr><tr>,F(xiàn)ooterTemplate中使用</tr></table>可以實現(xiàn)一個 <table> 元素效果。
???DataList控件,共有七個模板
??????6.SelectedItemTemplate,選擇項
??????7.EditItemTemplate,編輯項
???適用于:
??????可編輯數(shù)據(jù)的顯示,因此在普通的DataSource指定后2006年8月24日,需要綁定數(shù)據(jù)源的主鍵列(指定控件的DataKeyField屬性)。
??????該控件提供了多種事件,由放在Item中的Button控件激發(fā)。
??????Button控件將會激發(fā)ItemCommand事件,可通過傳入的事件參數(shù)來引發(fā)其它事件,例如使用SelectedItem模板:
????
protected
?
void
?DataList1_ItemCommand(
object
?source,?DataListCommandEventArgs?e)?{
????????
if
?(e.CommandName?
==
?
"
Select
"
)?{?????????????????????????
//
判斷激發(fā)事件的Button.CommandName
????????????
this
.DataList1.SelectedIndex?
=
?e.Item.ItemIndex;??
//
讓激發(fā)事件的行被選中
????????????myDataBind();???????????????????????????????????????????????
//
這里需要進行重新綁定
????????}
????}
??????使用EditItemTemplate模板,將Button.CommandName指定成"edit"即可。
????
protected
?
void
?DataList1_EditCommand(
object
?source,?DataListCommandEventArgs?e)?{
????????
this
.DataList1.SelectedIndex?
=
?
-
1
;??????????????????????
//
放棄選擇
????????
this
.DataList1.EditItemIndex?
=
?e.Item.ItemIndex;????
//
讓當前行進入編輯狀態(tài)
????????myDataBind();
????}
??????在EditItemTemplate模板中可以將允許修改的列綁定到子控件上,并放入兩個Button,CommandName分別指定為"update","cancel"與DataList的事件相對應(yīng),再在對應(yīng)的數(shù)據(jù)更新/放棄事件中編程即可。
Cancel事件
????
protected
?
void
?DataList1_CancelCommand(
object
?source,?DataListCommandEventArgs?e)?{
????????
this
.DataList1.EditItemIndex?
=
?
-
1
;??
//
放棄編輯狀態(tài)
????????myDataBind();
????}
Update事件
????
protected
?
void
?DataList1_UpdateCommand(
object
?source,?DataListCommandEventArgs?e)?{
????????
string
?id?
=
?
this
.DataList1.DataKeys[e.Item.ItemIndex].ToString();???????
//
獲取編輯行所對應(yīng)的主鍵
????????SqlConnection?con?
=
?Db.getConnection();
????????
//
根據(jù)主鍵更新數(shù)據(jù)庫
????????SqlCommand?cmd?
=
?
new
?SqlCommand(
"
update?vote?set?topic=@topic?where?vID=@vID
"
,?con);
????????SqlParameter?p1?
=
?
new
?SqlParameter(
"
@topic
"
,?SqlDbType.VarChar,?
20
);
????????p1.Value?
=
?((TextBox)e.Item.FindControl(
"
txtTopic
"
)).Text.Trim();
????????cmd.Parameters.Add(p1);
????????SqlParameter?p2?
=
?
new
?SqlParameter(
"
@vID
"
,?SqlDbType.Int);
????????p2.Value?
=
?id;
????????cmd.Parameters.Add(p2);
????????con.Open();
????????cmd.ExecuteNonQuery();
????????con.Close();
????????
this
.DataList1.EditItemIndex?
=
?
-
1
;??
//
放棄編輯狀態(tài)
????????myDataBind();
????}
??????將Button控件的CommandName指定為"delete",可激發(fā)數(shù)據(jù)刪除事件:
Delete事件
????
protected
?
void
?DataList1_DeleteCommand(
object
?source,?DataListCommandEventArgs?e)?{
????????
string
?id?
=
?
this
.DataList1.DataKeys[e.Item.ItemIndex].ToString();???????
//
獲取編輯行所對應(yīng)的主鍵
????????SqlConnection?con?
=
?Db.getConnection();
????????
//
根據(jù)主鍵更新數(shù)據(jù)庫
????????SqlCommand?cmd?
=
?
new
?SqlCommand(
"
delete?vote?where?vID=@vID
"
,?con);
????????SqlParameter?p1?
=
?
new
?SqlParameter(
"
@vID
"
,?SqlDbType.Int);
????????p1.Value?
=
?id;
????????cmd.Parameters.Add(p1);
????????con.Open();
????????cmd.ExecuteNonQuery();
????????con.Close();
????????myDataBind();
????}
??????通常需要在Update/Delete按鈕上加入確認,如果是靜態(tài)提示可以直接在Button的屬性中加入:
<
asp:Button?ID
=
"
btnDelete
"
?runat
=
"
server
"
?CommandName
=
"
delete
"
?Text
=
"
刪除
"
?OnClientClick
=
"
return?confirm(
'delete?'
);
"
?
/>
??????在這里可以看到,是利用了Button控件的 OnClientClick 屬性調(diào)用本地的 JS函數(shù) confirm() 來實現(xiàn)確認的功能。如果想提示動態(tài)消息(如提示一個ID),偶沒有試出來,不過可以寫在代碼段里,利用ItemCreated事件,這樣:

ItemCreated事件
????protected?void?DataList1_ItemCreated(object?sender,?DataListItemEventArgs?e)?{
????????//此事件表示的是Item的建立,而在不同的模板中是會有不同的呈現(xiàn)形式的,因此需要對當前的狀態(tài)進行判斷
????????//該delete按鈕是在正常瀏覽狀態(tài)下的,在select/edit狀態(tài)下并不存在
????????if?(e.Item.ItemType?==?ListItemType.Item?||?e.Item.ItemType?==?ListItemType.AlternatingItem)?{
????????????string?s1?=?"return?confirm('你確認要刪除ID=";
????????????s1?=?s1?+?DataBinder.Eval(e.Item.DataItem,?"vID");
????????????s1?=?s1?+?"的投票嗎?');";
????????????((Button)e.Item.FindControl("btnDelete")).Attributes.Add("onClick",?s1);
????????}
????}??????這樣一來就可以在Button激發(fā)DataList的事件之前進行簡單的確認了,不過需要注意的是:
?????????.用這種方式時,在運行時如果有錯誤是不會有什么提示消息的,而且流程直接向下進行;
?????????.如果同時使用了兩種,那么Button上的OnClientClick屬性優(yōu)先。
??????該控件的顯示方式也與Repeater控件大不相同,它的顯示方式分為"表"和"流"兩種,默認使用表布局。
?????????使用表布局時控件會自動生成一個<table>元素,樣式則由各模板對應(yīng)的<style>指定,在該控件的運用中不應(yīng)該再使用如Repeater那種自構(gòu)造<table>元素的方法,因為該控件中經(jīng)常需要用到FindControls方法,自行加入元素會影響到該方法。
??????可以看到模板還具有自己所對應(yīng)的Style,這個樣式與當前Item所使用的模塊對應(yīng),例如選中第1項,如果沒有配置<SelectedItemStyle>才會使用<ItemStyle>所定義的樣式。
???手工分頁:
??????如上面提到的Repeater、DataList控件都沒有提供分頁的功能,此時可以對數(shù)據(jù)源進行分頁再配合自定義的導(dǎo)航條來實現(xiàn)分頁效果。
??????手工分頁,需要用到 System.Web.UI.WebControls.PagedDataSource 對象。
??????例如:在頁面中加入lblPageNo控件以保存當前頁號(text="0"),btnPrevious表示上一頁,btnNext表示下一頁。

新的代碼

????/**////?<summary>
????///?數(shù)據(jù)綁定
????///?</summary>

????private?void?myDataBind()?
{
????????SqlConnection?con?=?Db.getConnection();

????????SqlDataAdapter?sda?=?new?SqlDataAdapter();
????????sda.SelectCommand?=?new?SqlCommand("select?*?from?vote",con);

????????DataTable?table?=?new?DataTable("vote");
????????sda.Fill(table);
????????
????????//使用了分頁
????????PagedDataSource?pds?=?new?PagedDataSource();????//使用一個PagedDataSource對象
????????pds.DataSource?=?table.DefaultView;
????????pds.PageSize?=?3;???????????????????????????????//每頁3條記錄
????????pds.AllowPaging?=?true;?????????????????????????//使用分頁效果
????????int?pageNo?=?int.Parse(this.lblPageNo.Text);????//取得保留的當前頁碼,初始值="0"
????????pds.CurrentPageIndex?=?pageNo;??????????????????//設(shè)置當前頁
????????//開關(guān)上,下頁導(dǎo)航按鈕
????????this.btnPrevious.Enabled?=?!(pageNo?<=?0);
????????this.btnNext.Enabled?=?!(pageNo?>=?pds.PageCount?-?1);
????????//將DataList的數(shù)據(jù)源指向該PagedDataSource
????????this.DataList1.DataSource?=?pds;
????????this.DataList1.DataBind();
????????this.DataList1.DataKeyField?=?"vID";????????????//指定需要綁定的主鍵列
????????DataList1.DataBind();

????????con.Close();
????}

????//上一頁

????protected?void?tbtnPrevious_Click(object?sender,?EventArgs?e)?
{
????????this.lblPageNo.Text?=?Convert.ToString(int.Parse(this.lblPageNo.Text)?-?1);
????????myDataBind();
????}
????//下一頁

????protected?void?btnNext_Click(object?sender,?EventArgs?e)?
{
????????this.lblPageNo.Text?=?Convert.ToString(int.Parse(this.lblPageNo.Text)?+?1);
????????myDataBind();
????}

???排序:
??????利用數(shù)據(jù)視圖DataView的Sort屬性來實現(xiàn)排序,將其屬性指定為一個字段名 + 排序規(guī)則。
??????在GridView中列上可以設(shè)置SortExpression排序表達式,并在Sorting事件中指定DataView的Sort為該事件的SortExpression即可。
???GridView:
??????就是一個數(shù)據(jù)表格,在以前的版本中叫DataGrid。它的功能比DataList更為強大,支持數(shù)據(jù)表格顯示、按列的控制(DataList按名字就只支持一列)、列的排序、分頁、空頁控制等多種功能,可以將DataList看做是一個輕量級的控件,不過從這兩種控件的屬性及使用方式來看,風(fēng)格完全不同,這可能也是兩控件命名不一的原因吧。
?