2

In a gridview, I have a column I use for images, using a template:

<asp:TemplateField HeaderText="Image">        

                <EditItemTemplate>
                <%#GenerateControl(Eval("id").toString) %>
                </EditItemTemplate>

                <ItemTemplate>
                    <%#GetImageById(Eval("id"))%>
                </ItemTemplate>
            </asp:TemplateField>

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:

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 "<asp:FileUpload ID=""FileUpload1"" runat=""server"" />"
      Else
        Return "<asp:button ID=""Button1"" runat=""server"" Text=""Verwijder afbeelding"" OnClick=""DeleteImage(" + id + ")""/>"
      End If
    End Function

This is method is invoked by the GridView.Updating event.

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.

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)

How do I correctly render controls dynamicly into a GridView? Thanks

3 Answers 3

2

Here's how I would do it:

<asp:TemplateField HeaderText="Image">        
    <EditItemTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" Visible='<%#GenerateControl(Eval("id").ToString()) %>' />
        <asp:button ID="Button1" runat="server" Text="Verwijder afbeelding" CommandArgument='<%#Eval("id").ToString() %>' CommandName="DeleteImage" Visible='<%#GenerateControl(Eval("id").ToString()) %>' OnClick="DeleteImage" />
    </EditItemTemplate>

    <ItemTemplate>
        <%#GetImageById(Eval("id"))%>
    </ItemTemplate>
</asp:TemplateField>

Then in the code behind:

Protected Function GenerateControl(ByVal id As String) As Boolean
      Dim scrString As String = "../../Assets/Zalen/" + id + ".jpg"
      Dim strImageUrl As String = Me.Server.MapPath(scrString)
      Return File.Exists(strImageUrl) 
End Function

Note: I have assumed that you are using a code behind method to perform the delete, so I chose to include CommandArgument and CommandName. This makes the OnClick assignment unnecessary since you would handle these 2 properties in the OnRowCommand event. Though the property of CommandArgument could still be used in the OnClick and would be meaningful.

I've found it easier when trying to decide which control to use just to use them both and turn one "off" if I don't need i. This is not terribly efficient since it still goes through the computational process up until render, but this is usually negligible and it never makes it out to the html.

Sign up to request clarification or add additional context in comments.

1 Comment

This might fix my problem. Thank you very much.
0

If the controls are to be in EditItemTemplate or ItemTemplate, you don't need to generate then dynamically. Place them statically and depending upon if the row is in edit mode or item mode, the correct control would display.

<asp:TemplateField HeaderText="Image">        
    <EditItemTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" />
    </EditItemTemplate>

    <ItemTemplate>
        <asp:button ID="Button1" runat="server" Text="Verwijder afbeelding"/>
    </ItemTemplate>
</asp:TemplateField>

The above code would display fileupload in edit mode, simple button otherwise.

Comments

0

First, here's WHY you're not seeing them visually: The web browser has no idea what to do with an <asp:FileUpload> tag. if you put that in your ASPX directly, and run it, you'll notice that in the actual html source the browser sees it as an <input type="file">. All of the .NET server controls work this way - they send html to the browser. (See here: http://support.microsoft.com/kb/306459)

At any rate, there's an article here on properly adding controls in code-behind: http://www.c-sharpcorner.com/UploadFile/sd_patel/DynamicallyCreateASPNETControls11232005020626AM/DynamicallyCreateASPNETControls.aspx

Update

This is a much simpler article than the one I linked to above: http://learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.