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

How to read property from a control in the item template

2 Answers 234 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 25 Feb 2011, 02:10 PM
Hi. I would really appreciate some guidance to show me how to read the state of a control property in a list. I have a simple list that has a user control within the item template.

I want to have a checkbox for each item so that the user can select one or more items. I can easily add the checkbox to the user control and provide code to get/set the checkbox (it is not linked to any datasource data but would be simply to enable the user to select items).

External to the listView I have a button. In the codebehind for the button I want to:

foreach (var i in myList.items) { }

to parse through the displayed items in the listView. My first question is will myList.items return the displayed items (after paging) or all of the items (ie if I have 100 items displayed as 10 pages of 10 items does myList.items return the displayed items or all 100 items - if it returns all is there a way to only get the displayed items in the current page).

within my foreach loop how can I reference the user control. I want to just read a property from the user control; ie. myItem.isChecked

Thanks


<telerik:RadListView ID="myList" runat="server" ItemPlaceholderID="PlaceHolder1"
    DataKeyNames="ListingId" ConvertEmptyStringToNull="False" AllowPaging="True"
    PageSize="15">
    <LayoutTemplate>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </LayoutTemplate>
    <ItemTemplate>
        <div class="rlvI">
            <uc:uListItem ID="myItem" runat="server" ListingId='<%# Eval("ListingId") %>' Title='<%# Eval("Title") %>'
                Description='<%# Eval("Description") %>' Owner='<%# Eval("Owner") %>' image='<%# Eval("ThumbImg") %>'
                Rating='<%# Eval("Rating") %>' Reviewers='<%# Eval("NoRate") %>' HasMail='<%# Eval("mail") %>' />
        </div>
    </ItemTemplate>
    <EmptyDataTemplate>
        <div>
            <div>
                There are no items to be displayed.</div>
        </div>
    </EmptyDataTemplate>
</telerik:RadListView>

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 Feb 2011, 11:14 AM
Hello Paul,

You can try the following code snippet to loops through each ListView item and get reference to UserControl from an external button click.
C#:
protected void Button1_Click(object sender, EventArgs e)
  {
      foreach (RadListViewDataItem item in myList.Items)//loops through each item in current page
      {
          UserControl control = (UserControl)item.FindControl("myItem");// accessing UserControl
          
       }
  }

-Shinu.
0
Paul
Top achievements
Rank 1
answered on 28 Feb 2011, 12:48 PM
Thanks. I replaced UserControl with Controls_myControlClass and it all works fine.
Tags
ListView
Asked by
Paul
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Paul
Top achievements
Rank 1
Share this question
or