locked
Changing control ID's before page is sent to browser RRS feed

  • Question

  • User381432427 posted

    I have a page as part of a Content Management System that loads multiple custom controls, which asp.net is assigning unique ID's to dynamically.  I am fine with that, except that once the page is rendered to the browser the ID's no longer match what is stored in the style sheet.  Since it seems that control.uniqueid is read only I thought it would be easy enough to capture the rendered stream from the response object before it goes out to the browser, do a string replace for the extra code being entered into the id's then flush.  But I don't seem to be able to successfully override any events in order to test this theory.  Does anyone know if I am wasting my time on this, or if any examples of what I am trying to do exists?

    Friday, June 6, 2008 2:46 PM

Answers

  • User-202031012 posted

    Something like this might work: 

    1) Make a class derrived from Page:

    public class PageBase : System.Web.UI.Page
    {
        protected override void Render(HtmlTextWriter writer)
        {

            // Render the page to a memory string
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter tempHtw = new HtmlTextWriter(new System.IO.StringWriter(sb));
            base.Render(tempHtw);

            // Do modifications on memory string
            sb.Replace(...);

            // Write the modified string to the real HtmlTextWriter
            writer.Write(sb.ToString());
        }
    }
    2) Derive all your page classes from PageBase instead of Page

    I am not sure how this behaves with master pages.

    And again, you should think twice before using such a solution.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2008 8:24 AM

All replies

  • User-202031012 posted

    It is possible to change the rendered output but messing with it is not recommended as many asp.net features depend on the generated code (page state, validation etc).

    solutions: 

    1) Change the css so that it doesn't use id's and add class attributes to controls

    <style>

    .MyClass { color:red } 

    </style> 

    <asp:Panel runat="server" CssClass="MyClass" ... 

    2) use some fixed html elements as containers for asp.net controls

    <style>

    #fixedID { color:red } 

    </style>

    <div id="fixedID">

    <asp:Panel runat="server" ...

    ... 

    </div>
     

    Friday, June 6, 2008 4:04 PM
  • User-16411453 posted

     If the controls internal code (Or Creator) does not assign an ID to the controls internal object; or particular object in question, then the server assigns that control object with it's own ID during the control render process. If the object is placed in a master page, or if script manager is added, then once again the ID's will change. Every object has to have it's own unique ID.

    You can modify your custom server control code (If your the creator) , and add an ID via object.ID or as an attribute object.Attribute.Add("id", "_controlID"). The later assigns an ID that the server won't change.

    There is also the FindControl function, in which you can find the control, and perhaps change the ID, but you would have to do it after the control renders, and before the actual page renders.  Somewhere in between.  I've never tried it so I don't know. 

    I think your wasting your time, because of many other issues that will arise in actual production use.

     

     

    Friday, June 6, 2008 7:26 PM
  • User381432427 posted

    Changing the style sheets isn't an option at this point, and I am changing the control's id property myself.  The problem is that it's inside a control that gets loaded programmatically  into a placeholder, and the final generated name gets prepended with the placeholder's ID. I'm also not worried about post back problems that may arise, because I actually don't need to retain the state information of these controls. 

    Anyone? 

     

     

    Monday, June 9, 2008 7:57 AM
  • User-202031012 posted

    Something like this might work: 

    1) Make a class derrived from Page:

    public class PageBase : System.Web.UI.Page
    {
        protected override void Render(HtmlTextWriter writer)
        {

            // Render the page to a memory string
            StringBuilder sb = new StringBuilder();
            HtmlTextWriter tempHtw = new HtmlTextWriter(new System.IO.StringWriter(sb));
            base.Render(tempHtw);

            // Do modifications on memory string
            sb.Replace(...);

            // Write the modified string to the real HtmlTextWriter
            writer.Write(sb.ToString());
        }
    }
    2) Derive all your page classes from PageBase instead of Page

    I am not sure how this behaves with master pages.

    And again, you should think twice before using such a solution.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 9, 2008 8:24 AM
  • User381432427 posted

    That did the trick!   Thank you!

    Monday, June 9, 2008 12:37 PM