Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net How to add a linkbutton in listview and do some progrimmg

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

like image 316
Vikram Avatar asked Feb 15 '23 22:02

Vikram


2 Answers

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*/
            }
        }
like image 113
Markus Fantone Avatar answered May 03 '23 06:05

Markus Fantone


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);
    }
}  
like image 29
Karl Anderson Avatar answered May 03 '23 06:05

Karl Anderson