Home > DeveloperSection > Forums > How can I access a ButtonFields text
Ashish Pandey

Total Post:128

Points:898
Posted on    August-26-2014 2:01 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 599  View(s)
Rate this:

I'm trying to access text from a ButtonField(databound), but I can't get the text if I refer to it as a TableCell. What can I do to get its text?

<asp:GridView ID="gvClass" runat="server" CssClass="grid" HeaderStyle-Font-Bold="true" Width="100%" OnSelectedIndexChanged="gvClass_SelectedIndexChanged" DataKeyNames="ClassID" AutoGenerateColumns="False" OnPageIndexChanging="gvClass_PageIndexChanging">

    <HeaderStyle Font-Bold="True" />

    <Columns>

        <asp:ButtonField DataTextField="ClassID" HeaderText="Class ID" CommandName="Select" />

        <asp:BoundField DataField="CourseName" HeaderText="Course Name" />

        <asp:BoundField DataField="WarehouseName" DataFormatString="Warehouse" HeaderText="Warehouse" />

        <asp:BoundField DataField="TrainerNames" HeaderText="Trainer(s)" />

        <asp:BoundField DataField="StartTime" HeaderText="Start Time" />

        <asp:BoundField DataField="Duration" HeaderText="Duration" />

        <asp:BoundField DataField="Course Category" HeaderText="Course Category" />

        <asp:BoundField DataField="Comment" HeaderText="Comment" />

    </Columns>

    <EmptyDataTemplate>

        <span class="italic grey">No Record Found</span>

    </EmptyDataTemplate>

</asp:GridView>

Code-behind:

foreach (GridViewRow gvr in gvClass.Rows)

{

    foreach (TableCell tc in gvr.Cells)

    {

        stringToAppend = tc.Text;

        sb.Append(PRTL_UtilityPackage.FormatHtmlCharacters(stringToAppend) + " \t");

    }

    sb.Append("\\n");

}

The foreach (TableCell tc in gvr.Cells) comes up with an empty string when looking at the ButtonField.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-26-2014 2:50 AM

Hi Ashish, 

You can still use the tablecell method:

protected void myGridView_DataBound(object sender, GridViewRowEventArgs e)

{

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

        // You may have to play with the index of the control

        // I have found that often a GridView will place a

        // Literal control as Control[0] and the LinkButton

        // as Control[1]. Once you find the index, it won't

        // change.

        LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[1];

        string text = btn.Text;

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick