我有一个gridview,我试图在代码所在的textview中获取标签的文本值:
<asp:TemplateField HeaderText="someText" SortExpression="someExpression">
<ItemTemplate>
<asp:Label ID="someLabel" runat="server" Text='<%# Bind("someField") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
我希望能够从selectedRow中获取“ someLabel”的文本值,作为我背后的代码中的字符串。
最佳答案
Label someLabel = selectedRow.FindControl("someLabel") as Label;
编辑:
private static Control FindControlRecursive(Control parent, string id)
{
if (parent.ID== id)
{
return parent;
}
return (from Control ctl in parent.Controls select FindControlRecursive(ctl, id))
.FirstOrDefault(objCtl => objCtl != null);
}
和
Label someLabel = FindControlRecursive(GridView.SelectedRow, "someLabel") as Label;
编辑2:
private void imageButton_Click(object sender, EventArgs e)
{
Label someLabel = (sender as Control).Parent.FindControl("someLabel") as Label;
}