(ASP.. NET - c#)在存储文本框时遇到问题.列表视图中的文本到一个数据表

本文关键字:文本 数据表 视图 一个 列表 NET ASP 遇到 存储 问题 | 更新日期: 2023-09-27 18:14:53

我试图保存单击链接按钮lnkUpdate后在文本框中输入的数据。当我点击lnkupdate时,数据表似乎没有变化。

aspx代码

<div class="col-md-7">
                <div class="well well-sm">
                    <fieldset>
                        <legend>Warehouse</legend>
                        <div class="container-fluid">
                            <div class="panel panel-default">
                                <table class="table table-bordered table-hover table-responsive">
                                    <thead>
                                        <tr>
                                            <td><strong>#</strong></td>                                        
                                            <td><strong>BranchName</strong></td>
                                            <td><strong>Quantity</strong></td>
                                            <td><strong>Critical Level</strong></td>
                                            <td></td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <asp:ListView ID="lvWarehouse" runat="server" 
                                            onitemediting="lvWarehouse_ItemEditing" 
                                            onitemcanceling="lvWarehouse_ItemCanceling" 
                                            onitemupdating="lvWarehouse_ItemUpdating">
                                        <ItemTemplate>
                                            <tr>
                                                <td><%# Eval("BranchID") %></td>
                                                <td><%# Eval("BranchName") %></td>
                                                <td><%# Eval("Quantity") %></td>
                                                <td><%# Eval("CriticalLevel") %></td>
                                                <td>
                                                    <asp:LinkButton ID="btnEdit" runat="server" class="glyphicon glyphicon-pencil" CommandName="Edit" />
                                                </td>
                                            </tr>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <tr>
                                                <td><asp:Label ID="lblID" runat="server" Text='<%# Eval("BranchID") %>' /></td>
                                                <td><asp:Label ID="lblBranchName" runat="server" Text='<%# Eval("BranchName") %>' /></td>
                                                <td><asp:TextBox ID="txtQty" runat="server" class="form-control input-sm" Text='<%# Eval("Quantity") %>' /></td>
                                                <td><asp:TextBox ID="txtCritical" runat="server" class="form-control input-sm" Text='<%# Eval("CriticalLevel") %>' /></td>
                                                <td>
                                                    <asp:LinkButton ID="lnkUpdate" runat="server" class="glyphicon glyphicon-ok" CommandName="Update" />
                                                    <asp:LinkButton ID="lnkCancel" runat="server" class="glyphicon glyphicon-remove" CommandName="Cancel" />
                                                </td>
                                            </tr>
                                        </EditItemTemplate>
                                    </asp:ListView>
                                    </tbody>
                                </table>    
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>

cs代码

SqlConnection con = new SqlConnection(Helper.GetCon());
    DataTable ProdDetails;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetSuppliers();
            GetProdDetails();
        }
    }
    void GetSuppliers()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT SupplierID, SupplierName FROM Suppliers";
        SqlDataReader data = cmd.ExecuteReader();
        ddlSupplierName.DataSource = data;
        ddlSupplierName.DataTextField = "SupplierName";
        ddlSupplierName.DataValueField = "SupplierID";
        ddlSupplierName.DataBind();
        con.Close();
    }
    void GetProdDetails()
    {
        ProdDetails = (DataTable)Session["proddetails"];
        if (ProdDetails == null)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT b.BranchID, b.BranchName, pd.QtyonHand AS Quantity, " +
                "pd.CriticalLevel FROM Branches AS b LEFT JOIN ProductDetails AS pd ON b.BranchID=pd.BranchID";
            SqlDataAdapter data = new SqlDataAdapter(cmd);
            ProdDetails = new DataTable();
            data.Fill(ProdDetails);
            DataRow[] rowList = ProdDetails.Select();
            foreach (DataRow dr in rowList)
            {
                dr["Quantity"] = "0";
                dr["CriticalLevel"] = "0";
            }
            lvWarehouse.DataSource = ProdDetails;
            lvWarehouse.DataBind();
            con.Close();
            Session["proddetails"] = ProdDetails;
        }
        else
        {
            lvWarehouse.DataSource = ProdDetails;
            lvWarehouse.DataBind();
            Session["proddetails"] = ProdDetails;
        }
        TotalQuantity();
    }
    void TotalQuantity()
    {
        int total = 0;
        for (int nRow = 0; nRow <= ProdDetails.Rows.Count - 1; nRow++)
        {
            int qty = int.Parse(ProdDetails.Rows[nRow]["Quantity"].ToString());
            total = total + qty;
        }
        txtQuantity.Text = total.ToString(); ;
    }
protected void lvWarehouse_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        GetProdDetails();
        string lblID = (lvWarehouse.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
        TextBox Qty = (TextBox)lvWarehouse.Items[e.ItemIndex].FindControl("txtQty");
        string txtQty = (lvWarehouse.Items[e.ItemIndex].FindControl("txtQty") as TextBox).Text;
        string txtCritical = (lvWarehouse.Items[e.ItemIndex].FindControl("txtCritical") as TextBox).Text;
        foreach (DataRow row in ProdDetails.Rows)
        {
            if (row["BranchID"].ToString() == lblID)
            {
                row["Quantity"] = Qty.Text;
                row["CriticalLevel"] = txtCritical;
                break;
            }
        }
        lvWarehouse.EditIndex = -1;
        GetProdDetails();
    }

(ASP.. NET - c#)在存储文本框时遇到问题.列表视图中的文本到一个数据表

www.codedigest.com/Articles/ASPNET/105_EditUpdateDelete_and_Insert_in_ListView_Control.aspx参考这个