你调用的对象是空的

发布于 2024-10-31 06:43:46 字数 4671 浏览 0 评论 0原文

我正在尝试为 page_load 期间的隐藏字段赋值..但它抛出 System.NullReferenceException..这是我在 page_load 事件中使用的代码..

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

    Dim hLogInfo As HiddenField = DirectCast(FindControl("HLogInfo"), HiddenField)
    Dim hUName As HiddenField = DirectCast(FindControl("HUName"), HiddenField)

    If Not Page.User.Identity.IsAuthenticated Then
        Response.Write("not logged in")
        hLogInfo.Value = "No"
        hUName.Value = "None"
    Else
        Dim currentUser As MembershipUser = Membership.GetUser()
        hUName.Value = currentUser.UserName
        Response.Write(currentUser.UserName)
        hLogInfo.Value = "Yes"
    End If

End Sub

下面给出的是错误详细信息...

 Server Error in '/colorshelf.com' Application.
 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 41:         Else
 Line 42:             Dim currentUser As MembershipUser = Membership.GetUser()
 Line 43:             hUName.Value = currentUser.UserName
 Line 44:             Response.Write(currentUser.UserName)
 Line 45:             hLogInfo.Value = "Yes"


 Source File: G:\Websites\colorshelf.com\html\*******.***.vb    Line: 43

 Stack Trace:

 [NullReferenceException:Object reference not set to an instance of an object.]
 ****_****.Page_Load(Object sender, EventArgs e) in 
 G:\Websites\colorshelf.com\html\******.***.vb:43
 System.Web.UI.Control.OnLoad(EventArgs e) +91
 System.Web.UI.Control.LoadRecursive() +74
 System.Web.UI.Control.LoadRecursive() +146
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
 Boolean includeStagesAfterAsyncPoint) +2207


 Version Information: Microsoft .NET Framework Version:4.0.30319; 
 ASP.NET Version:4.0.30319.1 

也像下面的那样修改代码抛出相同的错误

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

    If Not Page.User.Identity.IsAuthenticated Then
        Response.Write("not logged in")
        HLogInfo.Value = "No"
        HUName.Value = "None"
    Else
        Dim currentUser As MembershipUser = Membership.GetUser()
        HUName.Value = currentUser.UserName
        Response.Write(currentUser.UserName)
        HLogInfo.Value = "Yes"
    End If

End Sub

下面给出的是页面源..

<%@  Master Language="VB" MasterPageFile="../**.master" AutoEventWireup="false" CodeFile="***.master.vb"
Inherits="***_****" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
 <asp:ContentPlaceHolder ID="HeadContentSubMaster" runat="server">
    <script type="text/javascript">

    </script>
 </asp:ContentPlaceHolder>
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
 <div style="width: 100%; float: left;">
    <div style="width: 16%; float: left;" align="center">
        <div style="color: #FFFFFF; font-weight: bold; font-size: 14px; width: 100%; padding-top: 37px;"
            align="center">
            Colorshelf list
            <br />
            <hr width="70%" />
        </div>
        <table border="0" style="border-collapse: separate; padding-top: 7px;">
        </table>
    </div>
</div>
<div style="width: 56%; float: left; margin-left: 20px;">
    <asp:ContentPlaceHolder ID="BodyContent" runat="server">
    </asp:ContentPlaceHolder>
</div>
<div style="width: 22%; float: right;">
    <a href="http://www.lalsofttech.com/MRanking/Default.aspx" style="border-style: none">
        <img src="../Images/MRA_Banner.png" style="border-style: none; width: 92%; padding-left: 30px;
            padding-top: 50px;" /></a>
</div>
</div>
<div style="width: 100%; float: left;">
    <div style="width: 80%; float: left;">
        <asp:ContentPlaceHolder ID="ContentFooter" runat="server">
            <asp:HiddenField ID="HLogInfo" runat="server" Value="" />
            <asp:HiddenField ID="HUName" runat="server" Value="" />
        </asp:ContentPlaceHolder>
    </div>
</div>
<div class="clear">
    <br />
    <br />
</div>

请指导我解决这个问题???

I am trying to assign values to the hiddenfields duting page_load..but its throwing System.NullReferenceException.. This is the code iam using in the page_load event..

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

    Dim hLogInfo As HiddenField = DirectCast(FindControl("HLogInfo"), HiddenField)
    Dim hUName As HiddenField = DirectCast(FindControl("HUName"), HiddenField)

    If Not Page.User.Identity.IsAuthenticated Then
        Response.Write("not logged in")
        hLogInfo.Value = "No"
        hUName.Value = "None"
    Else
        Dim currentUser As MembershipUser = Membership.GetUser()
        hUName.Value = currentUser.UserName
        Response.Write(currentUser.UserName)
        hLogInfo.Value = "Yes"
    End If

End Sub

Given below is the error details...

 Server Error in '/colorshelf.com' Application.
 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 41:         Else
 Line 42:             Dim currentUser As MembershipUser = Membership.GetUser()
 Line 43:             hUName.Value = currentUser.UserName
 Line 44:             Response.Write(currentUser.UserName)
 Line 45:             hLogInfo.Value = "Yes"


 Source File: G:\Websites\colorshelf.com\html\*******.***.vb    Line: 43

 Stack Trace:

 [NullReferenceException:Object reference not set to an instance of an object.]
 ****_****.Page_Load(Object sender, EventArgs e) in 
 G:\Websites\colorshelf.com\html\******.***.vb:43
 System.Web.UI.Control.OnLoad(EventArgs e) +91
 System.Web.UI.Control.LoadRecursive() +74
 System.Web.UI.Control.LoadRecursive() +146
 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, 
 Boolean includeStagesAfterAsyncPoint) +2207


 Version Information: Microsoft .NET Framework Version:4.0.30319; 
 ASP.NET Version:4.0.30319.1 

Modifying the code like the one bewlow too throws the same error

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

    If Not Page.User.Identity.IsAuthenticated Then
        Response.Write("not logged in")
        HLogInfo.Value = "No"
        HUName.Value = "None"
    Else
        Dim currentUser As MembershipUser = Membership.GetUser()
        HUName.Value = currentUser.UserName
        Response.Write(currentUser.UserName)
        HLogInfo.Value = "Yes"
    End If

End Sub

Given below is the page source..

<%@  Master Language="VB" MasterPageFile="../**.master" AutoEventWireup="false" CodeFile="***.master.vb"
Inherits="***_****" %>
 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
 <asp:ContentPlaceHolder ID="HeadContentSubMaster" runat="server">
    <script type="text/javascript">

    </script>
 </asp:ContentPlaceHolder>
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
 <div style="width: 100%; float: left;">
    <div style="width: 16%; float: left;" align="center">
        <div style="color: #FFFFFF; font-weight: bold; font-size: 14px; width: 100%; padding-top: 37px;"
            align="center">
            Colorshelf list
            <br />
            <hr width="70%" />
        </div>
        <table border="0" style="border-collapse: separate; padding-top: 7px;">
        </table>
    </div>
</div>
<div style="width: 56%; float: left; margin-left: 20px;">
    <asp:ContentPlaceHolder ID="BodyContent" runat="server">
    </asp:ContentPlaceHolder>
</div>
<div style="width: 22%; float: right;">
    <a href="http://www.lalsofttech.com/MRanking/Default.aspx" style="border-style: none">
        <img src="../Images/MRA_Banner.png" style="border-style: none; width: 92%; padding-left: 30px;
            padding-top: 50px;" /></a>
</div>
</div>
<div style="width: 100%; float: left;">
    <div style="width: 80%; float: left;">
        <asp:ContentPlaceHolder ID="ContentFooter" runat="server">
            <asp:HiddenField ID="HLogInfo" runat="server" Value="" />
            <asp:HiddenField ID="HUName" runat="server" Value="" />
        </asp:ContentPlaceHolder>
    </div>
</div>
<div class="clear">
    <br />
    <br />
</div>

Please guide me to solve this issue???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星軌x 2024-11-07 06:43:46

如此简单,但我让它变得更糟..
看看我放置隐藏字段的位置,

<div style="width: 100%; float: left;">
<div style="width: 80%; float: left;">
    <asp:ContentPlaceHolder ID="ContentFooter" runat="server">
        <asp:HiddenField ID="HLogInfo" runat="server" Value="" />
        <asp:HiddenField ID="HUName" runat="server" Value="" />
    </asp:ContentPlaceHolder>
</div>
</div>

我应该将这些隐藏字段放置在 ContentPlaceHolder 之外,因为该部分用于子页面。

像这样更正代码后。一切正常。

<div style="width: 100%; float: left;">
<div style="width: 80%; float: left;">
<asp:HiddenField ID="HLogInfo" runat="server" Value="" />
<asp:HiddenField ID="HUName" runat="server" Value="" />
    <asp:ContentPlaceHolder ID="ContentFooter" runat="server">

    </asp:ContentPlaceHolder>
</div>
</div>

感谢 Muhammad Akhta 花费宝贵的时间提供帮助我从这个混乱中..

So simple but I made it worse ..
See where I have placed the hidden fields

<div style="width: 100%; float: left;">
<div style="width: 80%; float: left;">
    <asp:ContentPlaceHolder ID="ContentFooter" runat="server">
        <asp:HiddenField ID="HLogInfo" runat="server" Value="" />
        <asp:HiddenField ID="HUName" runat="server" Value="" />
    </asp:ContentPlaceHolder>
</div>
</div>

I should have place those hidden fields outside the ContentPlaceHolder as that portion is for the child pages..

After correcting the code like this..everything worked fine..

<div style="width: 100%; float: left;">
<div style="width: 80%; float: left;">
<asp:HiddenField ID="HLogInfo" runat="server" Value="" />
<asp:HiddenField ID="HUName" runat="server" Value="" />
    <asp:ContentPlaceHolder ID="ContentFooter" runat="server">

    </asp:ContentPlaceHolder>
</div>
</div>

Thanks Muhammad Akhta for spending your valuable time helping me from this mess..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文