我使用ASP.NET Repeater动态生成HTML代码和分页。我在两个aspx页面上使用完全相同的代码,并且在一个上工作正常,但在另一个上我得到一个异常:对象引用未设置为对象的实例。
现在我理解为什么发生异常,它只是一个空值,转发器没有渲染我猜。但有没有人知道为什么会发生这种错误,以及它如何在一个页面上运行,并且不能在另一个页面上工作?我应该在哪里寻找"真实"问题
这是我在aspx页面上的HTML代码:
<div class="accordion-wrap projects">
<asp:Repeater runat="server" ID="projectRepeter">
<ItemTemplate>
<div class="item">
<div class="heading">
<span>
<asp:Label runat="server" ID="TeamMemberName" Text='<%#Eval("ProjectName")%>'></asp:Label></span><span><em>x</em></span>
<i>+</i>
</div>
<div class="details">
<ul class="form">
<li>
<label>Project name:</label>
<input type="text" class="in-text" value="<%#Eval("ProjectName")%>" />
</li>
<li>
<label>Lead:</label>
<asp:DropDownList runat="server" DataSourceID="TeamMemberNameDS"></asp:DropDownList>
</li>
</ul>
<ul class="form">
<li>
<label>Description:</label>
<input type="text" class="in-text" value="<%#Eval("Description")%>" />
</li>
</ul>
<ul class="form last">
<li>
<label>Customer:</label>
<asp:DropDownList runat="server" DataSourceID="ClientNameDS"></asp:DropDownList>
</li>
<li class="inline">
<label>Status:</label>
<span class="radio">
<label for="inactive">Active:</label>
<input type="radio" value="1" name="status" id="inactive" />
</span>
<span class="radio">
<label for="active">Inactive:</label>
<input type="radio" value="2" name="status" id="active" />
</span>
<span class="radio">
<label for="active">Archive:</label>
<input type="radio" value="3" name="status" id="Radio1" />
</span>
</li>
</ul>
<div class="buttons">
<div class="inner">
<a href="javascript:;" class="btn green">Save</a>
<a href="javascript:;" class="btn red">Delete</a>
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<div class="pagination">
<asp:Repeater ID="rptPagingProject" runat="server" OnItemCommand="rptPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="btnPage"
CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="Black" Font-Bold="True"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
这就是背后的代码:
protected void BindRepeater()
{
string ConnectionString = "Data Source=PRACTICE-001;Initial Catalog=n.mosorinski;User ID=n.mosorinski;Password=n.mosorinski;MultipleActiveResultSets=True;Application Name=EntityFramework";
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT p.ProjectName, p.Description, c.ClientName FROM Project AS p INNER JOIN Client AS c ON p.CustomerID = c.ClientID", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
PagedDataSource pgitems = new PagedDataSource();
DataView dv = new DataView(dt);
pgitems.DataSource = dv;
pgitems.AllowPaging = true;
pgitems.PageSize = 5;
pgitems.CurrentPageIndex = PageNumber;
if (pgitems.PageCount > 1)
{
rptPagingProject.Visible = true;
ArrayList pages = new ArrayList();
for (int i = 0; i < pgitems.PageCount; i++)
pages.Add((i + 1).ToString());
rptPagingProject.DataSource = pages;
rptPagingProject.DataBind();
}
else
{
rptPagingProject.Visible = false;
}
projectRepeter.DataSource = pgitems;
projectRepeter.DataBind();
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
BindRepeater();
}
}