我正在疯狂地解决这个问题,我真的希望有人可以提供帮助!在下面的示例中,我无法获得“4”的值以显示在呈现的页面中。下面的输出编译并执行得很好,但显示的所有三个值都是“0”。
这是一个简短的片段:
(希望我正确地格式化这个)
(来自default.aspx)
<%@ Register Src="Modules/StarRating.ascx" TagName="StarRating" TagPrefix="mytag" %>
(来自StarRating.ascx)
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="StarRating.ascx.cs" Inherits="StarRating" %>
<h1>RATING: <%=OverallRating%></h1>
(来自StarRating.ascx.cs)
public partial class StarRating : System.Web.UI.UserControl
{
private int _OverallRating;
public string OverallRating
{
get { return _OverallRating.ToString(); }
set { _OverallRating = int.Parse(this.OverallRating); }
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("RATING (from behind code!): " + OverallRating);
Response.Write("<BR />RATING (another one): " + _OverallRating);
}
}
答案 0 :(得分:0)
在OverallRating
集访问器中,您正在解析基础整数变量的当前值,而不是传递给属性的值。这应该解决它:
public string OverallRating
{
get { return _OverallRating.ToString(); }
set { _OverallRating = int.Parse(value); }
}
(假设这不仅仅是一个错字)