Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a ListView in my project and i added a link button in ListView Item.Now i want to display the item data of listView in the text boxes on the ClickEvent of linkbutton.
So please tell me How to acces the ListView data and How to access the listview individual item.

XML
<asp:ListView ID="lvEmployee" runat="server" OnSelectedIndexChanging="lvEmployee_SelectedIndexChanging">
                <LayoutTemplate>
                    <table>
                        <tr>
                            <th>
                                <asp:Label ID="lblLayoutEmployeeId" runat="server" Text="Employee Id"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutEmployeeName" runat="server" Text="Employee Name"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutContactNo" runat="server" Text="Contact No"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutAddress" runat="server" Text="Address"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutSalary" runat="server" Text="Salary"></asp:Label>
                            </th>
                        </tr>
                        <tbody id="itemPlaceHolder" runat="server">
                        </tbody>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lblDataEmployeeId" runat="server"><%#Eval("Employee_ID")%></asp:Label>
                        </td>
                        <td>
                            <asp:LinkButton ID="btnDataEmployeeName" runat="server" CommandName="Select"><%#Eval("Employee_Name") %></asp:LinkButton>
                        </td>
                        <td>
                            <asp:Label ID="lblDataContactNo" runat="server"> <%#Eval("Contact_No") %></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblDataAddress" runat="server"><%#Eval("Address") %></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblDataSalary" runat="server"><%#Eval("Salary") %></asp:Label>
                        </td>
                    </tr>
                </ItemTemplate>
</asp:ListView>
Posted

Well, your UI design implementation is not too clear to me but it looks like you have caption and control for that caption to get data - all in a listview.

If at any time you need to access a control, you need to use FindControl method exposed at the row level of your listview.
For any listview event, you can get the rowItem and then use Findcontrol to get the control, sample:
C#
protected void ContactsListView_ItemCreated(object sender, ListViewItemEventArgs e)
  {
    // Retrieve the current item.
    ListViewItem item = e.Item;

    // Verify if the item is a data item.
    if (item.ItemType == ListViewItemType.DataItem)
    {
      // Get the EmailAddressLabel Label control in the item.
      Label EmailAddressLabel = (Label)item.FindControl("EmailAddressLabel");

      // Display the e-mail address in italics.
      EmailAddressLabel.Font.Italic = true;
    }
  }

Refer: MSDN: ListViewItem[^]
 
Share this answer
 
I am using following code and it give's me accurate result......

protected void lvEmployee_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
DataTable table = (DataTable)(Session["EmployeeDB"]);
int Emp_Id =Convert.ToInt32(lvEmployee.DataKeys[e.NewSelectedIndex].Value);
DataRow row = table.Rows.Find(Emp_Id);
txtEmployeeId.Text = row["Employee_Id"].ToString();
txtEmployeeName.Text = row["Employee_Name"].ToString();
txtContactNo.Text = row["Contact_No"].ToString();
txtAddress.Text = row["Address"].ToString();
txtSalary.Text = row["Salary"].ToString();
btnAdd.Text = "Update";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900