ASP.NET User Control : can't initialize a user control property using Eval(“…”)

前端 未结 1 544
情话喂你
情话喂你 2020-12-30 16:08

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.

1条回答
  •  有刺的猬
    2020-12-30 16:54

    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

    
        
            
        
    
    

    0 讨论(0)
提交回复
热议问题