Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I programmatically create a button inside a DetailsView and give it a CommandName?
    text
    copied!<p>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. That means there is something wrong with the way I'm creating my button in the code, but I can't figure out what it is. Do I need to specify a UniqueID or something like that? </p> <p>Here is my code that programmatically creates the delete button, which doesn't work:</p> <pre><code>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 </code></pre> <p>And if I create the button regularly like this, it works fine:</p> <pre><code>&lt;asp:LinkButton runat="server" ID="lnkDeleteAttachment" Text="delete" commandname="DeleteAttachment" CssClass="lblDeleteAttachment"&gt;&lt;/asp:LinkButton&gt; </code></pre> <p>Here is the rendered page output for the programmatically created button that doesn't work:</p> <pre><code>&lt;a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')"&gt;delete&lt;/a&gt; </code></pre> <p>And here is the rendered page output for the regularly created button that works:</p> <pre><code>&lt;a id="MainContent_dtlApplication_lnkDeleteAttachment" href="javascript:__doPostBack('ctl00$MainContent$dtlApplication$lnkDeleteAttachment','')"&gt;delete&lt;/a&gt; </code></pre> <p>You can see they are identical.</p> <p>Note: The reason I'm creating it programmatically is because eventually I'm going to add several delete buttons inside a For Next statement, and at this point I won't have a choice of whether or not to create it regularly or programmatically.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload