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

itemtemplate- (server side) radlistboxitems

2 Answers 299 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 15 Dec 2009, 02:12 AM
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 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Dec 2009, 10:18 AM
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.
0
Genady Sergeev
Telerik team
answered on 17 Dec 2009, 12:02 PM
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.
Tags
ListBox
Asked by
Dave
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Genady Sergeev
Telerik team
Share this question
or