user339160
user339160

Reputation:

HTML Text writer

In my project , i have created a custom control which inherits from label. My aim is to add 2 links in this . I need to use the same label to render these two links.i tried the bellow code only the first link is loading ,not the second .Please help

my sample code looks like

writer.RenderBeginTag(HtmlTextWriterTag.Link);
writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat(Path 1));
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
writer.RenderEndTag();

writer.RenderBeginTag(HtmlTextWriterTag.Link);
writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat(Path 2 ));
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
writer.RenderEndTag();

Upvotes: 0

Views: 4939

Answers (2)

skyfoot
skyfoot

Reputation: 20769

You need to add the attributes before you render the html tag. The attributes you define for the first link in your code is actually being assigned to the second link tag. The first link tag remains empty.

writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat(Path 1));
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
writer.RenderBeginTag(HtmlTextWriterTag.Link);

writer.RenderEndTag();

writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Concat(Path 2 ));
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
writer.RenderBeginTag(HtmlTextWriterTag.Link);

writer.RenderEndTag();

Upvotes: 3

Jalpesh Vadgama
Jalpesh Vadgama

Reputation: 14266

Here are code for you.


public class MyLabel : Label
{

    protected override void OnInit(EventArgs e)
        {
            this.InitLinkButton();
            base.OnInit(e);       

        }
    public void InitLinkButton()
    {
       System.Web.UI.HtmlControls.HtmlGenericControl div=new System.Web.UI.HtmlControls.HtmlGenericControl("div");
       HyperLink lnk = new HyperLink();
           lnk.NavigateUrl = "http://www.abc.com";
           lnk.Text = "Click here to go to abc.com";
           div.Controls.Add(lnk);  
           //same way you add more link to div
       //finally adding this to base control.
       base.Controls.Add(div);

    }

}

Upvotes: 0

Related Questions