Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从listview Asp.Net获取值_Asp.net_Listview_Listviewitem_Findcontrol - Fatal编程技术网

如何从listview Asp.Net获取值

如何从listview Asp.Net获取值,asp.net,listview,listviewitem,findcontrol,Asp.net,Listview,Listviewitem,Findcontrol,我在asp.net中有一个列表视图,如下所示: <asp:ListView ID="lvLoads" DataKeyNames="BookingId" OnItemCommand="lvLoads_OnItemCommand" runat="server"> <EmptyDataTemplate> <table> <tr>

我在asp.net中有一个列表视图,如下所示:

<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>
但是
e.Item.FindControl(“lblBookingId”)
总是为空。我使用chrome的inspect元素检查了页面的html,我震惊地发现我的id不知何故被更改为
ContentSection\u ctl00\u lblLoadTypeName\u 0


我真的被这件事难住了。请帮帮我。Thanx预先

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

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>
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;
    }
}
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;
}
}
Class1:

<%@ 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>
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;
    }
}
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;
}
}
公共类1
{
public int BookingId{get;set;}
公共字符串LoadTypeName{get;set;}
公共类别1()
{
}
公共列表绑定()
{
列表数据=新列表();
Class1 obj=新的Class1();
obj.BookingId=1;
obj.LoadTypeName=“类型名称1”;
添加数据(obj);
obj=新类别1();
obj.BookingId=2;
obj.LoadTypeName=“类型名称2”;
添加数据(obj);
obj=新类别1();
obj.BookingId=3;
obj.LoadTypeName=“类型名称3”;
添加数据(obj);
obj=新类别1();
obj.BookingId=4;
obj.LoadTypeName=“类型名称4”;
添加数据(obj);
返回数据;
}
}
正如您在两个列表视图中看到的,只有与您在列表视图中使用的设计相关的问题。请根据您的要求更改设计

您正在将控件放置在其他控件中,这就是为什么id在运行时会更改的原因


希望它能帮助你

我们也能有c代码吗?你说的其他进一步控制是什么意思