简体   繁体   中英

How to call a method with ListViewItemEventArgs In Page_Load Asp.net

I'm working on an asp.net app and I need to load a list view on Page_Load. I think I've found the solution but I cann't solve the problem of the secont argument ListViewItemEventArgs: How to create that and use the method directly in the page load.

My code

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

            }
}

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

You don't need to call this method in page_load. Just add OnItemDataBound="lvp" in list view mark up.

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

Update: one more thing. Keep method lvp inside calss FAQ . It is outside at the moment.

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

            }
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM