Can I use an Eval () in the ItemCommand of a ListView?

2

What I want to do is a button that edits me by SQL , and I have that, but I need the ID of the row to edit (I extract it from with Eval("id") of listview1 ).

I have a ListView that within ItemTemplate there is a button ( btnSeguir ) with property commandName = "seguir" .

listview :

<asp:ListView
    ID="ListView1"
    runat="server"
    OnItemCommand="ListView1_ItemCommand"
    DataSourceID="SqlDataSource1"
>

Button in ItemTemplate of listview1 :

<asp:LinkButton
    ID="btnSeguir"
    CommandName="seguir"
    runat="server"
><%# siguiendo() %></asp:LinkButton>

In the ListView1_ItemCommand I want to call the function Eval("id") of ListView1 :

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "seguir")
    {
        ConsultasSQL c = new ConsultasSQL("XConnectionString");
        c.seguir(Eval("id")+"", Session["usrid"].ToString());
        ListView1.DataBind();
    }
}

But when I run it, when I hit the btnFollow button, I get this error:

  

System.InvalidOperationException: 'Data link methods such as Eval (), XPath () and Bind () can only be used in the context of a data link control.'

The strange thing is that I work with the siguiendo() method of the button, which also calls Eval("id") and if I extract the data, but in itemCommand no.

How do I solve this? Is there any other way to do this?

    
asked by Pablo Sanchez 23.12.2018 в 04:23
source

1 answer

1

I have already solved it. And the answer is No because ItemCommand is a protected method. I solved it on my own by discovering the CommandArgument property that uses it in the button and in the ItemCommand instead of using Eval ("id") use e.CommandArgument

<asp:LinkButton ID="btnSeguir" CommandName="seguir" CommandArgument='<%# Eval("id") %>' runat="server"><%# siguiendo() %></asp:LinkButton>
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "seguir")
    {
        ConsultasSQL c = new ConsultasSQL("XConnectionString");
        c.seguir(e.CommandArgument+"", Session["usrid"].ToString());
        ListView1.DataBind();
    }
}
    
answered by 23.12.2018 / 06:09
source