一個很典型的情境,一個GRIDVIEW中的每個資料行,有兩個圖片按鈕 “操作(修改)”,“刪除”,
先來看前端的代碼
<asp:ScriptManager ID="sm" runat="server" ></asp:ScriptManager>
<table class="Table" border="0" cellpadding="5" cellspacing="0">
<tr>
<td colspan="2">
<asp:UpdatePanel runat="server" ID="up">
<ContentTemplate>
<asp:GridView ID="gvCategory" runat="server" Width="100%" AutoGenerateColumns="False" SkinID="gvSkin" OnRowCommand="gvCategory_RowCommand" OnRowDataBound="gvCategory_RowDataBound" AllowPaging="True" OnPageIndexChanging="gvCategory_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="相簿分類">
<ItemTemplate>
<a href='Category.aspx?CategoryID=<%# Eval("ID") %>'><%# Eval("Name") %>(<%# Eval("PhotoCount") %>)</a>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="70%" />
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="分類狀態">
<ItemTemplate>
<%# (byte)Eval("Status") == 0 ? "公開" : "私人" %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="18%" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:ImageButton ID="imgUpdate" runat="server" CommandArgument='<%# Eval("ID") %>' ImageUrl="~/App_Themes/ASPNETAjaxWeb/Images/edit.PNG" CommandName="update" />
<asp:ImageButton ID="imgDelete" runat="server" Visible='<%# (int)Eval("PhotoCount") > 0 ? false : true %>' CommandArgument='<%# Eval("ID") %>' ImageUrl="~/App_Themes/ASPNETAjaxWeb/Images/delete.PNG" CommandName="del" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="12%" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
要點是,修改和刪除的圖片按鈕的commandname要設定好,要注意commandargument的設定,因為這裡代表裡當前行的ID,刪除的時候要用
然後要設計rowcommand事件,在使用者單擊控制項每行中的按鈕時觸發
protected void gvCategory_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName.ToLower() == "update")
{ ///重新導向到修改分類頁面
Response.Redirect("~/UpdateCategory.aspx?CategoryID=" + e.CommandArgument.ToString());
return;
}
if(e.CommandName.ToLower() == "del")
{ ///刪除選擇的相簿分類
Album album = new Album();
if(album.DeleteCategory(Int32.Parse(e.CommandArgument.ToString())) > 0)
{
BindPageData();
}
return;
}
而要為刪除按鈕加提示資訊,則需要在rowdatabound事件中說明,在每一行資料繫結後觸發,為每個刪除的按鈕增加用戶端的確認
protected void gvCategory_RowDataBound(object sender,GridViewRowEventArgs e)
{ ///添加刪除確認的對話方塊
ImageButton imgDelete = (ImageButton)e.Row.FindControl("imgDelete");
if(imgDelete != null)
{
imgDelete.Attributes.Add("onclick","return confirm(\"您確認要刪除當前行的相簿分類嗎?\");");
}
}
當然,分頁部分也寫上吧
protected void gvCategory_PageIndexChanging(object sender,GridViewPageEventArgs e)
{ ///設定新的頁碼,並重新顯示資料
gvCategory.PageIndex = e.NewPageIndex;
BindPageData();
}
還有rowdatabound事件也經常用,比如資料庫取出來的1,0欄位,要變為“男”,“女”顯示,則這樣
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判斷當前行是否是資料行
if (e.Row.RowType == DataControlRowType.DataRow)
{ //用FindControl方法找到模板中的Label控制項
Label lb1= (Label)e.Row.FindControl("Label1");
//因為RowDataBound是發生在資料繫結之後,所以我們可以
//判斷Label繫結資料,如果是True,就更改其text屬性為男
if (lb1.Text== "True")
lb1.Text = "男";
else
lb1.Text = "female";
}
}