FindControl抛出NullReferenceException

时间:2015-04-23 19:57:25

标签: asp.net vb.net

在我的ASP gridview中,我有以下内容(已更新以显示完整的gridview):

Sub TPAnnuity_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles TPAnnuity_GridView.RowCommand
    If e.CommandName = "Cancel" Then
        'Reset Footer Row input fields
        CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedIndex = 0

    ElseIf e.CommandName = "Insert" Then
        TPAnnuity_SqlDataSource.InsertParameters.Clear()
        Dim test1 As New Parameter("CompanyIDInt", TypeCode.Int32)
        test1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("NewCompanyID"), DropDownList).SelectedValue
        TPAnnuity_SqlDataSource.InsertParameters.Add(test1)
        TPAnnuity_SqlDataSource.Insert()

    ElseIf e.CommandName = "Update" Then
        TPAnnuity_SqlDataSource.UpdateParameters.Clear()
        Dim param1 As New Parameter("CompanyIDInt", TypeCode.Int32)
        param1.DefaultValue = CType(TPAnnuity_GridView.FooterRow.FindControl("EditACompanyID"), DropDownList).SelectedValue ****THIS IS THE PROBLEM LINE****
        TPAnnuity_SqlDataSource.UpdateParameters.Add(param1)        
        TPAnnuity_SqlDataSource.Update()
    End If
End Sub

在我的后端,我有以下内容:

Cancel

Insertparam1.Default Value =函数工作得很好,从页脚操作。每次点击“更新”按钮,我都会在$_POST[url]=str_replace(str_split('\\/:*?"<>|.,;'), '', $_POST[title]); $_POST[url]=str_replace(" ","-",$id.'-'.$_POST[title].'.html'); 行上收到NullReferenceException。

任何人都可以帮我弄清楚这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

您的行应如下所示:

param1.DefaultValue = CType(e.CommandSource.FindControl("EditACompanyID"), DropDownList).SelectedValue

您必须使用源行,而不是使用FooterRow。由于您使用e传递了它,因此可以使用此功能。