7

I got a web user control where I have controls that needs to be fed with some data from variables or properties from the underlying page.

<%@ Control Language="C#" AutoEventWireup="False" CodeFile="Header.ascx.cs" Inherits="Site.UserControls.Base.Header" %>
<asp:Literal runat="server" Text='<%# Testing %>' id="ltrTesting" />

Codebehind

namespace Site.UserControls.Base
{
    public partial class Header : UserControlBase
    {
        public string Testing = "hello world!";

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.DataBind(); // Does not work
            //PageBase.DataBind(); // Does not work
            //base.DataBind(); // Does not work
            //Page.DataBind(); // Does not work
        }
    }
}

I did read this topic, but it wont solve my problem, I assume it's because this is a user control and not a page. I want to get property value from code behind

Community
  • 1
  • 1
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • Why not set the value to the controls from code behind instead? – Magnus Jan 16 '12 at 16:51
  • @Magnus Because of portability of the controls, that is of course an option but it's the last option out of this – Eric Herlitz Jan 16 '12 at 16:56
  • The sample above is a simplification of the actual code. The actual usage is that I populate a custom webcontrol with data by sending an entire object to the control. The user controls is standardized in their design and will usually only contain the empty PageLoad method and the object that is to be sent to the webcontrol. Now portability might be the wrong word but due to different reasons we'd prefer to keep it this way, if possible. If not we will go with the code-behind style instead. – Eric Herlitz Jan 16 '12 at 17:05

4 Answers4

14

Solved this, solution below

Since I used a web user control in this case the usual scenarios would not work. But by putting a databind in the page that controls the user control, or any materpage in the chain above the web user control the code started to work

MasterPage codebehind

public partial class MasterPages_MyTopMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Databind this to ensure user controls will behave
        this.DataBind();
    }
}

Ascx file, all suggested solutions below works

<%@ Control Language="C#" AutoEventWireup="False" CodeFile="Header.ascx.cs" Inherits="Site.UserControls.Base.Header" %>
1: <asp:Literal runat="server" Text='<%# DataBinder.GetPropertyValue(this, "Testing") %>' />
2: <asp:Literal runat="server" Text='<%# DataBinder.Eval(this, "Testing") %>' />
3: <asp:Literal runat="server" Text='<%# Testing2 %>' />

Codebehind of ascx

namespace Site.UserControls.Base
{
    public partial class Header : UserControlBase //UserControl
    {
        public string Testing { get { return "hello world!"; } }
        public string Testing2 = "hello world!";

        protected void Page_Load(object sender, EventArgs e)
        { }
    }
}

Thanks for the inspiration!

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
7

You can't usually put scriplets in server controls. But there's an easy workaround: use a plain html control:

<span id="ltrTesting"><%= this.Testing %></span>
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Thats a simple solution but if I let's say want to use a bit more advanced controls like a custom webcontrol that only renders if the value assigned is not null that wont do – Eric Herlitz Jan 16 '12 at 16:57
  • 1
    A web control that only renders if a certain value is assigned? That's a whole separate set of issues – Adam Rackis Jan 16 '12 at 16:59
  • Adam, that one is already covered and working. If we assign a value to the webcontrol from codebehind everything works fine. But why o why wont it work when accessing the variable values from the aspx page. – Eric Herlitz Jan 16 '12 at 17:07
  • @Trikks - you can't run scriplets in server controls. That's probably why it's not working. – Adam Rackis Jan 16 '12 at 17:09
  • Adam, have a look at my own answer, it solves the issue. And my tests to further extend this beyond literals or ASP:controls have been successful. It's awesome :) – Eric Herlitz Jan 16 '12 at 20:46
1

Or you could set the Text property of the Literal in the code behind:

ltrTesting.Text = "Hello World!";
Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
0

try making Testing be a property rather than a field:

e.g.

public string Testing
{
    get { return "Hello World!"; }
}
CD Jorgensen
  • 1,361
  • 9
  • 8