如何从列表视图Asp.Net获取价值

时间:2015-06-01 10:11:46

标签: asp.net listview listviewitem findcontrol

我在asp.net中有一个listview,如下所示:

<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>

现在点击按钮ViewMore我写了这段代码来提取listviewItem的值:

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

但是e.Item.FindControl("lblBookingId")总是为空。我使用chrome的inspect元素检查了页面的html,看到我的id以某种方式改为ContentSection_ctl00_lblLoadTypeName_0

我很震惊

我真的被这个困住了。请帮我。 Thanx in Advance

1 个答案:

答案 0 :(得分:0)

我已经对你的列表视图做了一些工作,并且我创建了一个类似于问题的工作。

<强> 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;
}
}

正如您在两个列表视图中所看到的,您只在列表视图中使用与设计相关的问题。请根据您的要求更改设计。

您将控件放在其他控件中,这就是id在运行时更改的原因。

希望它会对你有所帮助。