How to create my own header controls?

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

Hi,

a few weeks ago I had started a thread "Can't dynamically add controls
because of <%...%> code blocks". The error mentioned keeps me from inserting
an automatically generated navigation menu at the top of some pages. My
pages contain some <%= %> sections as well as sometimes <%# %> sections. Now
I have two questions:

a) Do <%# %> blocks also generate this error? In that case I couldn't use
the RepeaterControl anymore if I wanted to have my navigation menu.

b) To get rid of the error I want to replace my <%= %> blocks with server
controls. Here's the question: These blocks generate controls. How can I
possibly replace them in the header section by WHAT?

Here's a simplified example:

.aspx:

<%= generateHeader("Test.something") + generateHeader("Foo.something") %>

------

.aspx.cs:

string generateHeader(string filename)
{
return "<link href=\""+filename+"\"";
}

Axel Dahmen
 
<%# %> don't cause the error, because the processing is on the bind event,
its really just a string value.

simple header control:

[DefaultProperty("FileName"),
ToolboxData("<{0}:MyHeader runat=server></{0}:MyHeader >")]
public class MyHeader : System.Web.UI.WebControls.WebControl
{
private string mFileName = "";
public string FileName
{
get {return mFileName;}
set {mFileName = value;}
}
protected override void Render(HtmlTextWriter output)
{
output.Write(string.Format("<link href=\"{0}\">",mFileName));
}
}


-- bruce (sqlwork.com)


| Hi,
|
| a few weeks ago I had started a thread "Can't dynamically add controls
| because of <%...%> code blocks". The error mentioned keeps me from
inserting
| an automatically generated navigation menu at the top of some pages. My
| pages contain some <%= %> sections as well as sometimes <%# %> sections.
Now
| I have two questions:
|
| a) Do <%# %> blocks also generate this error? In that case I couldn't use
| the RepeaterControl anymore if I wanted to have my navigation menu.
|
| b) To get rid of the error I want to replace my <%= %> blocks with server
| controls. Here's the question: These blocks generate controls. How can I
| possibly replace them in the header section by WHAT?
|
| Here's a simplified example:
|
| .aspx:
|
| <%= generateHeader("Test.something") + generateHeader("Foo.something")
%>
|
| ------
|
| .aspx.cs:
|
| string generateHeader(string filename)
| {
| return "<link href=\""+filename+"\"";
| }
|
| Axel Dahmen
|
|
 
You're headed down the right path Axel. To ease the learning curve a bit
you might want to start with User Controls (.ascx files), which give you a
lot of capability without exposing you to too much of the inner plumbing at
this stage.

From a developer's perspective, a User Control is basically a mini webpage
that you embed in a larger webpage. You can reuse it across as many
webpages as you like, and within a single webpage as often as you like.
This makes it ideal for website-specific controls, like a custom header.

There are a lot of good resource on this and if you're using VS.NET, there
should be some solid example projects as well.

/// M
 
Back
Top