2

我在gridview中有文件上传控件,当我尝试更新gridview时,gridview在更新面板中,一切正常,但是来自fileupload的图像路径不保存请帮助我...

页面.aspx

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" BackColor="White" 
                                BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" CellSpacing="4" 
                                DataKeyNames="pid" ForeColor="Black" ShowHeaderWhenEmpty="True" 
                                GridLines="Horizontal" onrowediting="GridView1_RowEditing" 
                                onrowupdating="GridView1_RowUpdating" 
                                onrowcancelingedit="GridView1_RowCancelingEdit" 
                                onrowdeleting="GridView1_RowDeleting">
                                <Columns>
                                    <asp:TemplateField HeaderText="Operation">
                                        <EditItemTemplate>
                                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>&nbsp;
                                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" ForeColor="#94b52c"></asp:LinkButton>&nbsp;
                                            <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" ForeColor="#94b52c"
                                                            OnClientClick="return confirm('Are You Sure Want To Delete ?');"></asp:LinkButton>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        <ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Product ID" InsertVisible="False" SortExpression="pid">
                                        <EditItemTemplate>
                                            <asp:Label ID="lblpid" runat="server" Text='<%# Eval("pid") %>'></asp:Label>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblproductid" runat="server" Text='<%# Bind("pid") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        <ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Product Detail" SortExpression="pdetail">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblproductdetail" runat="server" Text='<%# Bind("pdetail") %>'></asp:Label>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        <ItemStyle Font-Bold="True" HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Product Image" SortExpression="pimage">
                                        <EditItemTemplate>
                                            <asp:FileUpload ID="FileUpload1" runat="server" />
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Image ID="imgproductimage" runat="server" ImageUrl='<%# Bind("pimage") %>' Height="50px" Width="50px"/>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                    </asp:TemplateField>

                                </Columns>
                                <FooterStyle BackColor="#F0F0F0" ForeColor="Black" />
                                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" VerticalAlign="Middle"  />
                                <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                                <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                                <SortedAscendingCellStyle BackColor="#F7F7F7" />
                                <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                                <SortedDescendingCellStyle BackColor="#E5E5E5" />
                                <SortedDescendingHeaderStyle BackColor="#242121" />
                            </asp:GridView>
    </ContentTemplate>
                        </asp:UpdatePanel>

这是.cs文件

 public void bindgrid()
{
    string qry = "select pid,pdetail,pimage from productdetail p,categorydetail c where p.cid=c.cid";
    GridView1.DataSource = abal.Qry_Fire(qry);
    GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    bindgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Label l = (Label)GridView1.Rows[e.RowIndex].FindControl("lblpid");
    TextBox txtproductdetail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtproductdetail");
    FileUpload f = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");
    string path = "~/user/product_image/" + f.FileName.ToString();
    int msg = abal.Qry_All("update productdetail set pdetail='" + txtproductdetail.Text + "',pimage='" + path 

+ "' WHERE pid='" + Convert.ToInt32(l.Text) + "'");
    if(msg==1)
        f.SaveAs(Server.MapPath(path));
    GridView1.EditIndex = -1;
    bindgrid();
}
4

5 回答 5

3

我遇到了同样的问题并使用以下解决方案解决了它。

在你的代码后面添加以下代码。

Page.Form.Attributes.Add("enctype", "multipart/form-data");

protected void gvLineItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {

    if (e.Row.RowState.Equals(DataControlRowState.Edit))
     {
       Button btnUpload = e.Row.FindControl("btnUpload") as Button;
       ScriptManager.GetCurrent(this).RegisterPostBackControl(btnUpload);
     }
  }
}

添加上述代码后,我可以检索 fileUpload 控件值。

protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
   FileUpload uploadedFile = (FileUpload)dgDocuments.Rows[editIndex].FindControl("UploadFile");
   if (uploadedFile.HasFile)
   {
    uploadedFile.SaveAs(FileUploadURL + "\\Temp\\" + uploadedFile.FileName);
   }
}
于 2013-07-02T04:38:58.023 回答
1

只需在后面PostBackTrigger添加 如下:</ContentTemplate>FileUploader

 </ContentTemplate>
  <Triggers>
  <asp:PostBackTrigger ControlID="FileUpload1" /> 
 </Triggers>
</asp:UpdatePanel> 

更新如果它在里面,gridview那么你可以试试下面的代码:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
 {  
   FileUpload flUpload = e.Row.FindControl("FileUpload1") as FileUpload;  
   ScriptManager.GetCurrent(this).RegisterPostBackControl(flUpload);  
 } 

Update2将事件添加OnRowDataBoundgridview

<asp:gridview
    id="GridView1" 
    onrowdatabound="GridView1_RowDataBound"

休息添加您的代码gridview

于 2013-02-21T09:00:42.930 回答
1

添加这个: Page.Form.Attributes.Add("enctype", "multipart/form-data"); 页面加载事件:

protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
             ..............................
         }

试试这个。

</ContentTemplate>
  <Triggers>
  <asp:PostBackTrigger ControlID="GridView1" /> 
 </Triggers>
</asp:UpdatePanel>
于 2015-01-27T04:11:44.270 回答
0

更新面板与 Fileupload 类似的某些控件无法正常工作。因此请尝试使用 ajax 调用,这是一个更好的选择。

于 2013-02-21T09:17:56.787 回答
0

只需添加 "Page.Form.Attributes.Add("enctype", "multipart/form-data");" 正如 Jisha Muthuswamy 所发布的。由 UpdatePanel 包裹的 gridview 内的 FileUpload 控件永远无法访问,因此无法设置为 PostBackTrigger ControlID。谢谢

于 2020-03-31T18:47:04.660 回答