How to add an attribute to a html control in code behind?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Imran Aziz

    How to add an attribute to a html control in code behind?

    Hello All,

    I have a navigation system for the site , in which the links are highlighted
    based on which section the site is in. Now the issue is that I do that using
    a class="thissect ion" attribute as show in the below example



    <li><a href="../themes/" id="n_themes" class="thissect ion" runat="server"[color=blue]
    >THEMES</a></li>[/color]



    How can I dynamically alter it in code behind, that is add attributes to an
    html control using code behind ?

    My current code behind does something like this to make the links bold, but
    need to add the class attribute to the currently selected section.



    Uri inUrl = Request.Url;

    String strURL = inUrl.AbsoluteU ri;

    if (strURL.IndexOf ("themes") > 0)

    {

    n_themes.InnerH tml = "<strong>THEMES </strong>";

    n_themes = "";

    }

    else

    {

    if (strURL.IndexOf ("bookmarks" ) > 0)

    {

    n_bookmarks.Inn erHtml = "<strong>BOOKMA RKS</strong>";

    }

    else

    {

    if (strURL.IndexOf ("members") > 0)

    {

    n_members.Inner Html = "<strong>MEMBER S</strong>";

    }

    else

    {

    if (strURL.IndexOf ("sources") > 0)

    {

    n_sources.Inner Html = "<strong>SOURCE S</strong>";

    }

    else

    {

    if (strURL.IndexOf ("_admin") > 0)

    {

    n_admin.InnerHt ml = "<strong>AD MIN</strong>";

    }

    else

    {

    n_home.InnerHtm l = "<strong>HO ME</strong>";

    }

    }

    }

    }

    }



    Thanks a lot,



    Imran


  • Jason L Lind

    #2
    RE: How to add an attribute to a html control in code behind?

    If you add the runat="server" attribute to the element you can call the
    element on the code behind by its id as an HtmlGenericCont rol and then set
    the attributes like

    this.id.Attribu tes["Attribute"] = value

    Hope that helps,

    Jason Lind
    Senior Software Engineer
    Triton Tek

    "Imran Aziz" wrote:
    [color=blue]
    > Hello All,
    >
    > I have a navigation system for the site , in which the links are highlighted
    > based on which section the site is in. Now the issue is that I do that using
    > a class="thissect ion" attribute as show in the below example
    >
    >
    >
    > <li><a href="../themes/" id="n_themes" class="thissect ion" runat="server"[color=green]
    > >THEMES</a></li>[/color]
    >
    >
    >
    > How can I dynamically alter it in code behind, that is add attributes to an
    > html control using code behind ?
    >
    > My current code behind does something like this to make the links bold, but
    > need to add the class attribute to the currently selected section.
    >
    >
    >
    > Uri inUrl = Request.Url;
    >
    > String strURL = inUrl.AbsoluteU ri;
    >
    > if (strURL.IndexOf ("themes") > 0)
    >
    > {
    >
    > n_themes.InnerH tml = "<strong>THEMES </strong>";
    >
    > n_themes = "";
    >
    > }
    >
    > else
    >
    > {
    >
    > if (strURL.IndexOf ("bookmarks" ) > 0)
    >
    > {
    >
    > n_bookmarks.Inn erHtml = "<strong>BOOKMA RKS</strong>";
    >
    > }
    >
    > else
    >
    > {
    >
    > if (strURL.IndexOf ("members") > 0)
    >
    > {
    >
    > n_members.Inner Html = "<strong>MEMBER S</strong>";
    >
    > }
    >
    > else
    >
    > {
    >
    > if (strURL.IndexOf ("sources") > 0)
    >
    > {
    >
    > n_sources.Inner Html = "<strong>SOURCE S</strong>";
    >
    > }
    >
    > else
    >
    > {
    >
    > if (strURL.IndexOf ("_admin") > 0)
    >
    > {
    >
    > n_admin.InnerHt ml = "<strong>AD MIN</strong>";
    >
    > }
    >
    > else
    >
    > {
    >
    > n_home.InnerHtm l = "<strong>HO ME</strong>";
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    > Thanks a lot,
    >
    >
    >
    > Imran
    >
    >
    >[/color]

    Comment

    Working...