我在列表视图的编辑模板中使用文件上传器来更新列表视图。我正在使用项目更新来处理列表视图的更新。然而,它在cmd.executeonquery()处抛出了错误。有人能在这方面帮助我吗。
<EditItemTemplate>
<tr style="background-color:#008A8C;color: #FFFFFF;">
<td>
<asp:Button ID="UpdateButton" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="productidLabel1" runat="server" Text='<%# Eval("productid") %>' />
</td>
<td>
<asp:TextBox ID="productnameTextBox" runat="server" Text='<%# Bind("productname") %>' />
</td>
<td>
<asp:TextBox ID="productdescTextBox" runat="server" Text='<%# Bind("productdesc") %>' />
</td>
<td>
<asp:TextBox runat="server" ID="productpriceTextBox" Text='<%# Bind("productprice") %>'> </asp:TextBox>
</td>
<td>
<asp:DropDownList SelectedValue='<%# Bind("productcateg") %>' runat="server" ID="dropdownlist" style="overflow:auto" CssClass="dropdown">
<asp:ListItem Enabled="true" Text="Select Category" Value="-1"></asp:ListItem>
<asp:ListItem Text="Watch" Value="Watch"> </asp:ListItem>
<asp:ListItem Text="Hand Bags" Value="handbag"></asp:ListItem>
<asp:ListItem Text="Television" Value="television"></asp:ListItem>
<asp:ListItem Text="Books" Value="book"></asp:ListItem>
<asp:ListItem Text="Accessories" Value="accessories"></asp:ListItem>
<asp:ListItem Text="Cars" Value="car"></asp:ListItem>
<asp:ListItem Value="bike"></asp:ListItem>
<asp:ListItem Text="Bikes" value="shoe"></asp:ListItem>
<asp:ListItem Text="Shoes" Value="garment"> </asp:ListItem>
<asp:ListItem Text="Garments" Value="cellphone"></asp:ListItem>
<asp:ListItem Text="Laptops" Value="laptop"></asp:ListItem>
<asp:ListItem Text="Home & Appliances" Value="homeappliance"></asp:ListItem>
<asp:ListItem Text="Perfumes" Value="perfume"></asp:ListItem>
<asp:ListItem Text="Sports" Value="sports"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:Button runat="server" ID="btnupload" OnClick="btnupload_Click" Text="Upload" />
</td>
</tr>
</EditItemTemplate>
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
DropDownList dropdownlist = ListView1.Items[e.ItemIndex].FindControl("dropdownlist") as DropDownList;
string category = dropdownlist.SelectedValue;
FileUpload fileUpload1 = ListView1.Items[e.ItemIndex].FindControl("FileUpload2") as FileUpload;
System.Drawing.Bitmap bmpPostedImage = new System.Drawing.Bitmap(fileUpload1.FileContent);
System.Drawing.Image objImage = ScaleImage(bmpPostedImage, 200);
var stream = new System.IO.MemoryStream();
objImage.Save(stream, ImageFormat.Png);
stream.Position = 0;
BinaryReader br = new BinaryReader(stream);
byte[] imagebytes = br.ReadBytes((Int32)stream.Length);
string base64String = Convert.ToBase64String(imagebytes, 0, imagebytes.Length);
string url = "data:image/png;base64," + base64String;
TextBox productname = ListView1.Items[e.ItemIndex].FindControl("productnameTextBox") as TextBox;
TextBox productprice = ListView1.Items[e.ItemIndex].FindControl("productpriceTextBox") as TextBox;
TextBox productdesc = ListView1.Items[e.ItemIndex].FindControl("productdescTextBox") as TextBox;
Label productid = ListView1.Items[e.ItemIndex].FindControl("productidLabel1") as Label;
con.Open();
string query = "UPDATE products SET productname=" + productname.Text + "," + "productdesc=" + productdesc.Text + "," + "productprice=" + productprice.Text + "," + "productcateg=" + category + "," + "productimage=" + url + "WHERE productid=" + productid.Text;
cmd = new SqlCommand("UPDATE products SET productname =" + productname.Text + "," + "productdesc =" + productdesc.Text + "," + "productprice =" + productprice.Text + "," + "productcateg =" + category + "," + "productimage =" + url + "WHERE productid =" + productid.Text,con);
cmd.ExecuteNonQuery();
con.Close();
}
public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight) //Image Resize
{
var ratio = (double)maxHeight / image.Height;
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
好的,我已经找到了添加e.NewValues的方法。add("columnname",url)和更新将反映在列表视图中