简体   繁体   中英

ASP.NET Cannot perform runtime binding on a null reference - after I'm try to login in my website

I have next controls in my ASP.NET page:

<asp:Content ID="headerPanelContent" ContentPlaceHolderID="mainContent" runat="server">
    <asp:Repeater ID="rptHomePage" runat="server" DataSourceID="dsHomePage" OnItemCreated="rptHomePage_ItemCreated">
        <ItemTemplate>
            <div class="content-forum-section">
                <table class="forum-table-view">
                    <tr class="content-forum-name f-background">
                        <td colspan="3">
                            <h2 class="t-color-white"><%# Eval("forumName") %></h2>
                            <asp:HiddenField ID="hdnForumID" Value='<%# Eval("forumID") %>' runat="server" />
                        </td>
                    </tr>
                    <tr class="f-background t-color-white">
                        <td style="width: 85%;">Section
                        </td>
                        <td style="width: 9%;">Themes
                        </td>
                        <td style="width: 9%;">Messages
                        </td>
                    </tr>
                    <asp:Repeater ID="rptSections" runat="server" DataSourceID="dsSectionsInForum" OnItemCreated="rptSections_ItemCreated">
                        <ItemTemplate>
                            <tr class="lightgrey-background">
                                <td>
                                    <div class="forum-section-container">
                                        <a href='<%# "./Section.aspx?id=" + Eval("SectionId").ToString() %>'><%#Eval("Name") %></a>
                                        <br />
                                        <asp:Repeater ID="rptSubsections" runat="server" DataSourceID="dsSubsectionsInSection">
                                            <ItemTemplate>
                                                <div class="subsection-link">
                                                    <a href='<%# "./Subsection.aspx?id=" + Eval("SubsectionId").ToString() %>'><%# Eval("Name") %></a>
                                                </div>
                                            </ItemTemplate>
                                        </asp:Repeater>
                                        <asp:LinqDataSource ID="dsSubsectionsInSection" runat="server">
                                        </asp:LinqDataSource>
                                    </div>
                                </td>
                                <td>0
                                </td>
                                <td>0
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                    <asp:LinqDataSource ID="dsSectionsInForum" runat="server"></asp:LinqDataSource>
                </table>
            </div>
        </ItemTemplate>
    </asp:Repeater>
    <asp:LinqDataSource ID="dsHomePage" runat="server"
        ContextTypeName="PWO_Projekt.ForumDBDataContext"
        Select="new(Id as forumID, Name as forumName)"
        TableName="Forums">
    </asp:LinqDataSource>
</asp:Content>

Code behind:

protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void rptHomePage_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        var forumDetails = (dynamic)e.Item.DataItem;
        int forumID = forumDetails.forumID;
        LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");
        lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
        lds.TableName = "Sections";
        lds.Where = "ForumId == @id";
        lds.WhereParameters.Add("id", DbType.Int32, forumID.ToString());
        lds.DataBind();
    }

    protected void rptSections_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        var sectionDetails = (dynamic)e.Item.DataItem;
        int sectionID = sectionDetails.SectionId;
        LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSubsectionsInSection");
        lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
        lds.TableName = "Subsections";
        lds.Where = "SectionId == @id";
        lds.WhereParameters.Add("id", DbType.Int32, sectionID.ToString());
        lds.DataBind();
    }

Also I have on this page my user control as login form:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.ascx.cs" Inherits="PWO_Projekt.Controls.LoginForm" %>
<form>
<table>
    <tr>
        <td>Login
        </td>
        <td>
            <asp:TextBox ID="txtLogin" runat="server" CssClass="smallfont" Columns="15"></asp:TextBox>
        </td>
        <td>
            <asp:CheckBox ID="chRemeber" runat="server" />
            Remember me
        </td>
    </tr>
    <tr>
        <td>Password
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="smallfont" Columns="15"></asp:TextBox>
        </td>
        <td>
            <asp:Button ID="btnLogin" runat="server" Text="Log in" OnClick="btnLogin_Click" />
        </td>
    </tr>
    <tr>
        <td colspan="3">
            <asp:Label ID="lblError" runat="server" Text="" ForeColor="Red"></asp:Label>
        </td>
    </tr>
</table>

And code behind:

protected void btnLogin_Click(object sender, EventArgs e)
    {
        string login = txtLogin.Text.Trim();
        string password = CommonFunctions.getMd5Hash(txtPassword.Text.Trim());
        using (ForumDBDataContext db = new ForumDBDataContext())
        {
            db.Connection.ConnectionString = CommonFunctions.getConnectionString();
            var user =
                from u in db.Users
                where (u.Login == login) && (u.Password == password)
                select u;
            if (user.Count() == 1)
            {
                Session["UserLogin"] = login;
                Response.Redirect("./");
            }
        }
    }

But after pressing Login button I have next error on my page:

Cannot perform runtime binding on a null reference 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.

{

var forumDetails = (dynamic)e.Item.DataItem;

int forumID = forumDetails.forumID; //error is here

LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");

lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";

And I don't understand what the problem here. This exception rises only after pressing Login button (As I understand after PostBack)

The DataItem property is set only when you are calling DataBind() on the repeater. After a postback, the DataItem is no more present.

You should replace Item_Created , which is fired on all requests by Item_Databound which is fired when applying the databinding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM