I designed a user control. It contains a public property \"CurrentValue\". When I try to initialize the property using an Eval expression, a null value is assigned.
The following code works for me. When do you use _CurrentValue
field?
UserControl
public partial class Test1 : System.Web.UI.UserControl
{
public string CurrentValue
{
get { return (string)ViewState["CurrentValue"] ?? string.Empty; }
set { ViewState["CurrentValue"] = value; }
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
writer.Write(this.CurrentValue);
}
}
Page
protected void Page_Load(object sender, EventArgs e)
{
var ds = new[]
{
new { FirstName = "F1", LastName = "L1" },
new { FirstName = "F2", LastName = "L2" },
};
DataList1.DataSource = ds;
DataList1.DataBind();
}
Html Markup