views:

1275

answers:

5

Context: ASP.NET 3.5 / C#

Hi,

I created a user control

public partial class MyControl : UserControl
{
    // EDIT: example first used "UniqueId" as property name, which was wrong.
    public Guid MyId { get; set; }
    // ...
}

and this example usage

<uc1:MyControl 
    ID="myControl" 
    MyId="443CBF34-F75F-11DD-BE2F-68C555D89123"
    runat="server" />

Steps:

  • Add this control to a web form (aspx)

Expected result:

  • the HTML for the user control is added, a unique value (corresponding to Guid.NewGuid()) for MyId is set in the ASPX HTML at design-time as the MyId attribute value.

Actual result:

  • the HTML for the user control is added, a unique value for MyId is not set in the HTML at design time for the MyId Attribute value.

If this is not possible:

  • Workaround 1: Would it be possible to achieve this using a server control? How?
  • Workaround 2: is it possible to achieve this using a UserControl design-mode task?

Clarification:

  • persisting the property value is not an issue, since it never changes for a control intance and is automatically set by ASP.NET through the control declaration in the aspx page.
  • the MyId attribute does not need to be rendered at runtime.

Gr B!

A: 

You have a couple problems here, but first I will answer your questions about the workarounds.

  1. No you are already using a server control.
  2. No design-mode is to just make the lives of the developer easy, it doesn't effect anything else

You have two problems here. There is already a property called UniqueID I don't know if you were trying to overload that, but the question wasn't clear. The second problem is that your UniqueID essentially not getting stored anywhere. Try the following code:

public Guid UniqueId {
    get { return (Guid)ViewState["MyUserControlUniqueId"]; }
    set { ViewState["MyUserControlUniqueId"] = value; }
}

That will store the GUID in the ViewState so that you can retrieve it on post backs.

Update: Given your comment you need to override/use this method to add attributes to the rendered content.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.addattributestorender.aspx

Nick Berardi
Hi Nick, I had renamed the original property to UniqueId for the example code, it was not my intention to override UniqueId.ViewState is not my issue, the goal is to make the new GUID appear in the generated HTML.
Bernard Vander Beken
See comment above.
Nick Berardi
Thanks for the update, I was not clear enough. My goal is to have a different GUID in the *ASPX* design time HTML every time a control is dragged to a web page by the page designer. The attribute does not need to be added to the rendered HTML.
Bernard Vander Beken
A: 

If I understand your question correctly, you are exposing a property on your user control called MyId. This allows you to set the property wherever you put that control.

What you also want, is for the rendered HTML to also include this attribute and value.

If that's the case, the property MyId is not passed through to the HTML, it's only because the user control has MyId as a property that it's visible in the designer.

In your user control you will have defined the markup that gets rendered. So for example if you have:

<asp:Panel runat="Server" Id="myControlDiv">Some other content</asp:Panel>

You can then in your controls prerender event (or wherever else you choose) put

myControlDiv.Attributes.Add("MyId", SomeGuid.ToString())

Which will then get output in the HTML as

<div id="generatedID" MyID="443CBF34-F75F-11DD-BE2F-68C555D89123">Some other content</div>
TreeUK
tnx for your reply. "What you also want, is for the rendered HTML to also include this attribute and value."I can see why you got this impression now, but this was not my intention. By HTML I meant the design time HTML in the aspx.
Bernard Vander Beken
A: 

So you just want a unique ID generated that you only ever use in design time?

Why not override Object.GetHasCode();

And then exposure this as a property?

codemonkeh
A: 

Did you ever figure this out?

Bruce
No, I never did and learned to live with it ;)
Bernard Vander Beken