ASP.NET User Control: cannot initialize user control property with Eval ("...")

I created a user control. It contains the public property "CurrentValue". When I try to initialize a property using an Eval expression, a value of zero is assigned.

// In the code below, label assignment is OK, assigning a RatingNull control gets zero

<asp:TemplateField> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Difficulty") %>' <uc1:RatingNull ID="RatingNull1" runat="server" CurrentValue='<%# Eval("Difficulty") %>' /> </ItemTemplate> 

If I directly assigned a value (that is, CurrentValue = "5"), user control is working fine.

 public partial class RatingNull : System.Web.UI.UserControl { private string _CurrentValue; public string CurrentValue { get { return _CurrentValue; } set { _CurrentValue = value; } } (...) } 
+4
source share
1 answer

The following code works for me. When do you use the _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

 <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <uc1:Test ID="Test1" runat="server" CurrentValue='<%# Eval("FirstName") %>' /> </ItemTemplate> </asp:DataList> 
0
source

Source: https://habr.com/ru/post/1302110/


All Articles