所以我是asp.net的新手。我有这个大学项目,需要使用更新功能。我做了更新功能,它的工作正常,除了图片列之外的所有列。它不更新图片数据库,并在gridview中显示上一张图片。文件夹中的图片已经更新,但gridview只显示上一张图片(不是更新的图片)。我需要你的帮助,谢谢你:)
这是gridview aspx代码,如果点击了更新按钮,它将重定向到另一个页面。
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Images" ItemStyle-Height="50px" ItemStyle-Width="50px" >
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("pic") %>'/>/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="nama" HeaderText="nama" />
<asp:BoundField DataField="harga" HeaderText="harga" />
<asp:BoundField DataField="stok" HeaderText="stok" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" wID='<%# Eval("Id") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
这是Page_Load函数
public List<Table> getAll()
{
List<Table> watches = (from x in de.Tables select x).ToList();
return watches;
}
public void loadData()
{
GridView1.DataSource = repo.getAll();
GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) loadData();
}
并重定向到更新aspx
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:FileUpload ID="pic" runat="server" />
<br />
<asp:TextBox ID="name" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="price" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="stok" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnUpdate" runat="server" Text="Button" OnClick="btnUpdate_Click" />
这是更新aspx中btnUpdate的更新功能
protected void btnUpdate_Click(object sender, EventArgs e)
{
string ext = System.IO.Path.GetExtension(pic.FileName);
string nama = name.Text;
int harga = Int32.Parse(price.Text);
int stock = Int32.Parse(stok.Text);
Table update = (Table)Session["updateWatch"];
string pict = "..\\Pictures\\Watch Pictures\\" + update.Id + ext;
bool updateStatus = repo.update(update.Id, pict, nama, harga, stock);
if (updateStatus == false) Label1.Text = "fail";
else
{
string subPic = update.pic.Substring(3);
string path = Server.MapPath("~") + subPic;
string savepath = Server.MapPath("~/Pictures/Watch Pictures/");
System.IO.File.Delete(path);
pic.SaveAs(savepath + update.Id + ext);
Response.Redirect("WebForm1.aspx");
}
}
这是存储库中的更新功能
public bool update(int id, string pic, string nama, int harga, int stok)
{
Table updateW = (from x in de.Tables where x.Id == id select x).FirstOrDefault();
updateW.pic = pic;
updateW.nama = nama;
updateW.harga = harga;
updateW.stok = stok;
de.SaveChanges();
if (updateW == null) return false;
return true;
}
这是gridview数据源的代码
public List<Table> getAll()
{
List<Table> watches = (from x in de.Tables select x).ToList();
return watches;
}