views:

611

answers:

2

Hello, I have a gridview that displays data from a database. The datbase stores the image filename (string) among other things for an item.

I have the fileupload control showing in the EDIT view, and that uploads the file just fine.

The problem is, I want to update the image filename in the database and I am not sure how to get the data to the textbox control that the gridview uses to UPDATE the database. The textbox control i have set the visibility to hidden. here is the aspx code:

    <asp:TemplateField HeaderText="Image" SortExpression="Image">
                <EditItemTemplate>
                    <asp:TextBox ID="txtImage" runat="server" Text='<%# Bind("Image") %>' Visible="False" OnTextChanged="txtImage_TextChanged"></asp:TextBox>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>                        
                    <asp:Image ID="Image1" runat="server" Width="50px" AlternateText='<%# Eval("Image") %>' ImageUrl='<%# "images/hardware/" + Eval("Image") %>' />
                </ItemTemplate>
            </asp:TemplateField>

And here is the function that stores the file onto the server, and places the filename into a variable:

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        FileUpload fileUpload = row.Cells[0].FindControl("FileUpload1") as FileUpload;
         if ( fileUpload != null && fileUpload.HasFile)
         {
             strFileName = fileUpload.FileName;
             fileUpload.SaveAs(Server.MapPath("images/hardware/" + strFileName));
             TextBox txtImage = row.Cells[0].FindControl("txtImage") as TextBox;
             txtImage.Text = strFileName;
         }
    }

So where do I go from here? I think the update has occured already or something? Because the "txtImage.Text" does not update the database... Am I out of order here or something? I can manipulate values in the textbox in this function before it gets saved to the DB right? Thanks for your help on this one.

+1  A: 

If you handle updating the database in the code behind you should be able to get the file name like you did in GridView1_RowUpdating. Just find the row in the grid that is in edit mode. Each row in the rows collection on the grid has a RowState property...your looking for DataControlRowState.Edit

Jon
Not really updating in the code behind I don't think... I am using the "UpdateQuery" property of the dataset to handle the update.
Kolten
Your answer below looks good, when you are not able to two way bind elements adding their values to the update collection by hooking a data source event is the way to go
Jon
A: 

I think I have a solution, but would like input into whether this is a good way of coding it. I have this setup as the "Updating" event for the dataset the gridview uses:

        protected void SetRecords(object sender, SqlDataSourceCommandEventArgs e)
    {
        if (strFileName != "")
        {
            e.Command.Parameters["@Image"].Value = strFileName;
        }
    }

Is this ok? Everything seems to work just fine... but is this a good way to solve the problem? I would like to code properly and not start bad coding practices right from the start (I am an accomplished classic ASP developer moving to C# and ASP.NET) Thanks all!

Kolten