Skip to content

How to add a href to htmlgenericcontrol img in asp.net c#

rajashri sarade

Asked Sep 6, 2013, 2:04 AM EDT6 answers
Share
LinkedInX
HELO SIR.
i m using htmlgenericcontrl for ul and li for dynamic menu
in that for submenu i want to disply submenu name and img
and i want  to give a link to submenu name and also for image
so pls help me
how to add a href link to image or to any div
thk u

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    +2
    that's too hard to read no?

    Anyway, follow this code, this is how you should do it:

    -------------------------------------------------
    Java
    HtmlGenericControl li = new HtmlGenericControl("li");
                HtmlGenericControl anchor = new HtmlGenericControl("a");
                anchor.Attributes.Add("href", "http://www.google.com");
                HtmlGenericControl img = new HtmlGenericControl("img");
                img.Attributes.Add("height", "100px");
                img.Attributes.Add("width", "100px");
                img.Attributes.Add("src", "Images/info.png");            
                anchor.Controls.Add(img);
                li.Controls.Add(anchor);
                panel1.Controls.Add(li);
    -------------------------------------------------
    
    
    Hope it helps!
    
    Happy Coding :)
    +2
  • Sunny  Sharma
    0
    Hi rajashri,

    Just put that image inside anchor like:




    Hope you got the point.

    Happy Coding :)
    0
  • 0
    helo sir i m doing this in code behide so how can i add a link to img generichtml control
    0
  • Sunny  Sharma
    0
    please share your code what you've tried so far.
    0
  • 0
    private HtmlGenericControl LIList(string innerHtml, string rel, string url,string pic) { string imgsr = null; //int len = 0; HtmlGenericControl li = new HtmlGenericControl("li"); if (pic == "") { //len = 0; imgsr = ""; } else { //len=pic.Length; imgsr = pic.Substring(2); } HtmlGenericControl limage = new HtmlGenericControl("img"); limage.Attributes.Add("width","90px"); limage.Attributes.Add("height","70px"); limage.Attributes.Add("src", "" + imgsr + ""); HtmlGenericControl menudiv = new HtmlGenericControl("div"); menudiv.Attributes.Add("width", "100%"); menudiv.Attributes.Add("height", "100%"); menudiv.Attributes.Add("rel", rel); if (rel == "") { menudiv.InnerHtml = "" + innerHtml + ""; Session["filepath"] = url.ToString(); } else { fileadd = url + "?Id=" + rel; menudiv.InnerHtml = "" + innerHtml + ""; Session["filepath"] = fileadd.ToString(); } string stpath = "" + Session["filepath"].ToString() + ""; //HtmlGenericControl alink = new HtmlGenericControl("a"); //alink.Attributes["href"] = stpath; //alink.Controls.Add(limage); limage.InnerHtml = ""; Session["subid"] = rel.ToString(); menudiv.Controls.Add(limage); li.Controls.Add(menudiv); return li; }
    0
  • 1 short reply
    • 0
      Thanks Sunny for your help
      0