User Control in Design Mode (newbie question)

S

Saber

We have a simple user control containing a Label and a Textbox.
Its code behind:

public partial class uc : System.Web.UI.UserControl
{
private string _Title;
public string Title
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
protected void Page_Load(object sender, EventArgs e){}
}

In runtime anything is going good,
But in "Design Mode" when we set a value to the Title property, like this:
<uc1:uc ID="Uc1" runat="server" Title="Hello" />
It doesn't displays Hello as the label text.

I read http://ajdotnet.wordpress.com/2006/11/11/reach-out-at-design-time/
but his solution works when we inherit from Label, not Web.UI.UserControl
 
B

Brian Williams

So what you have is a parent control (UserControl) that has a title property
and two child controls a label and a textbox.
Your parent control has a Title property that will display text. (You
shound't use this)
Your child controls have thier own Title property, but for the label control
you want to use the Text property. The Title propety on a label control is
used for a tooltip.

So what you will do is:
Don't set the Title property on <uc1:uc ID="Uc1" runat="server" />

In your control you can set it:
<asp:Label ID="Label1" Text="Hello" runat="server" ></asp:Label>

Or in the code-behind of your Uc1 (Control) set it:
Title = "Hello"

Hope this helps.
Regards,
Brian K. Williams
 
S

Saber

The Title propety on a label >control is used for a tooltip.
Thanks for your hint, I renamed the Title property of UC.
So what you will do is:
Don't set the Title property on <uc1:uc ID="Uc1" runat="server" />
In your control you can set it:
<asp:Label ID="Label1" Text="Hello" runat="server" ></asp:Label>

Or in the code-behind of your Uc1 (Control) set it:
Title = "Hello"

Brian,
The "Hello" word is for test, I've lots of user controls on my page,
and each of them have their own Text.
It is good in runtime, but having lots of UCs withe the same title in
"Design Time" is confusing.
 
B

Brian Williams

It shouldn't be confusing because they will all have a different
identification. Maybe I am not understanding your question.

What I am focusing on is the Title="Hello" in your code example on the UC1
control. All User Controls will have a Title property, that is really not
used for anything. It's the controls inside your User Control that you want
to set. So when you set the Title in UC1 it will display text for the User
Control, but that's not the Label control (Label1) inside your UC1

So if you have two User Contols named WebUserControl1 and WebUserControl2.
You would set the public Title property like this:
The HTML Code:
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<uc1:WebUserControl ID="WebUserControl2" runat="server" />

In the Code-Behind:
WebUserControl1.Title = "Hello";
WebUserControl2.Title = "World";

Regards,
Brian K. Williams
 
S

Saber

Brian,

Let's forget about Title!
I renamed it to UCTitle.
Let me ask the question in other way.
If I set a property of the UserControl in a WebForm, like it:
<uc1:WebUserControl ID="WebUserControl1" runat="server" UCTitle="Hello UC1
Title!" />

How I can render UCTitle in "Design Time" (in visual studio), to show the
UCTitle as the Text of Label1 of User Control?
I know I can do it in the Code-Behind, but the code-behind runs in Runtime
not Design Time.

Here is the screenshot I took:
http://server5.pictiger.com/img/852268/other/usercontrol.gif

** I want to somehow show a preview of fruit names in Design Mode instead of
Label,Label,Label,...**

and it is the code:
<uc1:uc ID="Uc1" runat="server" UCTitle="Apple" /><br />

<uc1:uc ID="Uc2" runat="server" UCTitle="Banana" /><br />

<uc1:uc ID="Uc3" runat="server" UCTitle="Orange" /><br />

<uc1:uc ID="Uc4" runat="server" UCTitle="Peach" /><br />



The UCTitle property of UserControl:
public string UCTitle

{

get { return Label1.Text; }

set { Label1.Text = value; }

}
 
B

Brian Williams

Saber,

I don't know of a way to show the child control propereties in a User
Control other than setting the child controls directly in the HTML or
Code-Behind.

If you create a Custom Control you can add attributes to the propeties such
as NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint),
Browsable(true) and override the GetDesignTimeHtml() method. This will give
you full control over the design time render.

I hope this helps.

Regards,
Brian K. Williams
 
S

Saber

Thanks Brian,

Yes, I think we should use Custom Control.
Thanks again for your followings.
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top