一、了解Template
AlternatingItemTemplate定义交替行的内容和外观,如果没有规定模板,则使用ItemTemplate;
EditItemTemplate定义当前正在编辑的行的内容和外观。该模板包含输入字段,而且还可能包含验证程序;
FooterTemplate定义该行的页脚的内容和外观;
HeaderTemplate定义该行的标题的内容和外观;
ItemTemplate定义该行的默认内容和外观。
二、模板应用
aspx代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ConferenceNo,VerNum,AttendeeCategory,Attendee"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound"
onrowcreated="GridView1_RowCreated">
<Columns>
其它字段
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDel" runat="server"
CommandArgument='<%# Eval("ConferenceNo") %>' οnclick="btnDel_Click"
Text="del" />
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("ConferenceNo") %>' CommandName="2">Link1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="3">Link2</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server"
CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="4">Link3</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server">Link4</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="1" Text="按钮" />
</Columns>
</asp:GridView>
aspx.cs代码
/// <summary>
/// 2、模板中自定义Button和CommandArgument
/// </summary>
protected void btnDel_Click(object sender, EventArgs e)
{
string strCommandArgument = ((Button)sender).CommandArgument;
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strCommandArgument + "')",true);
}
/// <summary>
/// 1、ButtonField和RowCommand
/// </summary>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//1、ButtonField和RowCommand
if (e.CommandName == "1")
{
//在ButtonField中CommandArgument属性是当前行索引(RowIndex)不需要开发人员设置
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
//3、模板中自定义Button和RowCommand
if (e.CommandName == "2")
{
//自定义Button中CommandArgument属性是开发人员设置
string strConferenceNo = e.CommandArgument.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
//4、模板中自定义Button和RowCommand
if (e.CommandName == "3")
{
//在RowDataBound针对模板中自定义Button的CommandArgument赋值
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
//5、模板中自定义Button和RowCommand
if (e.CommandName == "4")
{
//CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
}
/// <summary>
/// 行绑定事件
/// 1、常用于行选择事件注册
/// 2、特殊数据处理
/// </summary>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//4、针对模板中自定义Button的CommandArgument赋值
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton2");
lnk.CommandArgument = e.Row.RowIndex.ToString();
}
}
/// <summary>
/// GridView行创建后
/// </summary>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//5、针对模板中自定义Button的CommandArgument赋值
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton4");
lnk.Click += new EventHandler(lnk_Click);//按+=再按2次Tab键,实现快速注册事件
}
}
void lnk_Click(object sender, EventArgs e)
{
//获取当前行
GridViewRow grdRow = (GridViewRow)((LinkButton)sender).Parent.Parent;
string strConferenceNo = grdRow.Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}