Image upload via gridview question

Image upload via gridview question

梦中楼上月下 发布于 2021-11-29 字数 2297 浏览 890 回复 2 原文

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.

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2022-06-07 2 楼

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!

愿与i 2022-06-07 1 楼

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