我正在通过将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();
}
请帮助我
答案 0 :(得分:0)
尝试在
中加载 LoadProductDetailsif(!IsPostback)
{
//here
}
答案 1 :(得分:0)
在使用Session或Query String变量之前,首先要检查null条件,以避免出现任何错误。
我想你在这里检查!IsPostback
条件是错误的,这就是为什么当你点击“更新”按钮时,你的方法LoadProductDetails(productId);
所调用的更新值将会丢失。