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

但是在按下登录按钮后,我的页面上出现了下一个错误:

无法对空引用执行运行时绑定 说明:执行当前 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 回答