如何在Page_Load Asp.net中使用ListViewItemEventArgs调用方法

时间:2017-05-24 12:48:13

标签: c# asp.net listview

我正在开发一个asp.net应用程序,我需要在Page_Load上加载一个列表视图。我想我找到了解决方案,但我无法解决seco​​nt参数ListViewItemEventArgs的问题:如何创建它并直接在页面加载中使用该方法。

我的代码

 namespace X.Views
{
    public partial class FAQ : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
         public void lvp(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {                   
                Label content = (Label)e.Item.FindControl("positionContent");
                System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;


                    content.Text = "Hello";

            }
}

和我的listView:

 <asp:ListView ID="listFAQ" runat="server" ClientIDMode="Static">
        <LayoutTemplate>
        <table cellpadding="2" runat="server" id="tblFAQ">

            <tr runat="server" id="itemPlaceholder">
            </tr>
             </table>
            <asp:DataPager runat="server" ID="DataPager" PageSize="3">
      <Fields>
        <asp:NumericPagerField
          ButtonCount="5"
          PreviousPageText="<--"
          NextPageText="-->" />
      </Fields>
    </asp:DataPager>
            </LayoutTemplate>
        <ItemTemplate>
            <tr runat ="server">
                <td valign="top" colspan="2" align="center" >
                <asp:Label ID="lblposition" Text="position" runat="server" />
                <asp:Label ID="lblPositionText" runat="server" Text='<%#Eval("positionContent")%>' />
                <asp:Label ID="lblLibelle" Text="libelleContent" runat="server" />
                <asp:Label ID="lblLibelleText" runat="server" Text='<%#Eval("libelleContent")%>' />  
                 <asp:Label ID="lblDataContent" Text="dataContent" runat="server" />
                 <asp:Label ID="lblDataContentText" runat="server" Text='<%#Eval("dataContent")%>' />                            
                </td>
            </tr>

        </ItemTemplate>

    </asp:ListView>

1 个答案:

答案 0 :(得分:1)

您无需在page_load中调用此方法。只需在列表视图标记中添加OnItemDataBound="lvp"即可。

<asp:ListView ID="listFAQ" runat="server" ClientIDMode="Static" OnItemDataBound="lvp">

<强>更新 还有一件事。将方法lvp保留在calss FAQ中。现在在外面。

namespace X.Views
{
    public partial class FAQ : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void lvp(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {                   
                Label content = (Label)e.Item.FindControl("positionContent");
                System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;


                    content.Text = "Hello";

            }
        }
}