1

我有这个代码:

Dim txt = CType(GridView1.FindControl("cnt_content"), TextBox)
        txt.Attributes.Add("style", "word-wrap:break-word;")

我总是得到那个 txt 是一个没有设置为我的 asp 代码的对象实例的对象:

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" 
    DataKeyNames="cnt_id">
    <Columns>
<asp:TemplateField HeaderText="Content">
                <EditItemTemplate>
                         <asp:TextBox ID="cnt_content" runat="server" Text='<%# Bind("cnt_content") %>' />
                </EditItemTemplate> 
                <ItemTemplate> 
                    <asp:Label ID="lblcnt_content" runat="server" Text='<%# Bind("cnt_content") %>'></asp:Label> 
                </ItemTemplate> 
                <ItemStyle wrap="true" Width="400px" />
            </asp:TemplateField>  

有什么帮助吗?

4

2 回答 2

2

我的猜测是您正试图在RowDataBound事件中找到此控件,如下所示:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

End Sub

您只需要检查数据行上的此控件,而不是页眉或页脚行,因为该控件不会存在于其他类型的行中,请尝试以下操作:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Only check in data rows, ignore header and footer rows
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' Determine if you are in edit mode or not
        If GridView1.EditIndex = -1 Then
            ' Not in edit mode so look for label control defined in ItemTemplate of grid view
            ' Put logic here for label control
        Else
            ' In edit mode so look for textbox control defined in EditItemTemplate of grid view
            Dim txt = CType(GridView1.FindControl("cnt_content"), TextBox)
            txt.Attributes.Add("style", "word-wrap:break-word;")
        End If
    End If
End Sub
于 2013-09-04T13:50:39.700 回答
0

这意味着GridView1.FindControl("cnt_content")返回 null,这可能意味着它GridView直接包含名为"cnt_content".

于 2013-09-04T13:44:15.747 回答