Accessing controls inside of TreeNodes and MenuItems?



  • Ahoy mates,  question for ASP NET 2.0.
     
    A MenuItem is set up like this:

    <TD id="ctlWhatever"> <-- MenuItem
        <TABLE>
            <TBODY>
                <TR>
                    <TD>
                        <A>Text</A>

    And a TreeNode is set up basically the same way, except it is nested inside of a DIV instead of another table.

    I would like to know if there is a way to dig into these controls and access the hyperlink on the server side?  Like, is there any way I could cast a menu item to another type of control so I could do a .FindControl in it?  Or anything similar?

    Is it impossible?  Should I give up?  Thanks in advance!



  • What language and why do you have nested tables?



  • If I understand the problem correctly, you should be able to get what you want by adding runat="server" to your A tag. 



  • ASP.NET 2.0. VB or C#.

    I don't have nested tables.  The controls that the .NET framework offers have nested tables.  Specifically the MenuItem.  The TreeNode has tables inside of DIV tags.



  • @mfah said:

    If I understand the problem correctly, you should be able to get what you want by adding runat="server" to your A tag. 

    I would love to be able to add attributes to the A tag; however, it's inside a .NET control.  I'm trying to dig into the <FONT size=2>System.Web.UI.WebControls.MenuItem object and extract that A tag so I can add attributes to it.  You're probably thinking that this is possible front-page material... but I do have good reasons for wanting to do this.  If you want to know, I can explain it... but it's a long-winded story, so I've left it out.  </FONT>

    <FONT size=2>Thanks for the input!

    </FONT>


  • @Pope said:

    I would love to be able to add attributes to the A tag; however, it's inside a .NET control.  I'm trying to dig into the System.Web.UI.WebControls.MenuItem object and extract that A tag so I can add attributes to it.

    There's no way to access the anchor tag serverside as it is HTML content that is only generated during the RenderContents method of the System.Web.UI.WebControls.Menu control with which your MenuItem object instance is associated. System.Web.UI.WebControls.MenuItem itself doesn't even derive from System.Web.UI.Control, meaning it is not control in the ASP.NET sense to begin with!

    I'm interested in [i]why[/i] you'd need to decorate that anchor tag with attributes in the first place. The fact that it is this hard to get something like this done within the scope of the framework should've already told you the idea probably belongs in the 'bad things' category. Probably you are trying to implement some kind of functionality that you think has to be solved by manually manipulating the anchor tag, while the framework may already provide a better, established way of handling the problem at hand (as is usually the case with .NET's web controls).

    Still, if you insist on hacking away at the anchor tag manually, you could try deriving a custom control from Menu and overriding its RenderContents method. However, as I mentioned: most likely, that is [i]not[/i] the correct way of handling the problem. You can forget about taking the easy path of inheriting from MenuItem and overriding a few methods and/or properties: it's a sealed class and can not be inherited from.



  • Well, when you're right, you're right.  And you're right.  I thought about extending the MenuItem, but decided that was crazy.  Now I know it's impossible anyway. 

    Nah, I don't insist on hacking away at the anchor tag.  I just wondered if it was possible and you gave me that answer.  Thanks!

    Why I'm doing it... well... I'll try to keep it short and to keep out too much project history:

    Project specs:

    • Dynamic pages based on what administrator specifies.  Lots of text based on what users/clients input.
    • Multiple language interface, based on what flag a user clicks
      • Some server-side translation (The human translation if we have it available)
      • Some client-side translation (If we don't have the translation available, we use the Google Language API.  Use this until someone inputs a human translation.)
    • There will always be one human translation available, but it could be any language allowed by the project.

    The way a translation happens now:

    • When an object needs to be translated, the object is sent to a helper object that holds the object, the translationID and the property name that needs to be translated.
      • Stuck in an arraylist to await translation
    • Single trip to database for all human translations  (figured these last two steps are probably better than a single trip to the database for each object, especially if there are going to be a lot of objects.)
    • Human translations associated with object's property, or "NoTranslation" flagged if the language isn't available
    • If NoTranslation is flagged, the object's client id is stuck into a javascript array of objects along with a human translation that we DO have.
    • On the client-side a function is called that will loop through the javascript array which looks up the object and sends it to the Google Language API for translation.

    This method works for almost everything...except for ListItems, MenuItems and TreeNodes, because the ClientId isn't available on those objects.  The list object allows for attributes to be added, so it can be modified for my needs; however the other two types are a bit more elusive.  I think we've decided that there are going to be a definite number of menuItem objects so we will just have to have a human translation for all the menuItems.  That leaves the TreeNodes.

    I think I'm going to just go through the whole tree object in javascript if there is one object flagged "NoTranslation," then use the Google Language API to translate it if it isn't in the language that the user wants. (Again, it could be in any language, and they aren't necessarily all in the same language.)

    I know the Google Language API isn't perfect, but that's what the higher ups want.  They're pretty impressed with what they've seen so far.  It's really neat to watch the objects on the page get translated while you're watching them.  It takes a minute and could get annoying, but that's what they want right now.

    I'd love your thoughts, ideas and constructive criticisms. 

    Thanks a TON for your help Ragnax!



  • I guess I should be asking if there is another tool like the Google Language API that I could use server-side. 



  • @Pope said:

    I guess I should be asking if there is another tool like the Google Language API that I could use server-side.

    See: http://code.google.com/apis/ajaxlanguage/documentation/#fonje

    Just use the HttpRequest object in the .NET framework to talk to the Google Language API from the server side.



  • Dude!  You're a ninja.  This must be brand new because I didn't see this the last time I went through that site.  I even got on groups and saw people talking about the server-side functionality that was coming soon.

     Thanks!


Log in to reply