Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. ASP.NET Subtle bug

ASP.NET Subtle bug

Scheduled Pinned Locked Moved Clever Code
htmlcsharpcssasp-netsysadmin
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #1

    External CSS file:

    #Main
    {
    background-color: blue;
    }

    Somewhere in the HTML there is this markup:

    <div id="Main"></div>

    Style and everything works properly. Now to enable server side manipulation of the HTML tag, I added:

    <div id="Main" runat="server"></div>

    Everything stops working. The color is no longer blue. Took me a while to figure out even though I am normally very careful to avoid these issues.:-O

    R C S C 4 Replies Last reply
    0
    • R Rama Krishna Vavilala

      External CSS file:

      #Main
      {
      background-color: blue;
      }

      Somewhere in the HTML there is this markup:

      <div id="Main"></div>

      Style and everything works properly. Now to enable server side manipulation of the HTML tag, I added:

      <div id="Main" runat="server"></div>

      Everything stops working. The color is no longer blue. Took me a while to figure out even though I am normally very careful to avoid these issues.:-O

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      Looks like there are not many ASP.NET people over here, so here is the answer. #Main CSS selector selects an element of id Main. So it will select an element of id Main and apply the background color of blue. When you put runat=server on a div, it becomes a server control. Meaning that the elements properties can be modified through the code for the web page. The issue here is that one should never rely on the ID attribute of a server control and assume that it will be the id on the client too. ASP.NET provides another property called ClientID for that. There are instances when the ClientID does not match the actual server side ID. This happens when the control is in a naming container. For examples let's say that the control is in a user control. The user control can be included many times in the page but there can be only one element of a given ID. So what ASP.NET does is that it appends a unique ID to the id specified in the markup. For example, the id can become XYZ_Main where XYZ is the ID if the user control when it is embedded in the page. UserControl is not the only container where one can observe this behavior. It also happens when the control is in a master page or the control is inside a repeater/datalist/gridview control. Unfortunately teh solution is ugly and it involved using class selectors instead of a id selector.

      The CSS can be modified as below: .Main { background-color:blue; }

      1 Reply Last reply
      0
      • R Rama Krishna Vavilala

        External CSS file:

        #Main
        {
        background-color: blue;
        }

        Somewhere in the HTML there is this markup:

        <div id="Main"></div>

        Style and everything works properly. Now to enable server side manipulation of the HTML tag, I added:

        <div id="Main" runat="server"></div>

        Everything stops working. The color is no longer blue. Took me a while to figure out even though I am normally very careful to avoid these issues.:-O

        C Offline
        C Offline
        Chris Maunder
        wrote on last edited by
        #3

        Ooh. Subtle. Nice one.

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          External CSS file:

          #Main
          {
          background-color: blue;
          }

          Somewhere in the HTML there is this markup:

          <div id="Main"></div>

          Style and everything works properly. Now to enable server side manipulation of the HTML tag, I added:

          <div id="Main" runat="server"></div>

          Everything stops working. The color is no longer blue. Took me a while to figure out even though I am normally very careful to avoid these issues.:-O

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          Heh, nice. I don't care much for ASP.NET myself, but occasionally help a co-worker with his projects... this'd be quite a head-scratcher!

          ---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums

          1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            External CSS file:

            #Main
            {
            background-color: blue;
            }

            Somewhere in the HTML there is this markup:

            <div id="Main"></div>

            Style and everything works properly. Now to enable server side manipulation of the HTML tag, I added:

            <div id="Main" runat="server"></div>

            Everything stops working. The color is no longer blue. Took me a while to figure out even though I am normally very careful to avoid these issues.:-O

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            i run into that one almost daily. i'll find myself wanting to be able to tweak an element on the server, so i'll add that runat, then kick myself for not remembering that the ID changes. it's even more annoying if you're doing any javascript stuff with the element in question. now, getElementByID can no longer find the element by ID, because that ID doesn't exist. so, you have to do stuff like:

            // create the JS on the server, using a dummy name for the ctrl ID
            string scripts=@"
            ...
            var expandedCtrl = document.getElementById(""%%EXPANDEDLINKS%%"");
            ...
            ";

            // replace the dummy name with whatever .Net chose for the ctrl ID...
            scripts = scripts.Replace("%%EXPANDEDLINKS%%", hdn_expandedLinks.UniqueID);
            ...
            cs.RegisterStartupScript(cstype, scriptName, scripts, false);

            ...to make sure your JS has the right control ID.

            image processing toolkits | batch image processing | blogging

            T M 2 Replies Last reply
            0
            • C Chris Losinger

              i run into that one almost daily. i'll find myself wanting to be able to tweak an element on the server, so i'll add that runat, then kick myself for not remembering that the ID changes. it's even more annoying if you're doing any javascript stuff with the element in question. now, getElementByID can no longer find the element by ID, because that ID doesn't exist. so, you have to do stuff like:

              // create the JS on the server, using a dummy name for the ctrl ID
              string scripts=@"
              ...
              var expandedCtrl = document.getElementById(""%%EXPANDEDLINKS%%"");
              ...
              ";

              // replace the dummy name with whatever .Net chose for the ctrl ID...
              scripts = scripts.Replace("%%EXPANDEDLINKS%%", hdn_expandedLinks.UniqueID);
              ...
              cs.RegisterStartupScript(cstype, scriptName, scripts, false);

              ...to make sure your JS has the right control ID.

              image processing toolkits | batch image processing | blogging

              T Offline
              T Offline
              tarasp
              wrote on last edited by
              #6

              You don't need to do all of that. You can get the actual control id from your java script like this:

              var MyControl = document.getElementById("<%=MyControl.ClientID%>");

              Tara

              C 1 Reply Last reply
              0
              • T tarasp

                You don't need to do all of that. You can get the actual control id from your java script like this:

                var MyControl = document.getElementById("<%=MyControl.ClientID%>");

                Tara

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                true. but (for whatever reason), we put all of our JS on the code-behind pages.

                image processing toolkits | batch image processing | blogging

                1 Reply Last reply
                0
                • C Chris Losinger

                  i run into that one almost daily. i'll find myself wanting to be able to tweak an element on the server, so i'll add that runat, then kick myself for not remembering that the ID changes. it's even more annoying if you're doing any javascript stuff with the element in question. now, getElementByID can no longer find the element by ID, because that ID doesn't exist. so, you have to do stuff like:

                  // create the JS on the server, using a dummy name for the ctrl ID
                  string scripts=@"
                  ...
                  var expandedCtrl = document.getElementById(""%%EXPANDEDLINKS%%"");
                  ...
                  ";

                  // replace the dummy name with whatever .Net chose for the ctrl ID...
                  scripts = scripts.Replace("%%EXPANDEDLINKS%%", hdn_expandedLinks.UniqueID);
                  ...
                  cs.RegisterStartupScript(cstype, scriptName, scripts, false);

                  ...to make sure your JS has the right control ID.

                  image processing toolkits | batch image processing | blogging

                  M Offline
                  M Offline
                  MartinSmith
                  wrote on last edited by
                  #8

                  IMO You should use String.Format for this. string.Format(@" ... var expandedCtrl = document.getElementById(""%{0}""); ... ", hdn_expandedLinks.UniqueID)

                  C 1 Reply Last reply
                  0
                  • M MartinSmith

                    IMO You should use String.Format for this. string.Format(@" ... var expandedCtrl = document.getElementById(""%{0}""); ... ", hdn_expandedLinks.UniqueID)

                    C Offline
                    C Offline
                    Chris Losinger
                    wrote on last edited by
                    #9

                    when the JS is hundreds of lines long and that ID, and others, have to be inserted multiple times, string.Format would get very confusing.

                    image processing toolkits | batch image processing | blogging

                    M 1 Reply Last reply
                    0
                    • C Chris Losinger

                      when the JS is hundreds of lines long and that ID, and others, have to be inserted multiple times, string.Format would get very confusing.

                      image processing toolkits | batch image processing | blogging

                      M Offline
                      M Offline
                      MartinSmith
                      wrote on last edited by
                      #10

                      I can see your point for readability. I think it would be nicer if VS had better support for manipulating these though such as colour coding all instances of a selected parameter or allowing automatic re-numbering if you want to insert another parameter in the middle of a list (or delete an existing parameter)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups