This is a migrated thread and some comments may be shown as answers.

Need advice on implementing an editable list

7 Answers 160 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Roger
Top achievements
Rank 1
Roger asked on 17 Jan 2010, 12:49 PM
I am implementing an editable list, and I could use the standard (and wonderful) functionality of the listbox, using the move up, move down, remove, and add functionality provided by RadListBox. But, there is a twist to what I want to do. I want the listbox to show a list of hyperlinks. Is that possible with ListBox, or should I do this a different way (listview)?

For list box I would need to change the select functionality so that clicking on the item could still activate the hyperlink. So I thought I could use the Checkbox functionality for the list items so, if a user wanted to select a list item to move it or remove it, they could use check boxes. If the user wanted to follow the link, they could just click the link as normal.

Also, for the Add functionality, I want to have the user click the Add button to launch a "Dialog" that will allow them to select the item to which they can link (adding another hyperlink to the list).

Any advice on this would really be appreciated. I just got my license for Radcontrols and I need to accomplish quite a lot in the next two weeks.

Thanks in advance for whatever help you can provide.

7 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 21 Jan 2010, 10:45 AM
Hello Roger,

I have prepared basic example that demonstrates how you can implement your scenario. The basic idea is to use RadListBox templates for the rendering of the hyperlinks and RadWindow for the Add functionality. For details refer to the attached project. You can also take a look at the RadListBox help that can be found here.

Best wishes,
Genady Sergeev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Roger
Top achievements
Rank 1
answered on 21 Jan 2010, 01:08 PM
Wow!

Thank you so much for your reply, and for putting together the example. 

I will work my way through it in a few minutes and implement it in my project, but I wanted to take a moment to say "thanks". 

I really appreciate the help.
0
Roger
Top achievements
Rank 1
answered on 21 Jan 2010, 07:20 PM
Genady,

I have one question about implementing the code example you provided.

I am creating theses controls from within an XSLT file. That is no problem, I know how to create RadControls from within xslt.But this portion of the code you provided:

                Text='<%# DataBinder.Eval(Container, "Text") %>'
                NavigateUrl='<%# DataBinder.Eval(Container, "Value") %>'>

is causing problems. The "<%#" tags are not recognized.

Do you know how I can escape these characters in such a way that the xslt parser accepts them?

Thanks for whatever advice you can give, and, again, for the fantastic example you provided.

UPDATE:

I think I have this part working, and here is how:

                            <ItemTemplate>
                              <xsl:variable name="text">
                                <xsl:processing-instruction name="asp"># DataBinder.Eval(Container, "Text") </xsl:processing-instruction>
                              </xsl:variable>
                              <xsl:variable name="value">
                                <xsl:processing-instruction name="asp"># DataBinder.Eval(Container, "Value") </xsl:processing-instruction>
                              </xsl:variable>
                              <asp:HyperLink runat="server" ID="HyperLink1"
                                  Text='$text' NavigateUrl='$value'>
                              </asp:HyperLink>
                            </ItemTemplate>


0
Roger
Top achievements
Rank 1
answered on 21 Jan 2010, 08:53 PM
I haven't been able to apply the Hyperlink template to the RadListBoxItems.

I am sure it is because of the added complexity I have introduced by using XSLT to generate my pages.

Here is what I have in my code:

The page can have multiple listboxes in it, so with in a "for-each" loop in my xslt, when certain conditions are met, I create a RadListBox like this:
                       <div>
                          <telerik:RadListBox CheckBoxes="True" CssClass="grid_7" runat="server">
                            <xsl:attribute name="ID">
                              <xsl:value-of select="concat('RadListBox', @id)"/>
                            </xsl:attribute>
                            <Items>
                              <xsl:for-each select="propertyValue">
                                <telerik:RadListBoxItem >
                                  <xsl:attribute name="Text">
                                    <xsl:value-of select="@value"/>
                                  </xsl:attribute>
                                  <xsl:attribute name="Value">
                                    <xsl:value-of select="concat('Display.aspx?s=', @linkedID)"/>
                                  </xsl:attribute>
                                </telerik:RadListBoxItem>
                              </xsl:for-each>                              
                            </Items>
                            <ItemTemplate>
                              <xsl:variable name="text">
                                <xsl:processing-instruction name="asp"># DataBinder.Eval(Container, "Text") </xsl:processing-instruction>
                              </xsl:variable>
                              <xsl:variable name="value">
                                <xsl:processing-instruction name="asp"># DataBinder.Eval(Container, "Value") </xsl:processing-instruction>
                              </xsl:variable>
                              <asp:HyperLink runat="server" ID="HyperLink1" Text='$text' NavigateUrl='$value'>
                              </asp:HyperLink>
                            </ItemTemplate>                            
                          </telerik:RadListBox>
                        </div>

This seems to work fine, except the items in the list are not hyperlinks, they are normal text, with the check box at the beginning.

In your example code you have the following line in your Page_Load event:
        RadListBox1.DataBind();

Since I am "calling" the DataBinder.Eval functionality in my code above, I assume I need to call the DataBind function for all the RadListBoxes I have created, so I do that in my ASP.NET code, right after I have applied the XSL Transformation that contains the code above, like this:
            Control data = Page.ParseControl(sHTML); //sHTML contains all the markeup generated by the XSL transformation
            foreach (Control ctrl in data.Controls)
            {
                if (ctrl.ID != null)
                {
                    if (ctrl.ID.IndexOf("RadListBox", 0) != -1) //find all the RadListBox controls and call DataBind on each
                    {
                        Telerik.Web.UI.RadListBox radctrl = (Telerik.Web.UI.RadListBox)ctrl;
                        radctrl.DataBind();
                    }
                }
            }
            hldrItem.Controls.Add(data); //add the controls created by xslt to the Placeholder element in my ASP.NET page.

All this code runs without error, and the RadListBoxes are created. The list items are there but, they are not hyperlinks. So, I am missing something somewhere. (The code listed above does find the RadListBox controls, and it does call the DataBind function for each. No errors occur).

I would really appreciate any advice you can offer.

Thanks very much.









0
Roger
Top achievements
Rank 1
answered on 22 Jan 2010, 03:59 PM
I hope this thread has not been abandoned, because I am still trying to get this to work.

I am trying to create RadListBox controls which have hyperlinks as the list items. I am attempting to do this using XSLT. I can create the RadListBoxes from XSLT but, applying the Template that would create the Hyperlinks has proven to be practically impossible.

Here is the crucial part of the code from the Sample provided above by Genady:
    <telerik:RadListBox runat="server" ID="RadListBox1" CheckBoxes="true"
        <Items> 
            <telerik:RadListBoxItem Text="Text1" Value="http://www.telerik.com" /> 
            <telerik:RadListBoxItem Text="Text2" Value="http://www.telerik.com" /> 
            <telerik:RadListBoxItem Text="Text3" Value="http://www.telerik.com" /> 
            <telerik:RadListBoxItem Text="Text4" Value="http://www.telerik.com" /> 
        </Items> 
        <ItemTemplate> 
            <asp:HyperLink runat="server" ID="HyperLink1"  
                Text='<%# DataBinder.Eval(Container, "Text") %>'  
                NavigateUrl='<%# DataBinder.Eval(Container, "Value") %>'
            </asp:HyperLink> 
        </ItemTemplate> 
    </telerik:RadListBox>  

and here is my implementation, without the ItemTemplate, in XSLT:
                          <telerik:RadListBox CheckBoxes="True" CssClass="grid_7" runat="server"
                            <xsl:attribute name="ID"
                              <xsl:value-of select="concat('RadListBox', @id)"/> 
                            </xsl:attribute> 
                            <Items> 
                              <xsl:for-each select="propertyValue"
                                <telerik:RadListBoxItem > 
                                  <xsl:attribute name="Text"
                                    <xsl:value-of select="@value"/> 
                                  </xsl:attribute> 
                                  <xsl:attribute name="Value"
                                    <xsl:value-of select="concat('Display.aspx?s=', @linkedID)"/> 
                                  </xsl:attribute> 
                                </telerik:RadListBoxItem> 
                              </xsl:for-each>                               
                            </Items> 
                          </telerik:RadListBox> 

As I said, this works well. But, when I try to create the ItemTemplate part of the code I am trying to achieve, I run into Illegal Character errors in the XSLT compilation. I have tried things like this:
                            <ItemTemplate> 
                              <asp:HyperLink runat="server"
                                <xsl:attribute name="Text"
                                  <xsl:value-of select="concat('<%#', ' DataBinder.Eval(Container, ', '&quot;', 'Text', '&quot;', ') ', '%>;')"/> 
                                  </xsl:attribute> 
                                <xsl:attribute name="NavigateUrl"
                                  <xsl:value-of select="concat('<%#', ' DataBinder.Eval(Container, ', '&quot;', 'Value', '&quot;', ') ', '%>')"/> 
                                </xsl:attribute> 
                              </asp:HyperLink> 
                            </ItemTemplate>     

and I have tried escaping the "<" and ">" with "&lt;" and "&gt;", without any luck.

I abandoned the attempt to create the ItemTemplate from within XSLT and tried to capture the RadListBox controls I have created in the ASP.NET code, after the controls have been rendered, and then adding the ItemTemplate and calling DataBind. I have code that does not generate an error but, unfortunately the InstantiateIn code in my custom template class never fires. Here is what I have been attempting (I have tried to remove all irrelevant code. This code runs without error in my project but may not be valid here):
public class MyListBoxTemplate : ITemplate 
    static int itemcount; 
    public void InstantiateIn(System.Web.UI.Control container) 
    { 
        Literal lc = new Literal(); 
        lc.Text = "<asp:HyperLink runat=\"server\" ID=\"HyperLink1\" Text='<%# DataBinder.Eval(Container, \"Text\") %>' NavigateUrl='<%# DataBinder.Eval(Container, \"Value\") %>'></asp:HyperLink>"; 
        itemcount += 1; 
        container.Controls.Add(lc); 
    } 
 
protected void Page_Load(object sender, EventArgs e) 
        if (Request.Params["t"] == null) 
        { 
            displayItem(); 
        } 
 
 
private void displayItem() 
    XslCompiledTransform xtfrm = new XslCompiledTransform(); 
    String xslt; 
    xslt = "transformations/generic_item_edit.xslt"
    xtfrm.Load(Server.MapPath(xslt)); 
 
    XElement xObject = MyWebService.GetSomeXML(Request.Params["id"]); 
 
    StringWriter sw = new StringWriter(); 
    xtfrm.Transform(xObject.CreateReader(), null, sw); 
    String sHTML = sw.ToString(); 
    sHTMLsHTML = sHTML.Replace("xmlns:asp=\"remove\"", ""); 
 
 
    //hldrItem is the ASP Placeholder control in this page 
    hldrItem.Controls.Clear(); 
    StringWriter sw2 = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw2); 
    hldrItem.RenderControl(htw); 
    Control data = Page.ParseControl(sHTML); 
    foreach (Control ctrl in data.Controls) 
    { 
        if (ctrl.ID != null) 
        { 
            if (ctrl.ID.IndexOf("RadListBox", 0) != -1) 
            { 
                Telerik.Web.UI.RadListBox radctrl = (Telerik.Web.UI.RadListBox)ctrl; 
                radctrl.ItemTemplate = new MyListBoxTemplate(); 
                radctrl.DataBind(); 
            } 
        } 
    } 
    hldrItem.Controls.Add(data); 
 

This code runs without any errors, and the RadListBoxes are displayed but, the InstantiateIn function within the nested MyListBoxTemplate class is never fired, and the items in my RadListBoxes are not hyperlinks. They are regular text.

I have spent almost two full days trying to get this working. It seems like it should be a simple thing but using XSLT has greatly increased the complexity.

Is this possible or should I abandon this attempt and try to find some other way to have a list of hyperlinks?

Thanks in advance for any help.






0
Genady Sergeev
Telerik team
answered on 22 Jan 2010, 04:53 PM
Hello Roger,

Indeed, there are some difficulties when using XSLT transformations, I am not quite sure why the template is not adequately applied. However, I've prepared a sample demonstrating how to apply  a template to RadListBox items from the code behind. The sample is using hyper links.

Best wishes,
Genady Sergeev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Roger
Top achievements
Rank 1
answered on 22 Jan 2010, 05:27 PM
Genady,

That is perfect!

It works just great. I was applying the template to the RadListBox instead of to each RadListBoxItem, and I was trying to rely on DataBind() to call InstantiateIn, instead of calling InstantiateIn explicitly, as you have done.

I now have dynamically generated lists with hyperlink list items. Woohoo!

I have posts on several XML-related forums regarding the use of XSLT to generate the ItemTemplate code you originally provided. If I get an answer to that question, I will add it to this thread, for the sake of completeness. It would be best to create the ItemTemplate code in the XSLT so all the functionality is in one place but, this fix you have provided will at least allow me to continue with my project until I am able to figure out the XSLT implementation.

Thanks very much!
Tags
ListBox
Asked by
Roger
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
Roger
Top achievements
Rank 1
Share this question
or