急求!如何在GridView的OnRowCommand事件 用FindControl找到模版控件?

来源:百度知道 编辑:UC知道 时间:2024/06/07 06:15:59
Html:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" Width="100%">
<Columns>
<asp:TemplateField HeaderText="序号" Visible="False">
<ItemTemplate>
<asp:Label ID="Lab_DId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CloDId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandName="Edit" Text="

RowDataBound事件里面把行的索引绑定到button的CommandArgument 属性上面!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton rb = (LinkButton)e.Row.FindControl("LinkButton1");
if (rb != null)
rb.CommandArgument = e.Row.RowIndex.ToString();
//把选中行的RowIndex也传过去,提交后在服务器端取值时用
}
}

然后rowcommand事件里面这么写就找到啦!
Label lab1 = (Label)(GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("Label1"));