ASP.NET User Control : can#39;t initialize a user control property using Eval(quot;...quot;)(ASP.NET 用户控件:无法使用 Eval(...) 初始化用户控件属性)
问题描述
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.
// In below code, label assignment is OK, RatingNull user control assignment get null
<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 (ie, CurrentValue="5"), user control works fine.
public partial class RatingNull : System.Web.UI.UserControl
{
private string _CurrentValue;
public string CurrentValue
{
get { return _CurrentValue; }
set { _CurrentValue = value; }
}
(...)
}
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
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<uc1:Test ID="Test1" runat="server" CurrentValue='<%# Eval("FirstName") %>' />
</ItemTemplate>
</asp:DataList>
这篇关于ASP.NET 用户控件:无法使用 Eval("...") 初始化用户控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET 用户控件:无法使用 Eval("...") 初始化用户控件属性
基础教程推荐
- 从 C# 控制相机设备 2022-01-01
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
