如何以编程方式在DetailsView中创建一个按钮并为其指定一个CommandName?

时间:2011-10-20 15:02:59

标签: asp.net detailsview

在DetailsView中,我以编程方式创建一个删除按钮,并为其指定一个CommandName。当我测试我的应用程序时,按钮被创建正常,但是当我单击删除按钮时,没有任何反应。如果我在DetailsView中定期创建具有相同CommandName的完全相同的按钮,它将完美地工作并正确触发DetailsView的ItemCommand。这意味着我在代码中创建按钮的方式有问题,但我无法弄清楚它是什么。我是否需要指定UniqueID或类似的东西?

以下是我以编程方式创建删除按钮的代码,该按钮不起作用:

Public Sub GetAttachments(ByVal requestID As Integer)
    Try
        Dim pnlAttachments As Panel = dtlApplication.FindControl("pnlAttachments")
        Dim btnDelete As New LinkButton
        btnDelete.Text = "delete"
        btnDelete.CssClass = "lblDeleteAttachment"
        btnDelete.CommandName = "DeleteAttachment"
        btnDelete.ID = "lnkDeleteAttachment"
        pnlAttachments.Controls.Add(btnDelete)
    Catch ex As Exception
        'notify user on screen
        lblGeneralError.Text = ex.ToString
        lblGeneralError.CssClass = "red"
    End Try
End Sub  

如果我像这样定期创建按钮,它可以正常工作:

<asp:LinkButton runat="server" ID="lnkDeleteAttachment" Text="delete" commandname="DeleteAttachment" CssClass="lblDeleteAttachment"></asp:LinkButton>

以下是以编程方式创建的按钮的呈现页面输出:

<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>

以下是定期创建的按钮的渲染页面输出:

<a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')">delete</a>

你可以看到它们完全相同。

注意:我以编程方式创建它的原因是因为最终我要在For Next语句中添加几个删除按钮,此时我将无法选择是否定期创建它或以编程方式。

3 个答案:

答案 0 :(得分:1)

如果要触发,则需要在每次回发时添加onclick事件处理程序。我的建议是,在标记中将visible属性声明为false,并在需要时将其显示为可见。你不必处理这种事情,除非在某些时候设置为可见,否则控件将无法呈现。

答案 1 :(得分:0)

我可以更容易地在标记中创建按钮,然后以编程方式隐藏或显示它。

答案 2 :(得分:0)

请参阅代码。我可以在button1_Command中捕获button命令。但是,您需要附加事件处理程序 - button1.Command + = button1_Command;

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" OnItemCommand="DetailsView1_ItemCommand"
    OnDataBound="DetailsView1_DataBound" AllowPaging="true">
    <Fields>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Panel ID="Panel1" runat="server">
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

public class Customer
{
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

private List<Customer> _customers;

public List<Customer> Customers
{
    get
    {
        if (_customers == null)
        {
            _customers = new List<Customer>();

            _customers.Add(new Customer {CustomerId = 1, FirstName = "Jon", LastName = "Doe"});
            _customers.Add(new Customer {CustomerId = 2, FirstName = "Mary", LastName = "Doe"});
            _customers.Add(new Customer {CustomerId = 3, FirstName = "Brian", LastName = "Newton"});
        }
        return _customers;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    DetailsView1.DataSource = Customers;
    DetailsView1.DataBind();
}

protected void DetailsView1_DataBound(object sender, EventArgs e)
{
    Panel panel1 = DetailsView1.FindControl("Panel1") as Panel;
    Button button1 = new Button();
    button1.Text = "Delete";
    button1.Command += button1_Command;
    button1.CommandName = "Delete";
    panel1.Controls.Add(button1);
}

void button1_Command(object sender, CommandEventArgs e)
{
    // Delete data here
}