Gridview值传递到下一页并执行更新内容

时间:2016-09-02 14:29:03

标签: c# asp.net data-binding

我在我的网站上使用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;
    }

此处,在调试代码时,产品名称为null。

1 个答案:

答案 0 :(得分:0)

您需要设置TextBox,如下所示

TitleTextBox.Text = product.ProductName

可以在PageLoad

中完成分配

基于更新

您需要按以下方式分配

  var product = ProductBL.GetProductById(productId);

        TitleTextBox.Text = product.ProductName;