文本框在gridview中不保存其在表中的值

时间:2013-03-12 16:22:22

标签: c# asp.net

我有一个gridview gv_Products和一个gridview Gv_selected。我的产品gridview有一个复选框,当选中时,在gv_selected gridview中输入所选行 我在gv_selected gridiew中添加了一个文本框,输入我想重新排序的数量。按下提交按钮后,我输入的数量会丢失其值。

<asp:GridView ID="gvSelected" runat="server" 
    AutoGenerateColumns = "False" Font-Names = "Arial" CssClass="gridviewsSmall" Font-Size = "11pt" 
    OnRowDataBound="GridView_gvSelected_RowDataBound" EnableViewState="False"                                    
    EmptyDataText = "No Records Selected"  >
        <Columns>
         <asp:BoundField DataField="ProductId" HeaderText="Product ID" ReadOnly="True" 
            SortExpression="ProductId" />
        <asp:TemplateField HeaderText="Product No" SortExpression="ProductNo">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductNo") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Product Name" SortExpression="Product_name">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Product_name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
         <asp:BoundField DataField="SupplierId" HeaderText="Supplier ID" ReadOnly="True" 
            SortExpression="SupplierId" />
            <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"  Text='<%#Bind("Quantity") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
 </Columns>  
 </asp:GridView>
 <asp:Button ID="btnSendOrder" Visible="false" runat="server" Text="Send Order" 
onclick="btnSendOrder_Click" />

这是我在gvSelected gridview

中添加行的代码
       private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ProductId");
        dt.Columns.Add("ProductNo");
        dt.Columns.Add("Product_name");
        dt.Columns.Add("SupplierId");
        dt.Columns.Add("Quantity");

        dt.AcceptChanges();
        return dt;
    }

    private DataTable AddRow(GridViewRow gvRow, DataTable dt)
    {

        DataRow[] dr = dt.Select("ProductId = '" + gvRow.Cells[3].Text + "'");
        if (dr.Length <= 0)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["ProductId"] = gvRow.Cells[3].Text;
            dt.Rows[dt.Rows.Count - 1]["ProductNo"] = (gvRow.FindControl("Label2") as Label).Text;
            dt.Rows[dt.Rows.Count - 1]["Product_name"] = (gvRow.FindControl("Label3") as Label).Text;
            dt.Rows[dt.Rows.Count - 1]["SupplierId"] = (gvRow.FindControl("Label5") as Label).Text;
            dt.Rows[dt.Rows.Count - 1]["Quantity"] = 0;
            dt.AcceptChanges();
        }
        return dt;
    }

    protected void CheckBox_CheckChanged(object sender, EventArgs e)
    {
        GetData();
        SetData();
        BindSecondaryGrid();

    }

    private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)ViewState["SelectedRecords"];
        gvSelected.DataSource = dt;
        gvSelected.DataBind();
    }

这是提交按钮!

protected void btnSendOrder_Click(object sender, EventArgs e)
    {

        t_supplier_orders newOrder = new t_supplier_orders();

        newOrder.UserName = User.Identity.Name;
        newOrder.Order_date = DateTime.Now;


        newOrder.Order_status = "Pending";
        MembershipUser myObject = Membership.GetUser();
        Guid UserID = new Guid(myObject.ProviderUserKey.ToString());
        newOrder.UserId = UserID;
        newOrder.SupplierId = Convert.ToInt32(ddl1.SelectedValue);
        newOrder.Received_date = null;


        Bohemian.t_supplier_orders.AddObject(newOrder);
        Bohemian.SaveChanges();


        //------------------------------------------------------------------------+
        //  Create a new OderDetail Record for each item in the gvSelected     |
        //------------------------------------------------------------------------+

        foreach (GridViewRow row in gvSelected.Rows)
        {

            {
                t_supplier_orders_details od = new t_supplier_orders_details();

                     TextBox txt1 = (TextBox)gvSelected.FindControl("TextBox1");

                    od.OrderId = newOrder.OrderId;
                    od.ProductId = Convert.ToInt32(row.Cells[0].Text);
                    od.Product_name = (row.FindControl("Label3") as Label).Text;
                    od.ProductNo = (row.FindControl("Label2") as Label).Text;

                    od.Quantity = Convert.ToInt32(txt1.text);

                    Bohemian.t_supplier_orders_details.AddObject(od);

            }
        }

       Bohemian.SaveChanges();
       lblSuccess.Text = "The Order has been successfully sent to supplier!!";
        lblSuccess.ForeColor=System.Drawing.Color.BlueViolet;
        lblSuccess.Font.Bold = true;

    }

1 个答案:

答案 0 :(得分:1)

我假设您在每次回发时分配DataSourceDataBind GridView。这将覆盖所有变化。

所以请将您的代码包装在!IsPostBack支票中:

protected void Page_Load()
{
    if(!IsPostBack)
    {
        BindSecondaryGrid();
    }
}

顺便说一下,你不应该将DataTable存储在ViewState中,因为这样会将其搞砸。