1

ASP.NETページに次のコントロールがあります。

<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>

背後にあるコード:

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();
    }

また、このページには、ログインフォームとしてのユーザーコントロールがあります。

<%@ 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>

そして背後にあるコード:

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("./");
            }
        }
    }

しかし、ログインボタンを押した後、私のページに次のエラーがあります:

null参照でランタイムバインディングを実行できません説明:現在のWeb要求の実行中に未処理の例外が発生しました。エラーとそれがコードのどこで発生したかについての詳細は、スタックトレースを確認してください。

{

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";

そして、私はここで何が問題なのか理解していません。この例外は、ログインボタンを押した後にのみ発生します(PostBackの後で理解します)

4

1 に答える 1

1

この プロパティは、リピーターDataItemを呼び出すときにのみ設定されます。DataBind()ポストバック後、DataItemは存在しなくなります。

データバインディングを適用するときに発生するItem_Createdすべてのリクエストで発生するを置き換える必要があります。Item_Databound

于 2013-01-21T14:17:28.960 に答える