Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using asp.net 4.0 code using visual basic in visual studio 2010 with web forms.

When i'm using the page without masterpage, it works. But when i link it a masterpage, it doesn't work, I wonder why??

I'm getting this error:

VB
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 8:                  assControl.BackColor = Drawing.Color.Yellow
Line 9:              Else
Line 10:                 assControl.BackColor = Drawing.Color.White
Line 11:             End If
Line 12:         Next

Source File: C:\Users\Muntansir\Desktop\CheckMasterpage\FormFieldsWithMasterPage.aspx.vb    Line: 10 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   FormFieldsWithMasterPage.Page_PreRender(Object sender, EventArgs e) in C:\Users\Muntansir\Desktop\CheckMasterpage\FormFieldsWithMasterPage.aspx.vb:10
   System.Web.UI.Control.OnPreRender(EventArgs e) +92
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974



Below is the code:

XML
Code with master page:
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="FormFieldsWithMasterPage.aspx.vb" Inherits="FormFieldsWithMasterPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div>
    <asp:Label
id="lblFirstName"
Text="First Name"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
ControlToValidate="txtFirstName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastname"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
ControlToValidate="txtLastName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
</asp:Content>

Code-behind with master page:
Partial Class FormFieldsWithMasterPage
    Inherits System.Web.UI.Page
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        For Each valControl As BaseValidator In Page.Validators
            Dim assControl As WebControl = Page.FindControl(valControl.ControlToValidate)
            If Not valControl.IsValid Then
                assControl.BackColor = Drawing.Color.Yellow
            Else
                assControl.BackColor = Drawing.Color.White
            End If
        Next
    End Sub
End Class

Code without masterpage:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FormFieldsWithoutMasterpage.aspx.vb" Inherits="FormFieldsWithoutMasterpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label
id="lblFirstName"
Text="First Name"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
ControlToValidate="txtFirstName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastname"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
ControlToValidate="txtLastName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" />
    </div>
    </form>
</body>
</html>

code-behind without masterpage:
Partial Class FormFieldsWithoutMasterpage
    Inherits System.Web.UI.Page

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        For Each valControl As BaseValidator In Page.Validators
            Dim assControl As WebControl = Page.FindControl(valControl.ControlToValidate)
            If Not valControl.IsValid Then
                assControl.BackColor = Drawing.Color.Yellow
            Else
                assControl.BackColor = Drawing.Color.White
            End If
        Next
    End Sub
End Class
Posted
Updated 31-Oct-14 8:51am
v2
Comments
AnvilRanger 31-Oct-14 14:15pm    
Minhaaj

Most people will not download your code from some outside link. This is just way too dangerous. You need to include the section of the code that is generating the exception.

Also from the error it is pretty clear that what ever "assControl" is, it has not been properly initialized. Make sure you review you code and find where the object was defined.
Minhaaj Edoo 31-Oct-14 14:29pm    
Firstly I didn't know that i have to write everything in here, so sorry for that.

Secondly, if the object was not properly initialized, then it would not have work without the masterpage. Review my question once again, I want to know why it doesn't work with the masterpage, that's why i'am asking for someone to explain me?
jkirkerx 31-Oct-14 18:04pm    
for the masterpage example, did you place your code on the masterpage code behind?

To the best of my knowledge, when referencing html or asp.net objects in Content PlaceHolder 2 MainContent, the code goes on the webform, that references the masterpage.

I would have to copy your code into a sample and see what happens with page.findcontrol to see if asscontrol is nothing. If I get time later, I will give it 5 minutes.
Minhaaj Edoo 1-Nov-14 2:06am    
There is no code in the code behind of my masterpage.
only this:

Partial Class Site
Inherits System.Web.UI.MasterPage
End Class

1 solution

You have to find the content place holder first, then use the content placeholder to find the control instead of using page.findcontrol

find the cph.findcontrol in the 2nd code example.

<asp:content id="Content3" contentplaceholderid="MainContent" runat="Server" xmlns:asp="#unknown">
    <div style="width: 350px; text-align: left;">
        <asp:label>
            ID="lblFirstName"
            Text="First Name"
            AssociatedControlID="txtFirstName"
            runat="server" />
    </asp:label></div>
    <div style="width: 350px; text-align: left;">
        <asp:textbox id="txtFirstName" runat="server" />
        <asp:requiredfieldvalidator id="reqFirstName" controltovalidate="txtFirstName" text="(Required)" enableclientscript="false" runat="server" />
       </div>
    <div style="width: 350px; text-align: left;">
        <asp:label id="lblLastName" text="Last Name" associatedcontrolid="txtLastName" runat="server" />
    </div>
    <div style="width: 350px; text-align: left;">
        <asp:textbox id="txtLastname" runat="server" />
        <asp:requiredfieldvalidator>
            ID="reqLastName" ControlToValidate="txtLastName" Text="(Required)" EnableClientScript="false" runat="server" />
    </asp:requiredfieldvalidator></div>
    <div style="width: 350px; text-align: left;">
        <asp:button id="btnSubmit" text="Submit" runat="server" />
    </div>
</asp:content>

Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

        If Page.IsPostBack Then

            'Indicate to the user that the page is not valid

            Dim cphMainContent As ContentPlaceHolder = Me.Master.FindControl("MainContent")

            For Each fieldValidator As RequiredFieldValidator In Page.Validators

                Dim txtControl As TextBox = cphMainContent.FindControl(fieldValidator.ControlToValidate.ToString)

                If Not txtControl Is Nothing Then

                    If Not fieldValidator.IsValid Then
                        txtControl.Style.Remove("border")
                        txtControl.Style.Add("border", "solid 1px rgb(255,0,0)")
                    Else
                        txtControl.Style.Remove("border")
                        txtControl.Style.Add("border", "solid 1px rgb(140,140,140)")
                    End If

                End If
            Next

        End If

    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900