How to read property from a control in the item template

3 posts, 1 answers
  1. Paul
    Paul avatar
    55 posts
    Member since:
    Nov 2010

    Posted 25 Feb 2011 Link to this post

    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. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Feb 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Paul
    Paul avatar
    55 posts
    Member since:
    Nov 2010

    Posted 28 Feb 2011 Link to this post

    Thanks. I replaced UserControl with Controls_myControlClass and it all works fine.
Back to Top