繁体   English   中英

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

[英]How do I programmatically create a button inside a DetailsView and give it a CommandName?

在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语句内添加几个删除按钮,而此时,我无法选择是否要定期或以编程方式创建它。

如果要触发,则需要在每个回发中添加onclick事件处理程序。 我的建议是让您在标记中使用visible属性将其声明为false,并在需要使其可见时才使其可见。 您将不必处理此类事情,并且除非将其设置为可见,否则控件将始终不会呈现。

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

请参考代码。 我可以在button1_Command中捕获按钮命令。 但是,您需要附加事件处理程序-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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM