Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating ASP.NET Controls code-behind
    text
    copied!<p>In a gridview, I have a column I use for images, using a template:</p> <pre><code>&lt;asp:TemplateField HeaderText="Image"&gt; &lt;EditItemTemplate&gt; &lt;%#GenerateControl(Eval("id").toString) %&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;%#GetImageById(Eval("id"))%&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; </code></pre> <p>I want generate a control, depending on wether the image exists or not. The code I use for doing this in the code behind is:</p> <pre><code>Protected Function GenerateControl(ByVal id As String) As String Dim scrString As String = "../../Assets/Zalen/" + id + ".jpg" Dim strImageUrl As String = Me.Server.MapPath(scrString) If Not File.Exists(strImageUrl) Then Return "&lt;asp:FileUpload ID=""FileUpload1"" runat=""server"" /&gt;" Else Return "&lt;asp:button ID=""Button1"" runat=""server"" Text=""Verwijder afbeelding"" OnClick=""DeleteImage(" + id + ")""/&gt;" End If End Function </code></pre> <p>This is method is invoked by the GridView.Updating event.</p> <p>So, basicly: If the image exists I want to show a button so that the user is able to delete the image. If the image does not yet exist (images are based on the primary key of the table ("id")) then I want to show a FileUpload control.</p> <p>The controls show up in the HTML code when I run the website, but they do not show up on the website itself. (ie: visible through code, not visually)</p> <p>How do I correctly render controls dynamicly into a GridView? Thanks</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