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
ASPX:
<asp:ListView runat="server">
<LayoutTemplate>
<table>
<thead>
<tr>
<th>ID</th>
<th>Foo</th>
<th>Bar</th>
<th> </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*/
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With