gpt4 book ai didi

asp.net - fileupload 在更新面板内的 gridview 中不起作用

转载 作者:行者123 更新时间:2023-12-02 22:12:39 26 4
gpt4 key购买 nike

我在 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();
}

最佳答案

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

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

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);
}
}

关于asp.net - fileupload 在更新面板内的 gridview 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14997821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com