Asp.net如何在listview中添加链接按钮并执行一些编程

时间:2013-07-16 18:00:09

标签: asp.net c#-4.0 listview button linkbutton

我想在asp:listview控件中添加一个按钮或链接按钮,并且还想在我在asp.net中的listview控件中添加的rhat按钮的click事件上编码,请让我知道如何在asp中执行此操作。 net 4.0即时通讯使用c#4.0中的程序。我希望有些伙伴有一个简单的解决方案来完成这项任务。

谢谢

2 个答案:

答案 0 :(得分:6)

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*/
            }
        }

答案 1 :(得分:1)

以下是一个例子:

标记:

<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>

代码隐藏:

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);
    }
}