ASP.Net ListView call method or pass row object

Anyone knows how to bind a property in a list view, but someone doesn’t knows that it’s possible to call a method with a property value or a method with row object as parameter

<asp:ListView ID="ListViewResult" runat="server" ItemPlaceholderID="itemContainer">
        <table class="table table-hover">
            <tbody id="itemContainer" runat="server" >

                <asp:Literal ID="Literal1" runat="server" Text='<%# GetNumber(Container.DataItemIndex)%>' ></asp:Literal>
                <asp:Literal ID="Literal2" runat="server" Text='<%# Eval("RoomTitle")%>' ></asp:Literal>
                <asp:Literal ID="Literal3" runat="server" Text='<%# GetRoomSize(Container.DataItem)%>' ></asp:Literal>
            <td class="cs cell-buttons" onclick='<%# GenDeleteRow(Eval("IdRoom")) %>'>
                <span class="glyphicon glyphicon-remove cs remove"></span>
            No room available

As you can see I create two td with the RoomTitle binded as usual.
But if you look on the next

I Bind onclick with a method evalutaion.
I call the method GenDeleteRow and I pass the IdRoom Property
Here the code behind

public partial class RoomMng : System.Web.UI.Page
    public const string EditPage = "~/Edit/Room/";

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

    private void FillData()
        using (Models.DBDataContext db = new Models.DBDataContext())
            ListViewResult.DataSource = db.Salas.Where((item) => item.Stato == null); ;

    public string GenDeleteRow(object id)
        return "return deleteRow(this, " + id.ToString() + ")";

    public string GetNumber(int index)
        return (index + 1).ToString();

    public string GetRoomSize(object obj)
        var objCasted = obj as Room;
        var area = objCasted.Width * objCasted.Height;
        return area.ToString() + "m2";

Focus on GenDeleteRow, it take the property passed and do some logic.
GetNumber instead take the index of item passwd by Container.DataItemIndex.
To get the entire object of the row use Container.DataItem and pass it to a method.
The method can cast to the right object and access to multiple properties.

Another method to add some logics to object property is to add other property by creating a partial class to extend the original one.
I told about this solution in this post:
ASP.Net ListView call javascript, css class


Informazioni su Andrea Regoli

Project Manager .Net Developer WPF WP7 Asp.Net c# javascript ajax SQL sharepoint
Questa voce è stata pubblicata in .Net, Asp.Net, c# e contrassegnata con , , , , , , . Contrassegna il permalink.


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...