dynamically adding links
-
Hi all, This is probably a easy one for someone out there and im hoping that someone can help me as it has been doing my head in for two days. What im trying to do is dynamically add several links in a bullet point form on a aspx page from the code behind file. the problem is that i have mouse over events associated with the link which will display a image in a desinated picture box on the form which relates to the link. All the information about the link including image url is coming from a class that is populated from the database. What i can do: I can add the link in a lable control or place holder control using the:
lable.controls.add(asplink)
but this only puts the link in the lable but not in bullet point form. Please help. Thanks inadvance -
Hi all, This is probably a easy one for someone out there and im hoping that someone can help me as it has been doing my head in for two days. What im trying to do is dynamically add several links in a bullet point form on a aspx page from the code behind file. the problem is that i have mouse over events associated with the link which will display a image in a desinated picture box on the form which relates to the link. All the information about the link including image url is coming from a class that is populated from the database. What i can do: I can add the link in a lable control or place holder control using the:
lable.controls.add(asplink)
but this only puts the link in the lable but not in bullet point form. Please help. Thanks inadvanceCreate a class that inherits the hyperlink or label. Give it the extra properties you need it to display. And make it render them properly. Use that class inside of a repeater, and databind a collection to it. Probably a ton of ways to implement what you want, but I think at first glance, that's the route I'd take.
-
Create a class that inherits the hyperlink or label. Give it the extra properties you need it to display. And make it render them properly. Use that class inside of a repeater, and databind a collection to it. Probably a ton of ways to implement what you want, but I think at first glance, that's the route I'd take.
Thanks so much for the reply. Greatly appreciated! Only thing is never used a repeater and not sure how that going to put my links within the html* tags: Here is my code, hopefully you can understand what i want to do.
if(!IsPostBack) { dbnews = new NewsManip(); //class that contains methods to get data #region Product News ArrayList newslist = new ArrayList(dbnews.GetHomeProductNews()); //Get arraylist which contains a list of classes with links information imgProdNews.ImageUrl = "/img/news/productnews.gif"; Label newslinks = new Label(); newslinks.Text = " "; HyperLink testlink; foreach(News curr in newslist) { newslinks.Text += "* "; testlink = new HyperLink(); testlink.ID = "link"+curr.NewsId.ToString(); testlink.NavigateUrl = "#"; testlink.Text = curr.NewsHeadline; testlink.Attributes.Add("onMouseOver", (imgProdNews.ClientID + ".src=\'"+curr.NewsImageUrl+"'; window.status=\'"+testlink.NavigateUrl+"'; return true;")); testlink.Attributes.Add("onMouseOut", (imgProdNews.ClientID + ".src=\'/img/news/productnews.gif'; window.status=\'\'; return true;")); newslinks.Controls.Add(testlink); newslinks.Text += " "; } newslinks.Text += " "; phProdNews.Controls.Add(newslinks); //Add lable to the place holders
basically i need the format to look like:
* Dynamic link 1
* Dynamic link 2
* Dynamic link 3Again thank you for your help. -
Thanks so much for the reply. Greatly appreciated! Only thing is never used a repeater and not sure how that going to put my links within the html* tags: Here is my code, hopefully you can understand what i want to do.
if(!IsPostBack) { dbnews = new NewsManip(); //class that contains methods to get data #region Product News ArrayList newslist = new ArrayList(dbnews.GetHomeProductNews()); //Get arraylist which contains a list of classes with links information imgProdNews.ImageUrl = "/img/news/productnews.gif"; Label newslinks = new Label(); newslinks.Text = " "; HyperLink testlink; foreach(News curr in newslist) { newslinks.Text += "* "; testlink = new HyperLink(); testlink.ID = "link"+curr.NewsId.ToString(); testlink.NavigateUrl = "#"; testlink.Text = curr.NewsHeadline; testlink.Attributes.Add("onMouseOver", (imgProdNews.ClientID + ".src=\'"+curr.NewsImageUrl+"'; window.status=\'"+testlink.NavigateUrl+"'; return true;")); testlink.Attributes.Add("onMouseOut", (imgProdNews.ClientID + ".src=\'/img/news/productnews.gif'; window.status=\'\'; return true;")); newslinks.Controls.Add(testlink); newslinks.Text += " "; } newslinks.Text += " "; phProdNews.Controls.Add(newslinks); //Add lable to the place holders
basically i need the format to look like:
* Dynamic link 1
* Dynamic link 2
* Dynamic link 3Again thank you for your help.Here's a pretty good example on using the repeater control. http://www.asp101.com/articles/john/repeater/default.asp That should help you out. And it seems you need to create a class called NewsLink that inherits Hyperlink. That has additional properties of OnMouseOver, and OnMouseOut, and when those values aren't empty, they are applied to itself. Use that inside of your repeater, with an unordered list, and you'll be good to go.