4

I want to add a button or linkbutton in asp:listview control and also want to code on click event of rhat button which i did add in listview control in asp.net please let me know how can i do this in asp.net 4.0 im using a program in c# 4.0. I hope some buddy have a simple solution for do this task.

Thank you

Vikram
  • 489
  • 3
  • 8
  • 18
  • 1
    Unfortunately WebForms makes this unnecessarily difficult. The actual click event of the button isn't available, you'll need to use the `ItemCommand` event of the `ListView` and check which button send the command. A Google search for `asp.net linkbutton listview" finds a lot. – David Jul 16 '13 at 18:06
  • 1
    Could you please google first before asking a question in SO? There are hundreds of examples in google if not thousands. http://weblogs.asp.net/jeffwids/archive/2009/12/08/handle-the-button-click-event-from-an-asp-net-listview-control.aspx – Win Jul 16 '13 at 18:06

2 Answers2

7

ASPX:

<asp:ListView runat="server">
        <LayoutTemplate>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Foo</th>
                        <th>Bar</th>
                        <th>&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
                </tbody>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("ID") %></td>
                <td><%# Eval("Foo") %></td>
                <td><%# Eval("Bar") %></td>
                <td><asp:LinkButton Text="Some Text" ID="lkbUniqueAction" OnClick="lkbUniqueAction_Click" runat="server" /></td>
                <td><asp:LinkButton Text="Some Other Text" ID="lkbCommandAction" CommandArgument='<%# Eval("ID") %>' OnCommand="lkbCommandAction_Command" runat="server" /></td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

C#

        protected void lkbUniqueAction_Click(object sender, EventArgs e) 
        {
            /*TODO*/
        }

        protected void lkbCommandAction_Command(object sender, CommandEventArgs e) 
        {
            if (e.CommandArgument == null)
            {
                /*TODO*/
            }
            else 
            {
                /*TODO*/
            }
        }
Markus Fantone
  • 341
  • 1
  • 8
1

Here is an example:

Markup:

<asp:GridView ID="gridMembersList" AutoGenerateColumns="False" GridLines="None" 
        runat="server" onrowcommand="gridMembersList_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="User Name">
            <ItemTemplate>
                <asp:Literal ID="ltrlName" runat="server" Text='<%# Eval("Name") %>'>    </asp:Literal>
                <asp:Literal ID="ltrlSlno" runat="server" Visible="False" Text='<%# Eval("Id") %>'></asp:Literal>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="View More">
             <ItemTemplate>
                  <asp:Button ID="btnViewmore" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="More" runat="server" Text="View More" />
             </ItemTemplate>
         </asp:TemplateField> 
    </Columns>
</asp:GridView>

Code-behind:

protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "More")
    {
        int index = Convert.ToInt32(e.CommandArgument.ToString());
        Literal ltrlslno = (Literal)gridMembersList.Rows[index].FindControl("ltrlSlno");
        Literal ltrlName = (Literal)gridMembersList.Rows[index].FindControl("ltrlName");
        ScriptManager.RegisterStartupScript(this, this.GetType(), 
        "Message", "alert('" + ltrlName.Text+ "');", true);
    }
}  
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 1
    Thank you brother. I wasted a lot of time checking (With Response.write()) if Argument is sending without success. After ready you Answer. your post make me remember that we can use javascript to check it. Everything was ok in my code but Response.write was sending me an empty string. after using this javascript code but with ListView and the answer of @Markus Fantone, my live change !!!! I upvoted you just for this javascript code ! – Tchaps Jun 08 '14 at 07:12