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:

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


    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
            Next 


    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.

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

    CS:
     
        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 
            } 
        } 

    -Shinu.
  3. Genady Sergeev
    Admin
    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

    Example:

    <telerik:radlistbox id="RadListBox1" runat="server">
                <Items>
                    <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" />
                </Items>
                <ItemTemplate>
                    <p>
                        <asp:Label runat="server" ID="DateTimeLabel"
                            Text='<%# DateTime.Now.ToShortDateString() %>' ></asp:Label>
                    </p>
                </ItemTemplate>
            </telerik:radlistbox>

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

    RadListBox1.DataBind();
            foreach (RadListBoxItem item in RadListBox1.Items)
            {
                Label label = (Label)item.FindControl("DateTimeLabel");
                Response.Write(label.Text);
            }


    Regards,
    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