Comment puis-je accéder à un texte ButtonFields, si elle est pas considéré comme un TableCell?

StackOverflow https://stackoverflow.com/questions/4760465

Question

Je suis en train de texte d'accès d'un ButtonField (databound), mais je ne peux pas obtenir le texte si je me réfère à elle comme un TableCell. Que puis-je faire pour obtenir le texte?

Markup:

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

Le foreach (TableCell tc en gvr.Cells) vient avec une chaîne vide en regardant le ButtonField.

Était-ce utile?

La solution

Vous pouvez toujours utiliser la méthode TableCell:

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

Autres conseils

Dim lbtn As Button = DirectCast(gwvFileList.Rows(e.CommandArgument).Cells(1).Controls(0), Button)
Dim sName As String = lbtn.Text

Exemple: http://www.editingmate.com

Dans le ButtonField, il y a un autre contrôle dans la cellule que vous avez besoin pour obtenir le texte:

Control control = gvr.Cells[0].Controls[0];
string text = string.Empty;
if (((ButtonField)gvClass.Columns[0]).ButtonType ==
    ButtonType.Link)
{
    LinkButton btn = control as LinkButton;
    text = btn.Text;
}
else
{
    Button btn = control as Button;
    text = btn.Text;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top