如何在属性方法VB.NET中使用GridViewUpdateEventArgs

时间:2013-04-23 10:08:04

标签: asp.net vb.net gridview

我想在GridView的页脚上显示下拉列表。

    aspx code:
 <FooterTemplate>                                                      
 <asp:DropDownList ID="ddSrc" runat="server">    
 </asp:DropDownList>                                                   
 </FooterTemplate>



 'VB Code
         Protected Sub gvID_RowDataBound(sender As Object, e As GridViewRowEventArgs)
             If e.Row.RowType = DataControlRowType.DataRow Then
                 If e.Row.DataItem IsNot Nothing Then
                    Dim ddSrc As DropDownList = DirectCast(e.Row.FindControl("ddSrc"), DropDownList)      
                        If ddSrc IsNot Nothing Then
                             ddSrc.DataTextField = "Name"
                             ddSrc.DataValueField = "Id"
                             ddSrc.DataSource = GetData()
                             ddSrc.DataBind()
                        End If
                 End If
             End If
         End Sub

我在上面的代码后面使用了代码来加载我的下拉列表,但问题就像是 在运行期间“ ddSrc.DataTextField =”名称“”行“对象引用未设置为对象的实例”。

我已将我的问题编辑为易于理解。

2 个答案:

答案 0 :(得分:2)

您需要在网格视图的RowDataBound事件上加载下拉列表

Protected Sub gvID_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Footer Then
        If e.Row.DataItem IsNot Nothing Then
           Dim ddSrc As DropDownList = DirectCast(e.Row.FindControl("ddSrc"), DropDownList)      
               If ddSrc IsNot Nothing Then
                    ddSrc.DataTextField = "Name"
                    ddSrc.DataValueField = "Id"
                    ddSrc.DataSource = GetData()
                    ddSrc.DataBind()
               End If
        End If
    End If
End Sub

答案 1 :(得分:1)

在黑暗中拍摄,但这是否意味着它需要声明中的“新”?

Dim e As New GridViewUpdateEventArgs 

如果尚未创建,则无法为其分配变量。