|
我在gridview的模板列中放置了一个LinkButton,用做一个处理操作的按钮,原来打算在LinkButton的click事件里面处理,后来处理的时候发现,获取LinkButton所触发的行的索引有点困难,考虑用findcontrol,但是后来没有搞好.
所以用了下面这种方面.
aspx页面代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Width="662px" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated"> <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> <RowStyle BackColor="#DEDFDE" ForeColor="Black" /> <Columns> <asp:BoundField DataField="SID" HeaderText="ID" /> <asp:BoundField DataField="title" DataFormatString="{0:c}" HeaderText="问卷列表" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Down" Text="点击下载"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /> </asp:GridView>
下面就利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。
下面的代码是如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。
/// <summary>
/// 为模版列LinkButton写入CommandArgument事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
// Retrieve the LinkButton control from the first column.
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
// Set the LinkButton's CommandArgument property with the row's index.
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}
} /// <summary> /// linkbutton的单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { //下载按扭
if (e.CommandName == "Down") {
//设置下载行高亮显示
this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");
//string index= this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
int sid =int.Parse( row.Cells[0].Text.ToString()); //save(sid); //down(sid); 这两个是我用到的函数...呵呵 } }
|