C# – Dynamic event handler for the ListView

.net-3.5asp.netcnet

It must be my special luck that today I stack with another problem and for second time coming here for help. And I really need help on this one.

I have a listView control on which i have a button control. I am trying to add event handler to this button, I googled a lot before coming to here, but examples i found either don't work for me, or too complex for my understanding as a beginner.

What I need is to add event handler to the button, so when the button is pressed some function (event handler) will be executed on the server side.

Here is the code of aspx
On the top i Have

EnableEventValidation="false"

Then the ListView itself:

List of all the books that are currently in the Library.

    <table width="630" cellpadding="2" cellspacing="5"/>
    <tr style="background-color:#DBE2E2;">
        <th>Image</th>
        <th>Book</th>
        <th>Description</th>

    </tr>
       <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </table>

    <p>
    <asp:DataPager ID="pageTopics" runat="server" PageSize="5">
        <Fields>
            <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />
            <asp:NumericPagerField />
            <asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" />
        </Fields>
    </asp:DataPager>
    </p>
</LayoutTemplate>
<ItemTemplate>
     <tr>
    <td valign="top"> <br />
        <asp:Image ID="book_img" ImageUrl='<%# Eval("book_img") %>' Height="100" Width="100" runat="server" />
    </td>

    <td valign="top"><p><b>Book Name: </b><asp:Label ID="book_name" runat="server" Text='<%# Eval("book_name") %>' /><br />
        <b>Author: </b><asp:Label ID="book_author" runat="server" Text='<%# Eval("book_author") %>' /><br />
    <p></td> 

    <td valign="top"><p><asp:Label ID="book_short_desc" runat="server" Text='<%# Eval("book_short_desc") %>' /><br />
        <b>Sku id: </b><asp:Label ID="book_squ" runat="server" Text='<%# Eval("book_squ") %>' /><br />
        <b>Arrived On: </b><asp:Label ID="book_arrived" runat="server" Text='<%# Eval("book_arrived") %>' />
        <asp:HyperLink ID="bookDesc" runat="server" NavigateUrl='<%# "BookDesc.aspx?id=" + Eval("book_id") %>' Text="To read more..." />
        <asp:Button ID="orderbook" runat="server" CommandName="order" CommandArgument="ordernow" Text="order now" />
    </p></td>

    </tr>
</ItemTemplate>

And in the CodeBehind I try to execute following code:

override protected void OnInit(EventArgs e)
{
        base.OnInit(e);
        book_list.ItemCommand += new EventHandler<ListViewCommandEventArgs>(book_list_ItemCommand);
}


void book_list_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    testLabel.Text = e.CommandArgument.ToString();
    //throw new NotImplementedException();
}

However when i debug the book_list_ItemCommand doesn't executes.

I really need help on this one.

Thank you in advance.

Best Answer

After breaking my head over the wall, I actually found a way. I will post it here, so maybe somebody with the similar problem will find solution for himself.

That is my button that located in the ListView

<asp:Button ID="orderbook" runat="server" CommandName="order" CommandArgument="ordernow" OnCommand="Order_Command" Text="order now" />

And in code behind i added

protected void Order_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "order")
        {
            testLabel.Text = e.CommandName.ToString();
        }
    }

And now when i click on the button, it catches the event.