Server control inside another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 200dogz
    New Member
    • Oct 2008
    • 52

    Server control inside another

    Hi,

    I've just noticed if there is a control that runat server and another one directly inside it, it will result in a compilation error.

    eg.

    If I have an asp:Label directly inside a table, it doesn't like it:

    <table id="myTable" runat="server">
    <asp:Label id="myLabel" runat="server" />
    </table>
    Where the error message is CS1502: The best overloaded method match for 'System.Web.UI. HtmlControls.Ht mlTableRowColle ction.Add(Syste m.Web.UI.HtmlCo ntrols.HtmlTabl eRow)' has some invalid arguments


    On the other hand, if I place the asp:Label inside a row or a cell of the table, it would work fine:

    <table id="myTable" runat="server">
    <tr><td>
    <asp:Label id="myLabel" runat="server" />
    </td></tr>
    </table>

    Any idea what's causing the problem? and if I must have a control directly inside another, how can I overcome this problem?


    Thanks in advance :)
  • naga01
    New Member
    • Oct 2008
    • 3

    #2
    In your case with Table, its mandatory to have it wrapped inside a TR and TD

    The controls are suppose to placed where it is suppose to placed.

    Table is a container where it can contain only TR, only TD is a control container.

    Comment

    • 200dogz
      New Member
      • Oct 2008
      • 52

      #3
      Originally posted by naga01
      In your case with Table, its mandatory to have it wrapped inside a TR and TD

      The controls are suppose to placed where it is suppose to placed.

      Table is a container where it can contain only TR, only TD is a control container.
      Hmm thanks for your help.

      The reason I use <asp:Label> inside a <table> is to expend the table on server side, based on the data retrieved from the databases. This has been used for most of the pages in my site.

      what I'm trying to do right now is to provide the ability to export tables to Excel. I can only get it to work if I make the tables to run at server. Is there a way I might be able to do that if I don't want to change what I already have too much, like putting the tables in panel and export them instead?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Why don't you use CSS and JavaScript to "expand" your table instead of placing it inside a Label control? This is not now a Label Control is supposed to be used.

        Then you would have a pure table that you could export easily.

        Comment

        • 200dogz
          New Member
          • Oct 2008
          • 52

          #5
          Originally posted by Frinavale
          Why don't you use CSS and JavaScript to "expand" your table instead of placing it inside a Label control? This is not now a Label Control is supposed to be used.

          Then you would have a pure table that you could export easily.
          sorry I guess "expand" isn't the correct word. I like to use Lables for table with indefinite number of rows.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Sorry, I still don't understand why you are using Labels the way you are.
            Could you better explain what you mean by "expand"?

            Comment

            • 200dogz
              New Member
              • Oct 2008
              • 52

              #7
              Originally posted by Frinavale
              Sorry, I still don't understand why you are using Labels the way you are.
              Could you better explain what you mean by "expand"?
              Say if I have...

              <table>
              <tr><th>Heading </th></tr>
              <asp:Label id="rows" runat="server" />
              </table>
              and in the code I can do:

              OdbcDataReader reader = ... // read from database
              while (reader.Read())
              {
              rows.Text += "<tr><td>" + reader["stuff"] + "</td></tr>";
              }
              I'm kind of new to ASP.NET and this was the first method of table control that I thought of and have been using it since then. It might not be conventional and might not be good, but I don't plan on changing this; at least not for this project I'm working on since it's almost done.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                This is not a good solution because when the Label is rendered it becomes an HTML <span>. Your table will become invalid because <span> tags are not valid within a table.

                For example, the following is valid:
                Code:
                <table>
                <tr>
                  <td></td>
                </tr>
                </table>
                But what's going to happen with the Label is that you're going to end up with:
                Code:
                <table>
                    <span>
                        <tr>
                           <td></td>
                        </tr>
                     </span>
                </table>
                If you're dead set on using this solution to create your table then at least switch out the Label for a Literal Control. Literals are rendered as text.

                In the future you should consider using a Repeater to create your table.


                -Frinny

                Comment

                Working...