4

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

谢谢

4

2 回答 2

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*/
            }
        }
于 2013-07-16T18:10:38.323 回答
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);
    }
}  
于 2013-07-16T18:13:38.237 回答