简体   繁体   中英

Accessing GridView Label field from Code-Behind

I have a gridview, and I'm trying to get the text value of a label in the textview where the code is:

<asp:TemplateField HeaderText="someText" SortExpression="someExpression">
    <ItemTemplate>
        <asp:Label ID="someLabel" runat="server" Text='<%# Bind("someField") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

I want to be able to get the text value of the "someLabel" from the selectedRow as a string in my codebehind.

Label someLabel = selectedRow.FindControl("someLabel") as Label;

EDIT:

    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);
    }

and

Label someLabel = FindControlRecursive(GridView.SelectedRow, "someLabel") as Label;

EDIT 2:

private void imageButton_Click(object sender, EventArgs e)
{
     Label someLabel = (sender as Control).Parent.FindControl("someLabel") as Label;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM