0

I have a listview in asp.net like this:

<asp:ListView ID="lvLoads" DataKeyNames="BookingId" OnItemCommand="lvLoads_OnItemCommand" runat="server">
            <EmptyDataTemplate>
                <table>
                    <tr>
                        <td>No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <GroupTemplate>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </GroupTemplate>
            <ItemTemplate>
                <fieldset>
                    <table class="table table-striped table-responsive">
                        <thead>
                            <tr class="hidden-lg">
                                <th width="15%">Type</th>
                                <th width="31%">Details</th>
                                <th width="25%">Distance/Duration</th>
                                <th width="21%">&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td width="15%">
                                    <asp:Label CssClass="hide" ID="lblBokingId" runat="server"><%# Eval("BookingId") %></asp:Label>             

                <asp:Label ID="lblLoadTypeName" runat="server"> <%# Eval("LoadTypeName") %></asp:Label>
                                    <br>
                                </td>
                                <td width="21%">
                                  <asp:LinkButton ID="btnViewMore" CssClass="btn btn-warning" CommandName="ViewMore" runat="server">View More</asp:LinkButton>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </fieldset>
            </ItemTemplate>
        </asp:ListView>

Now on click of the button ViewMore i wrote this code to extract listviewItem's value:

var id = ((Label)e.Item.FindControl("lblBookingId")).Text;
var name = ((Label)e.Item.FindControl("lblLoadTypeName")).Text;

But e.Item.FindControl("lblBookingId") always comes null. I checked the page's html by using inspect element of chrome, and i was shocked to see that my id somehow got changed to ContentSection_ctl00_lblLoadTypeName_0

I am really stuck at this. Please help me. Thanx in Advance

Murtaza Munshi
  • 1,065
  • 4
  • 13
  • 40

1 Answers1

0

I had done some work with your list view and one which i had created to resemble the problem.

Default2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <div>
            WORKING EXAMPLE
        <asp:ListView ID="lvLoads" runat="server">
           <LayoutTemplate>
               <table border="0" cellpadding="1">
                   <tr style="background-color: #E5E5FE">
                       <th align="left">EmployeeId
                       </th>
                       <th align="left">LastName
                       </th>

                       <th align="left"></th>
                   </tr>
                   <tr runat="server" id="itemPlaceholder" />
               </table>
           </LayoutTemplate>
           <ItemTemplate>
               <tr id="Tr1" runat="server">
                   <td id="Td1" runat="server">
                       <asp:Label ID="lblCustomerID" runat="server" Text='<%# Eval("BookingId") %>'></asp:Label>
                   </td>
                   <td id="Td2" runat="server">
                       <asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LoadTypeName") %>'></asp:Label>
                   </td>

                   <td id="Td3" runat="server">
                       <asp:LinkButton ID="lnkSelect" runat="server" OnClick="btnViewMore_Click" Text="Select" />
                   </td>
               </tr>
           </ItemTemplate>

       </asp:ListView>
        </div>
        <div>
            ERROR
        <asp:ListView ID="ListView1" DataKeyNames="BookingId" OnItemCommand="lvLoads_OnItemCommand" runat="server">
            <EmptyDataTemplate>
                <table>
                    <tr>
                        <td>No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <GroupTemplate>
                <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
            </GroupTemplate>
            <ItemTemplate>
                <fieldset>
                    <table class="table table-striped table-responsive">
                        <thead>
                            <tr class="hidden-lg">
                                <th width="15%">Type</th>
                                <th width="31%">Details</th>
                                <th width="25%">Distance/Duration</th>
                                <th width="21%">&nbsp;</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td width="15%">
                                    <asp:Label CssClass="hide" ID="lblBokingId" runat="server"><%# Eval("BookingId") %></asp:Label>

                                    <asp:Label ID="lblLoadTypeName" runat="server"> <%# Eval("LoadTypeName") %></asp:Label>
                                    <br>
                                </td>
                                <td width="21%">
                                    <asp:LinkButton ID="btnViewMore" CssClass="btn btn-warning" OnClick="btnViewMore_Click1" runat="server">View More</asp:LinkButton>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </fieldset>
            </ItemTemplate>
        </asp:ListView>
        </div>
    </div>
</form>

Default2.aspx.cs:

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Class1 obj = new Class1();
        lvLoads.DataSource = obj.bind();
        lvLoads.DataBind();

        ListView1.DataSource = obj.bind();
        ListView1.DataBind();
    }
    protected void lvLoads_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {

    }

    protected void btnViewMore_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
        Label lblStatus = (Label)item.FindControl("lblLastName");
        string message = lblStatus.Text;
    }
    protected void btnViewMore_Click1(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        ListViewDataItem item = (ListViewDataItem)(sender as Control).NamingContainer;
        Label lblStatus = (Label)item.FindControl("lblLoadTypeName");
        string message = lblStatus.Text;
    }
}

Class1:

public class Class1
{
public int BookingId { get; set; }
public string LoadTypeName { get; set; }


public Class1()
{

}

public List<Class1> bind()
{
    List<Class1> data = new List<Class1>();
    Class1 obj = new Class1();
    obj.BookingId = 1;
    obj.LoadTypeName = "Type name1";
    data.Add(obj);
    obj = new Class1();
    obj.BookingId = 2;
    obj.LoadTypeName = "Type name2";
    data.Add(obj);
    obj = new Class1();
    obj.BookingId = 3;
    obj.LoadTypeName = "Type name3";
    data.Add(obj);
    obj = new Class1();
    obj.BookingId = 4;
    obj.LoadTypeName = "Type name4";
    data.Add(obj);
    return data;
}
}

As you can see in both list view there is only problem related with design which you are using in your list view. Please change design as per your requirement.

You are placing control in further other controls that's why id will change at run time.

hope it will help you.

Sunny
  • 3,185
  • 8
  • 34
  • 66