pass dynamic variable to user controls

H

Howard

I have a header control, control.ascx page and a user.aspx page
I would like to generate a unique page title for each user.

in user.aspx i used this
<Controls:Header id="header" runat="server" title="User Profile -
<%=userName%>" />

but it doesn't allow me to do that. How should I modify the contro.ascx
so that it accepts variables? or any other way to do this?

Thanks,

Howard
 
S

Siva M

Hi,

Have a Label control in your .ascx file to serve as the page title. Then,
expose a property named Title in the .ascx code-behind that will essentially
set/get the label's Text property.

HTH.

I have a header control, control.ascx page and a user.aspx page
I would like to generate a unique page title for each user.

in user.aspx i used this
<Controls:Header id="header" runat="server" title="User Profile -
<%=userName%>" />

but it doesn't allow me to do that. How should I modify the contro.ascx
so that it accepts variables? or any other way to do this?

Thanks,

Howard
 
H

Howard

That's not going to work because <title> is in the <head> tag I can't
use a label control.
How would you set a property value of something in an .ascx in an aspx
file dynamically?
could you give me an example?

Howard
 
S

Siva M

Ok, didn't realize it's about page title.

Have runat=server & id attributes for the <title> tag and declare it as a
HtmlGenericControl in the code-behind. Now you can set its content using the
InnterText property.

I assume your .ascx control emits all the HTML header details as
<html><head><title id="title" runat=server></title></head>

In the .ascx.cs file:

public string Title
{
get {return title.InnerText;}
set {title.InnerText = value;}
}

In the containing .aspx.cs file:
<user control id>.Title = "Your custom title goes here...";

Or in the .aspx file:
<ascxControl runat="server" id="myControl" Title="Your custom title goes
here..." />

Hope this helps?

That's not going to work because <title> is in the <head> tag I can't
use a label control.
How would you set a property value of something in an .ascx in an aspx
file dynamically?
could you give me an example?

Howard
 
H

Howard

<ascxControl runat="server" id="myControl" Title="Your custom title
goes here..." />
But the Title propergy in this is still hardcoded. My title value is
pull directly from a database in the .aspx page
pseudo code:
//
uId = request.querystring("id")
select userName where id = uid
userName = results from db
<Controls:Header id="header" runat="server" title="User Profile
<%=userName%>" />
//

Thanks for your help
 
S

Siva M

Hi,

As I had indicated, you can set the value from the code-behind also (after
fetching the title from the db):

In the page_load perhaps,

// Code to fetch title from the db
.... ...
header.Title = "title using db value";

Or as you have already indicated, you can use the <%= %> syntax also as in:

In .aspx.cs

protected string userName;
....
// Code to fetch title from the db
.... ...
userName = "User profile " + "username from db";

In .aspx
<Controls:Header id="header" runat="server" title="<%=userName%>" />

I prefer the first approach.

Hope this helps.

<ascxControl runat="server" id="myControl" Title="Your custom title
goes here..." />
But the Title propergy in this is still hardcoded. My title value is
pull directly from a database in the .aspx page
pseudo code:
//
uId = request.querystring("id")
select userName where id = uid
userName = results from db
<Controls:Header id="header" runat="server" title="User Profile
<%=userName%>" />
//

Thanks for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top