• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

CommandName of programmatically created button is not firing

Within a DetailsView I am creating a delete button programmatically and giving it a CommandName. The button gets created fine when I test my app, but when I click the delete button, nothing happens. If I create this exact same button with the same CommandName regularly in the DetailsView, it works perfectly and fires the ItemCommand of the DetailsView correctly.

Here is my code that programmatically creates the delete button, which doesn't work:

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

And here is the same button created regularly which works fine:

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

Someone suggested that I need to add an onclick event handler on every postback, but I can't figure out how to do that. Here is my modified code with the addition of the AddHandler:

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"
        AddHandler btnDelete.Command, AddressOf OnbtnDelete_Click
        pnlAttachments.Controls.Add(btnDelete)
    Catch ex As Exception
        'notify user on screen
        lblGeneralError.Text = ex.ToString
        lblGeneralError.CssClass = "red"
    End Try
End Sub

And then here is my OnbtnDelete_Click:

Private Sub OnbtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lblID As Label = dtlApplication.FindControl("lblID")
        Dim requestID As String = lblID.Text
        'refresh the notes panel
        GetNotes(requestID)
        'populate attachments panel
        GetAttachments(requestID)
End Sub

But it never gets fired when I click the btnDelete button. What am I doing wrong?
0
CitySec
Asked:
CitySec
1 Solution
 
strickddCommented:
What the other person was trying to convey is that .Net web development is stateless. That means, anything created on postback is destroyed before the next postback and therefore needs to be re-created every postback.

Simply put, if since you are dynamically creating this control in the GetAttachments() method, you will need to ensure that it is dynamically created every postback from there on out.

So in your Page_Load method, call GetAttachements() and it should work just fine. You may have to rearrange a little code to function how you need it to.

Alternatively, you can create the button in the .aspx file and merely set it's visibility and other attributes on a postback.q
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now