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">
    <LayoutTemplate>
        <table class="table table-hover">
            <thead>
                <th></th>
                <th>Title</th>
                <th>Size</th>
                <th></th>
            </thead>
            <tbody id="itemContainer" runat="server" >

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

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)
        {
            FillData();
        }
    }

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

    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

Annunci

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.

Rispondi

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

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. 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...