Gridview值传递到下一页,并执行更新的东西

本文关键字:更新 执行 值传 一页 Gridview | 更新日期: 2023-09-27 18:05:27

我在我的网站使用3层架构。而不是更新在gridview,我想发送gridview行值到其他页面,并执行更新的东西在那里。这是查看页面中的链接代码。

更新产品页面加载代码:

protected void Page_Load(object sender, EventArgs e)
    {
        int productId = 0;
        productId = Convert.ToInt32(Request.QueryString["ProductID"]);
        ProductBL.GetProductById(productId);
    }

按ID获取产品功能码:

public static Product GetProductById(int productID)
    {
        Product product;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ProductName, Description, ItemsInSet,  UnitPriceOwner, UnitPriceReseller, ShippingCost from Products where ProductID = @ProductID";
        cmd.Parameters.AddWithValue("@ProductID", SqlDbType.Int).Value = productID;
        SqlDataReader reader = DbUtility.GetDataReader(cmd);
        if (reader.HasRows)
        {
            product = new Product();
            reader.Read();
            product.ProductName = reader.GetString(0);
            product.Description = reader.GetString(1);
            product.ItemsInSet = reader.GetInt32(2);
            product.UnitPriceOwner = reader.GetInt32(3);
            product.UnitPriceReseller = reader.GetInt32(4);
            product.ShippingCost = reader.GetInt32(5);
            return product;
        }
        else
        {
            return null;
        }
    }

UpdateProduct页面:

 <label><b>Title</b></label>
        <asp:TextBox ID="TitleTextBox" runat="server" placeholder="Enter Product Title"></asp:TextBox>  
         <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
          ControlToValidate="TitleTextBox"
          ErrorMessage="Product Name is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>

      <label><b>Description</b></label>
        <asp:TextBox ID="DescriptionTextBox" runat="server" placeholder="Description"></asp:TextBox>  
        <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server"
          ControlToValidate="TitleTextBox"
          ErrorMessage="Product Name is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>
      <label><b>Set of</b></label>
         <asp:TextBox ID="SetTextBox" runat="server" placeholder="Enter quantity"></asp:TextBox>  
        <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server"
          ControlToValidate="SetTextBox"
          ErrorMessage="Quantity is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>
      <label><b>Unit Price (Owner)</b></label>
          <asp:TextBox ID="UnitOwnerTextBox" runat="server" placeholder="Unite Price Owner"></asp:TextBox>  
        <asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server"
          ControlToValidate="UnitOwnerTextBox"
          ErrorMessage="Unit Price Owner is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>
           <label><b>Unit Price (Reseller)</b></label>
          <asp:TextBox ID="UnitResellerTextBox" runat="server" placeholder="Unite Price Reseller"></asp:TextBox>  
        <asp:RequiredFieldValidator id="RequiredFieldValidator5" runat="server"
          ControlToValidate="UnitResellerTextBox"
          ErrorMessage="Unit Price Reseller is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>
      <label><b>Choose Category</b></label><br /><br />
      <asp:DropDownList ID="CategoryDropDownList" runat="server" placeholder="Select Category" DataSourceID="SqlDataSource1" 
          DataTextField="CategoryName" DataValueField="CategoryID" ></asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UmmatiConStr %>" SelectCommand="SELECT [CategoryName], [CategoryID] FROM [Categories]"></asp:SqlDataSource>
        <br /><br /><hr />
      <label><b>Shipment Cost</b></label>
          <asp:TextBox ID="ShipmentTextBox" runat="server" placeholder="Enter Shipment Charge"></asp:TextBox>  
        <asp:RequiredFieldValidator id="RequiredFieldValidator6" runat="server"
          ControlToValidate="ShipmentTextBox"
          ErrorMessage="Shipment Cost is a required field."
          ForeColor="Red">
        </asp:RequiredFieldValidator>

谁能帮助如何在文本框中绑定数据?

public partial class UpdateProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int productId = 0;
        productId = Convert.ToInt32(Request.QueryString["ProductID"]);
        LoadProductDetails(productId);
    }
    protected void LoadProductDetails(int productId)
    {
        ProductBL.GetProductById(productId);
        Product product = new Product();
        TitleTextBox.Text = product.ProductName;
    }

在这里,调试代码时Product name为null。

Gridview值传递到下一页,并执行更新的东西

你需要像下面这样设置文本框

TitleTextBox.Text = product.ProductName

分配可以在PageLoad

中完成基于更新 的

您需要按照以下方式进行分配

  var product = ProductBL.GetProductById(productId);
        TitleTextBox.Text = product.ProductName;