itemtemplate- (server side) radlistboxitems

3 posts, 0 answers
  1. Dave
    Dave avatar
    2 posts
    Member since:
    Mar 2009

    Posted 14 Dec 2009 Link to this post

    How do I read the content of my item template on the server side by looping thru my collection of RadListBoxItems?

    Suppose I have  a template as follows:

                            <div id="TheValue" runat="server">> 
                                Test Id: 
                                <%#DataBinder.Eval(Container, "Value")%> 
                                <br /> 
                            <div id="TheText" runat="server"
                                <%#DataBinder.Eval(Container, "Text")%> 
                                <br /> 
                            <div id="dateadded" runat="server"
                                Date Added: 
                            <hr style="height: 1px; border-color: Black; border-style: solid; width: 95%" /> 

    So in VB.NET in the code behind I expect to be able to  do something like this:

            For Each item As RadListBoxItem In RadListBoxDestination.Items 
               'Grab variables from ItemTemplate (e.g. dateadded ) and do stuff

    Any insight appreciated, thanks!

    - David
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Dec 2009 Link to this post

    Hi Dave,

    Try attaching custom attributes to RadListBoxItem in ItemDataBound event as shown below so that you can access the third value from code behind.

        <telerik:RadListBox DataSourceID="SqlDataSource1" ID="RadListBox1" runat="server" AutoPostBack="True" DataTextField="CategoryName" 
            DataValueField="Description" OnItemDataBound="RadListBox1_ItemDataBound"

        protected void RadListBox1_ItemDataBound(object sender, Telerik.Web.UI.RadListBoxItemEventArgs e) 
            e.Item.Attributes["myId"] = ((DataRowView)e.Item.DataItem)["CategoryID"].ToString(); // Extra value 
        protected void Button3_Click(object sender, EventArgs e) 
            foreach( RadListBoxItem item in RadListBox1.Items ) 
                Response.Write(item.Attributes["myId"]); // get the third value 

  3. Genady Sergeev
    Genady Sergeev avatar
    1600 posts

    Posted 17 Dec 2009 Link to this post

    Hi Dave,

    This can be done the following way:

    1) Attach the DateTimeLiteral to a serverside control, e.g. Label
    2) When iterating on the Items, use the FindControl method of each item to find the label you have added


    <telerik:radlistbox id="RadListBox1" runat="server">
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem1" />
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem2" />
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem3" />
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem4" />
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem5" />
                    <telerik:RadListBoxItem runat="server" Text="RadListBoxItem6" />
                        <asp:Label runat="server" ID="DateTimeLabel"
                            Text='<%# DateTime.Now.ToShortDateString() %>' ></asp:Label>

    Then you can access the DateTime.Now value the following way:

            foreach (RadListBoxItem item in RadListBox1.Items)
                Label label = (Label)item.FindControl("DateTimeLabel");

    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.
Back to Top