Trouble with 'set' accessor

  • Thread starter Thread starter Mike Irwin
  • Start date Start date
M

Mike Irwin

I'm having a little difficulty with the set accessor. I'm working on a
template-based site design and trying to use the set accessor to set the
document's title. The problem is that it's not setting the title and I
can't seem to figure out what I'm missing. Here's some sample code:

---
ControlBase.cs
---
public class ControlBase : System.Web.UI.UserControl
{
private string _strTitle;

public string Title
{
get
{
return _strTitle;
}

set
{
_strTitle = value;
}
}
}

---
Default.aspx
---
<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false"
Inherits="namespace.PageBase" %>
....
<title><asp:literal id="litTitle" runat="server"></asp:literal></title>

---
Default.aspx.cs
---
public class PageBase : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal litTitle;
private ControlBase _ctlContent;

private void Page_PreRender(object sender, System.EventArgs e)
{
if (_ctlContent.Title != string.Empty)
{
this.litTitle.Text = _ctlContent.Title;
}
}
}

---
Default.ascx.cs
---
public class Default : ControlBase
{
private void Page_Load(object sender, System.EventArgs e)
{
this.Title = "This should be the title of the document";
}
}
 
Mike Irwin said:
I'm having a little difficulty with the set accessor. I'm working on a
template-based site design and trying to use the set accessor to set the
document's title. The problem is that it's not setting the title and I
can't seem to figure out what I'm missing. Here's some sample code:

---
ControlBase.cs
---
public class ControlBase : System.Web.UI.UserControl
{
private string _strTitle;

public string Title
{
get
{
return _strTitle;
}

set
{
_strTitle = value;
}
}
}

---
Default.aspx
---
<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false"
Inherits="namespace.PageBase" %>
...
<title><asp:literal id="litTitle" runat="server"></asp:literal></title>

---
Default.aspx.cs
---
public class PageBase : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal litTitle;
private ControlBase _ctlContent;

private void Page_PreRender(object sender, System.EventArgs e)
{
if (_ctlContent.Title != string.Empty)
{
this.litTitle.Text = _ctlContent.Title;
}
}
}

---
Default.ascx.cs
---
public class Default : ControlBase
{
private void Page_Load(object sender, System.EventArgs e)
{
this.Title = "This should be the title of the document";
}
}

The _ctlContent in your Default.asPx is a different instance of ControlBase
than the base-class of Default.asCx.
That is why you don't see a change when you set a value in one instance and
try to read in in the other.
You need to set the variable _ctlContent to the Default.ascx control.

Hans Kesting
 
Hans Kesting said:
The _ctlContent in your Default.asPx is a different instance of ControlBase
than the base-class of Default.asCx.
That is why you don't see a change when you set a value in one instance and
try to read in in the other.
You need to set the variable _ctlContent to the Default.ascx control.

Hans Kesting

Thank you for your reply, Hans. I understand what you mean, but I'm not
sure how to set _ctlContent to the Default.ascx control.

Default.asPx is the template from which the other files are derived. The
content for each file is contained in a *.asCx file.

Maybe I'm misunderstanding you, but I would appreciate a little further
explanation as I'm fairly new to C#. Thanks again.
 
Mike Irwin said:
Thank you for your reply, Hans. I understand what you mean, but I'm not
sure how to set _ctlContent to the Default.ascx control.

Default.asPx is the template from which the other files are derived. The
content for each file is contained in a *.asCx file.

Maybe I'm misunderstanding you, but I would appreciate a little further
explanation as I'm fairly new to C#. Thanks again.

Nevermind this message as I've figured out the issue. All that I needed to
do was add an EventHandler for Page_PreRender.

Anyway, thanks for your help.
 
Back
Top