Solved

Get values of controls in ListView Items

Posted on 2008-06-23
3
513 Views
Last Modified: 2013-11-26
I have an ASP.NET 3.5 application with a web control that has a ListView which displays a list of questions.  Each question has: the question (label), "pass" radio button, "fail" radio button and a notes multi-line textbox.  The end user will select either pass or fail for each question (list item) and then click a submit button (not in the ListView).  On click I need to iterate through each of the items in the ListView and figure out if pass or fail was checked and record that question id and the response (pass or fail).  I have the recording process done with LINQ and it works well but need to get the iteration down.

Thank you for your help!
0
Comment
Question by:csg_int_it
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 21848507
It's pretty much the same as any data listing control...
aspx:
==========================================================
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VB_2008._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT TOP 4 [EmployeeID], [LastName], [FirstName] FROM [Employees]"></asp:SqlDataSource>
    </div>
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="EmployeeID" 
        DataSourceID="SqlDataSource1">
        <LayoutTemplate>
            <table id="Table1" runat="server">
                <tr id="Tr1" runat="server">
                    <td id="Td1" runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="1" 
                            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                            <tr id="Tr2" runat="server" style="background-color: #FFFBD6;color: #333333;">
                                <th id="Th1" runat="server">
                                    EmployeeID</th>
                                <th id="Th2" runat="server">
                                    LastName</th>
                                <th id="Th3" runat="server">
                                    FirstName</th>
                                    <th id="Th4" runat="server">Stuff</th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr id="Tr3" runat="server">
                    <td id="Td2" runat="server" 
                        style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr style="background-color: #FFFBD6;color: #333333;">
                <td>
                    <asp:Label ID="EmployeeIDLabel" runat="server" 
                        Text='<%# Eval("EmployeeID") %>' />
                </td>
                <td>
                    <asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>' />
                </td>
                <td>
                    <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' />
                </td>
                <td>
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                    <asp:ListItem Text="Pass" Value="p"></asp:ListItem>
                    <asp:ListItem Text="Fail" Value="f"></asp:ListItem>
                    </asp:RadioButtonList>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    
    </form>
</body>
</html>
 
 
 
 
codebehind:
=================================================================
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim str As String = String.Empty
        For i As Integer = 0 To Me.ListView1.Items.Count - 1
            If Me.ListView1.Items(i).ItemType = ListViewItemType.DataItem Then
                str &= "Row: " & i.ToString & " "
                Dim rbl As RadioButtonList
                rbl = CType(ListView1.Items(i).FindControl("RadioButtonList1"), RadioButtonList)
                If rbl.SelectedValue = "p" Then
                    str &= ", Pass"
                Else
                    str &= ", Fail"
                End If
                str &= ", Text: " & CType(ListView1.Items(i).FindControl("TextBox1"), TextBox).Text & "<br />"
            End If
        Next
        Response.Write(str)
    End Sub

Open in new window

0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
ID: 21848527
the gist of the above sample being:
1. Loop through the listview items
2. findcontrol to find your controls and set them to instantiated controls
3. get the value of the instantiated controls:



       For i As Integer = 0 To Me.ListView1.Items.Count - 1
            If Me.ListView1.Items(i).ItemType = ListViewItemType.DataItem Then
                str &= "Row: " & i.ToString & " "
                Dim rbl As RadioButtonList
                rbl = CType(ListView1.Items(i).FindControl("RadioButtonList1"), RadioButtonList)
                If rbl.SelectedValue = "p" Then
                    str &= ", Pass"
                Else
                    str &= ", Fail"
                End If
                str &= ", Text: " & CType(ListView1.Items(i).FindControl("TextBox1"), TextBox).Text & "<br />"
            End If
        Next
0
 

Author Closing Comment

by:csg_int_it
ID: 31469818
Thank you for the very fast response, it helped me to continue on with my development.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question