1

It's there a way i can control the ID of my HtmlGenericControl.

I try to create my own HtmlGenericControl and override UniqueID, ClientID, onPreRender etc ... but nothing work

Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98

3 Answers3

2

The following article demonstrates how to override controls your own ID's. Whilst it does not show how to do it for htmlgenericcontrol it should be pretty easy to figure out from all the other examples.

How to Display Asp.Net Controls with Clean ID Values

Alex
  • 913
  • 6
  • 9
  • Thanks it's working fine, in my case it's was not the AddAttribute like the example but the WriteAttribute. Thanks – Cédric Boivin Aug 16 '10 at 20:32
  • :( And this is why link-only answers are bad. Link is now broken. – Ian Sep 28 '16 at 03:11
  • Sorry about that, I wrote the comment about 6 years ago however I did find a reference to the page on wayback machine. Probably a bit out of date now :) http://web.archive.org/web/20110122003516/http://www.bytechaser.com/en/articles/ts8t6k5psp/how-to-display-aspnet-controls-with-clean-id-value.aspx – Alex Sep 28 '16 at 12:06
1

You don't need to override this all, just override Render and do everything manually

Dewfy
  • 23,277
  • 13
  • 73
  • 121
1

The HtmlGenericControl can be manually given a client Id by adding the id attribute:

var bob = new HtmlGenericControl("div")
{
    InnerHtml = "give me an Id!"
};

bob.Attributes.Add("id", "myId");

this.Page.Controls.Add(bob);

The Id given will only be accessible from the client side (e.g. JavaScript).

Pete Amundson
  • 890
  • 5
  • 16