-1

I am getting the error above, I really am stuck at the moment, it is a plain form that I am trying to add a control dynamically when I click a button. I have highligted in a comment on the code where I am getting the error "pnltxtProductDescription.Controls.Add(txt) ' THIS IS WHERE I AM GETTING THE ERROR"

My ASP Page

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Pages/master-pages/main.Master" CodeBehind="quotation-new.aspx.vb" Inherits="iconHub.quotation_new1" %>

<title>New Quotation | iconHub</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

<div id="wrapper" class="wrapper">
    <!-- MAIN CONTENT WRAPPER -->
    <div id="main-content-wrapper" class="content-wrapper ">

            <div class="main-content">

                        <div class="col-md-12" style="padding-bottom: 15px;">
                            <div class="row">
                                <table class="table table-bordered">

                                    <thead>
                                        <tr>
                                            <th class="col-md-1">#</th>
                                            <th class="col-md-2">Item</th>
                                            <th class="col-md-4">Description</th>
                                            <th class="col-md-2">Unit Cost</th>
                                            <th class="col-md-1">Quantity</th>
                                            <th class="col-md-2">Line Total</th>

                                        </tr>
                                    </thead>

                                </table>
                            </div>
                                <div class="col-md-4">
                                    <!--Left Column-->
                                    <div class="form-group">
                                        <input type="text" class="form-control" name="txtProductDescription" runat="server" id="txtProductDescription">
                                    </div>
                                </div>

                            </div>
                            <div class="row">

                                <asp:Panel ID="pnltxtProductDescription" runat="server">
                                </asp:Panel>                                      
                            </div>
                        </div>
                        <div class="col-md-12 pull-right" style="padding-bottom: 15px;">
                            <div class="row">
                                <asp:Button id="cmdAddLine" CssClass="btn btn-default nextBtn btn-sm pull-right" runat="server" onclick="AddTextBox" Text="Add Line" />
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

My Behind Page Code

Public Class quotation_new1 Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub AddTextBox(sender As Object, e As EventArgs)
    Dim index As Integer
    'Description
    index = pnltxtProductDescription.Controls.OfType(Of TextBox)().ToList().Count + 1
    CreatetxtProductDescription("txtProductDescription" & index)


End Sub

Private Sub CreatetxtProductDescription(id As String)
    Dim txt As New TextBox
    txt.ID = id

    pnltxtProductDescription.Controls.Add(txt) ' THIS IS WHERE I AM GETTING THE ERROR

    Dim lt As New Literal With {
        .Text = "<br />"
    }
    pnltxtProductDescription.Controls.Add(lt)
End Sub




Protected Sub Page_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
    Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtProductDescription")).ToList()
    Dim i As Integer = 1
    For Each key As String In keys
        CreatetxtProductDescription("txtProductDescription" & i)
        i += 1
    Next
End Sub

End Class

Hackerman
  • 12,139
  • 2
  • 34
  • 45
Terence
  • 59
  • 2
  • 9
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD Mar 26 '18 at 20:49

1 Answers1

2

The reason it's failing is because you are calling CreatetxtProductDescription() from your PreInit event. PreInit fires before controls are initialized, including your Panel control (See the ASP.Net Page Life Cycle). Since the Panel control hasn't been created yet, it's throwing a NullException error when you try to access the .Controls property.

To fix your issue, move your code that's in the PreInit event to the Load event instead.

Icemanind
  • 47,519
  • 50
  • 171
  • 296