无法更新更新页中的数据
本文关键字:更新 数据 | 更新日期: 2023-09-27 18:05:28
我正在通过发送gridview值到其他页面来更新gridview值。在这里,我只能更新类别下拉列表值和图像文件。我无法更新文本框值。
这是我的代码。
更新产品代码:
protected void SubmitButton_Click(object sender, EventArgs e)
{
int productId = Convert.ToInt32(Request.QueryString["ProductID"]);
Product product = new Product();
product.ProductID = productId;
product.ProductName = TitleTextBox.Text;
product.Description = DescriptionTextBox.Text;
product.ItemsInSet = Convert.ToInt32(SetTextBox.Text);
product.UnitPriceOwner = Convert.ToInt32(UnitResellerTextBox.Text);
product.UnitPriceReseller = Convert.ToInt32(UnitResellerTextBox.Text);
product.ShippingCost = Convert.ToInt32(ShipmentTextBox.Text);
product.CategoryID = Convert.ToInt32(CategoryDropDownList.SelectedValue.ToString());
product.Visible = true;
product.InOffer = false;
if (ImageUpload.HasFile)
{
int length = ImageUpload.PostedFile.ContentLength;
product.ProductImage = new byte[length];
ImageUpload.PostedFile.InputStream.Read(product.ProductImage, 0, length);
}
else
{
Byte[] image;
image = ProductBL.GetImage(productId);
product.ProductImage = image;
}
ProductBL.UpdateProduct(product);
MessageLabel.Text = "Product successfully Updated!";
}
UpdateProduct()代码:
public static void UpdateProduct(Product product)
{
string query = "UPDATE [Products] SET [ProductName] = @ProductName, [Description] = @Description, [ItemsInSet] = @ItemsInSet, " +
"[UnitPriceOwner] = @UnitPriceOwner, [UnitPriceReseller] = @UnitPriceReseller, [CategoryID] = @CategoryID, " +
"[ShippingCost] = @ShippingCost, [InOffer] = @InOffer, [ProductImage] = @ProductImage, [Visible] = @Visible WHERE [ProductID] = @ProductID";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@ProductName", SqlDbType.Text).Value = product.ProductName;
cmd.Parameters.AddWithValue("@Description", SqlDbType.Text).Value = product.Description;
cmd.Parameters.AddWithValue("@ItemsInSet", SqlDbType.Int).Value = product.ItemsInSet;
cmd.Parameters.AddWithValue("@UnitPriceOwner", SqlDbType.Int).Value = product.UnitPriceOwner;
cmd.Parameters.AddWithValue("@UnitPriceReseller", SqlDbType.Int).Value = product.UnitPriceReseller;
cmd.Parameters.AddWithValue("@CategoryID", SqlDbType.Int).Value = product.CategoryID;
cmd.Parameters.AddWithValue("@ShippingCost", SqlDbType.Int).Value = product.ShippingCost;
cmd.Parameters.AddWithValue("@InOffer", SqlDbType.Bit).Value = product.InOffer;
cmd.Parameters.AddWithValue("@Visible", SqlDbType.Bit).Value = product.Visible;
cmd.Parameters.AddWithValue("@ProductID", SqlDbType.Text).Value = product.ProductID;
cmd.Parameters.Add("@ProductImage", SqlDbType.Image).Value = product.ProductImage == null ? (object)DBNull.Value : product.ProductImage;
DbUtility.UpdateDb(cmd);
}
页面加载代码:
protected void Page_Load(object sender, EventArgs e)
{
int productId = 0;
productId = Convert.ToInt32(Request.QueryString["ProductID"]);
LoadProductDetails(productId);
}
protected void LoadProductDetails(int productId)
{
var product = ProductBL.GetProductById(productId);
TitleTextBox.Text = product.ProductName;
DescriptionTextBox.Text = product.Description;
SetTextBox.Text = Convert.ToInt32(product.ItemsInSet).ToString();
UnitOwnerTextBox.Text = Convert.ToInt32(product.UnitPriceOwner).ToString();
UnitResellerTextBox.Text = Convert.ToInt32(product.UnitPriceReseller).ToString();
ShipmentTextBox.Text = Convert.ToInt32(product.ShippingCost).ToString();
}
please Help me
尝试在
中加载LoadProductDetailsif(!IsPostback)
{
//here
}
在使用会话或查询字符串变量之前总是检查null条件,以避免任何获得错误的机会。我认为这里你缺少检查!IsPostback
条件,这就是为什么当你点击更新按钮时,你的更新值将丢失,因为你的方法LoadProductDetails(productId);
被调用。