locked
DetailsView and FileUpload RRS feed

  • Question

  • User59162070 posted

    Hi,

    I have a DetailsView that has three templated fields. One of these fields is an Image server control that gets its ImageURL from corresponding DataField in database. I have placed a FileUpload control for this field(image) in the EditItemTemplate and want to that the end-user can change the picture at run time. How can i accomplish this?

    My HTML is as bellow:

    <asp:TemplateField HeaderText="Pic:">
                                    <ItemTemplate>
                                        <asp:Image ID="Image2" runat="server" Width="85px" Height="128px" ImageUrl='<%#Bind("PictureURL") %>'></asp:Image>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:FileUpload ID="FileUpload2" runat="server" />
                                    </EditItemTemplate>
                                </asp:TemplateField>

    Friday, October 16, 2009 8:17 AM

Answers

  • User1123702521 posted

    FileUpload control doesn't expose a property some thing like File etc... so that the underlying datasource could automatically understand that its a file and it has to be inserted in a byte[] kind of field...

     

    You've correctly placed the FileUpload control in the EditItemTemplate of the DetailsView... Now please capture the OnItemUpdating event of the detailsView and finde the FileUpload control in it. Like this

     

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            FileUpload fileUpload1 = DetailsView1.FindControl("FileUpload2") as FileUpload;
            if (fileUpload1 != null)
            {
                //..your code here..
            }
        }


     

     

    Now manipulate  the control the way you want (if you are storing the files in database, then readout the bytes and assign them to that particular field, if you want to store the files in filesystem then save them and store its name in database...)

     

    Hope it helps.

    Thanks.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 8:49 AM

All replies

  • User1123702521 posted

    FileUpload control doesn't expose a property some thing like File etc... so that the underlying datasource could automatically understand that its a file and it has to be inserted in a byte[] kind of field...

     

    You've correctly placed the FileUpload control in the EditItemTemplate of the DetailsView... Now please capture the OnItemUpdating event of the detailsView and finde the FileUpload control in it. Like this

     

        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            FileUpload fileUpload1 = DetailsView1.FindControl("FileUpload2") as FileUpload;
            if (fileUpload1 != null)
            {
                //..your code here..
            }
        }


     

     

    Now manipulate  the control the way you want (if you are storing the files in database, then readout the bytes and assign them to that particular field, if you want to store the files in filesystem then save them and store its name in database...)

     

    Hope it helps.

    Thanks.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 8:49 AM
  • User1759999623 posted

    The issue with this is that the FileUpload is placing the image file on your server and does not have any functionality to place it in your database.  One option would be if you access your web server from your database, then when you update/insert your data record from the DetailsView, use a stored procedure that will pick up the image file and put it in the database. Unfortunately, you may have an issue with the postback caused by the FileUpload control, so this may not be feasible.

    Another option would be to make the FileUpload control independent, outside of the DetailsView and allow for the user to update it whenever.  In this scenario, you could create your own control that uploads the image to a BinaryStream and then add the BinaryStream to your database.  Under this scenario, it would be best done where the record already exists so you only need to perform an update with the image.

    Friday, October 16, 2009 8:56 AM