More in this section

Forums / Developing with Sitefinity / How to handle links dynamically?

How to handle links dynamically?

9 posts, 0 answered
  1. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    07 Jul 2009
    Link to this post
    Hey all, what would be the best way to do this:

    I have a module that contains a collection of items, and each item contains links to outside websites. I want to be able to have SF generate the hyperlink from the collection. Something like the following:


    Items:
    Item 1 > google.com
               > sitefinity.com
    Item 2 > yahoo.com
               > telerik.com

    So on the first view page we would have "Item 1" and "Item 2" listed, and when a user clicks either one it should load a page with all the links associated with the clicked item displayed. Normally I would accomplish this by passing the item's id to another page by way of query string, but I distinctly remember from a webinar or tutorial video that SF is able to handle this much cleaner and nicer.

    Thanks!
  2. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    07 Jul 2009
    Link to this post
    OK, since I haven't been able to find any information (probably looking in all the wrong places, lol) on how to do what I want to do, I made this in the meantime:

    ListView Code-Behind:
    protected void Page_Load(object sender, EventArgs e) 
        var db = new GSFyi.GSFyiDataClasses_DataContext(); 
     
        var states = (from s in db.FYI_States 
                              where !s.id.Equals(Guid.Empty) 
                              orderby s.name ascending 
                              select s).ToArray(); 
     
     
        System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
     
        sb.AppendLine("<ul class=\"multicolumnlist\">"); 
        foreach (var state in states) 
        { 
            sb.AppendLine("<li class=\"multicolumnlistitem\"><a href=\"StateFyi/StateFyiView.aspx?s=" + state.id + "\">" + state.name + "</a></li>"); 
        } 
        sb.AppendLine("</ul>"); 
        test.Text = sb.ToString(); 

    ItemView Code-Behind:
    public partial class Custom_Modules_GSFyi_Frontend_Controls_GSFyiStateItem : System.Web.UI.UserControl 
        Guid _guid; 
        string sName; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            string s = Request.QueryString["s"]; 
            _guid = new Guid(s); 
            GetStateItems(); 
            sName = StateName(); 
            this.Page.Title = string.Format("{0} FYI",sName); 
            litTitle.Text = string.Format("{0}-Specific Information",sName); 
        } 
        protected string StateName() 
        { 
            var db = new GSFyi.GSFyiDataClasses_DataContext(); 
     
            var state = (from s in db.FYI_States 
                         where s.id == _guid 
                         select s).Single(); 
     
            return state.name; 
        } 
        protected void GetStateItems() 
        { 
            var db = new GSFyi.GSFyiDataClasses_DataContext(); 
     
                var items = (from i in db.FYI_Items 
                             where i.stateid == _guid 
                             select i).ToArray(); 
     
                if (items.Length == 0) 
                { 
                    litOutput.Text = "No Items!"
                } 
                else 
                { 
                    System.Text.StringBuilder sb = new System.Text.StringBuilder(); 
                    sb.AppendLine("<ul style=\"list-style-type:none;\">"); 
                    foreach (var item in items) 
                    { 
                        switch (item.type) 
                        { 
                            case 1: // url 
                                sb.AppendFormat("<li><a href=\"{0}\" target=\"_blank\">{1}</a></li>", item.url, item.name); 
                                break
                            case 2: // email 
                                sb.AppendFormat("<li><a href=\"mailto:{0}\" target=\"_blank\">{1}</a></li>", item.url, item.name); 
                                break
                            case 3: // phone 
                                sb.AppendFormat("<li>{0} - {1}</li>", item.name, item.url); 
                                break
                            default
                                sb.AppendFormat("Something else..."); 
                                break
                        } 
     
                    } 
                    sb.AppendLine("</ul>"); 
                    litOutput.Text = sb.ToString(); 
                } 
         
        } 

    So as you can see, the ListView control lists out all of the state items, then when one of those is clicked it passes the state's id with the URL, which is then read by the ItemView control and it populates an unordered list with the available items.

    I think what I am looking for on SF's side is a HostView? I remember one of the tutorials I followed for creating modules utilized this, and I could access a passed ID without using a querystring:

    this.DataItemId; 

    Anyway, I hope that this update post makes my original one a little more clear. I am notorious for failing to explain what I am trying to do haha.

    Also, on a side note, how could I implement a BreadCrumb control into this? That way there will be a link that users can click to go back to the overview page rather than having to use their browser's back button.
  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    10 Jul 2009
    Link to this post
    Hello andersleet,

    If I should be honest, I didn't get the scenario. I am not exactly sure what you mean by "hyperlink from the collection", perhaps you want to have a hyperlink for every link in the collection?
    I apologize that we are not able to answer this one so fast, but I am sure we will, if you could elaborate a bit more. Thank you in advance!

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  4. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    10 Jul 2009
    Link to this post
    Sorry Georgi, I am notorious for explaining some things poorly! Let me try again:

    I have three new tables in my SF database. One holds the States, one holds the Link Type, and the last one holds the actual Links. If you look at the first section of code I posted, you will see that it queries the state database and then outputs them in a list with a hyperlink to the display page. Each of these state hyperlinks has their database ID passed along with them (StateFyiView.aspx?s=6199697a-b5cc-4c7a-ac26-347928f75c17, for example). When the StateFyiView.aspx loads, it reads the query string 's' and loads the appropriate links and outputs them in a style list (second block of code).

    What I want to do is eliminate the need to pass the stateid in the URL. I know I have used this.DataItemId before, but that is probably only for databound controls correct? So my question is: how do I pass the stateid without using a query string?

    I hope this clarifies it a little for you!

    Regards,
    anders

  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    15 Jul 2009
    Link to this post
    Hi andersleet,

    Is there an option there to post the information, rather than taking it from the query string?
    For example, you might have the value that is in the query string in a hidden html control. Then when the form is posted (or there is a postback), you can just read the hidden control's value?

    Best wishes,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    15 Jul 2009
    Link to this post
    Thanks Georgi,

    Since the links are generated dynamically, how would I make sure that when I click one of the links the hidden field's value is set to the id of that particular item? 

    Thanks,
    anders
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    20 Jul 2009
    Link to this post
    Hi andersleet,

    Since the click is performed on the client side, everything should happen there. You can tune your links, and set a javascript function to be executed on Click. The function itself will find the hidden field in the DOM and will assign it a value. Then the same function will call the Asp.Net form postback, so the value of the field will be submitted.

    I hope this makes sense.

    All the best,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  8. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    21 Jul 2009
    Link to this post
    Hi Georgi,

    This is what I have come up with so far:

    foreach (GSDataLayer.Fyi.States.State s in stateList) 
                { 
                    stateListOutput.Text += string.Format("<li class=\"multicolumnlistitem\"><a href=\"javascript:document.getElementById('{0}').value='{1}';document.stateform.submit();\">{2}</a></li>\n"new string[] { idfield.ClientID, s.ID.ToString(), s.Name }); 
                } 

    The problem is 'stateform' always comes up null. It is a normal HTML form:

    <form id="stateform" action="StateFyi/StateFyiView.aspx" method="post"
        <input type="hidden" id="idfield" name="s" runat="server" /> 
    </form> 

    I have been reading around that I cannot have a form nested inside of another (this form [obviously] resides inside of the main form that is declared on my masterpage). Could you please elaborate on your last post regarding the postback and submission? 

    Thanks again!

    Regards,
    anders
  9. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    24 Jul 2009
    Link to this post
    Hello andersleet,

    With Form Submission I meant that you should submit the Asp.Net Form. You can define your fields without any surrounding html form. Then if you define a submit button, it will cause page postback and the whole "asp.net" form together with your input controls will be submitted.

    Best wishes,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
9 posts, 0 answered