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