如何在asp.net

时间:2016-06-24 15:26:14

标签: asp.net

大家好我真的需要你的帮助我在datalist中有一个文本框问题是,它没有用我输入的新值初始化,除了数据库中的值。我不知道以前是否有过这样的经历,我很乐意感谢你的帮助。

这是html:

 <asp:DataList ID="SubList" runat="server" DataKeyField="ProductID" OnUpdateCommand="SubList_UpdateCommand">
                    <ItemTemplate>
                        PRICE: <%# Eval("Price", "{0:c}") %>
                       Quantity:<asp:Label ID="EditQuantitylbl" Text='<%# Eval("Quantity").ToString() %>' runat="server"></asp:Label>
                        COLOR:<asp:Label runat="server" ID="ColorLbl" Text='<%# Eval("ColorName").ToString() %>' ></asp:Label>
                        Size:<asp:Label runat="server" ID="Sizelbl" Text='<%# Eval("SizeName").ToString() %>' ></asp:Label>
                        PRODUCT NAME:<asp:Label runat="server" ID="productNamelbl" Text='<%# Eval("ProductName").ToString() %>'></asp:Label> 
                        <br />
                        <asp:LinkButton runat="server" ID="editBtn" CommandName="edit">Edit</asp:LinkButton><br />
                    </ItemTemplate>
                </asp:DataList>

以下是文件背后的代码:

     protected void SubList_UpdateCommand(object source, DataListCommandEventArgs e)
    {

        string Pid = SubList.DataKeys[e.Item.ItemIndex].ToString();
        string colorlbl = ((Label)e.Item.FindControl("Coloredit")).Text;
        string sizelbl = ((Label)e.Item.FindControl("Sizeedit")).Text;
        string quantityBox = ((TextBox)e.Item.FindControl("EditQuantityBox")).Text;
        bool success = true;

        string colorID = ShoppingCartAccess.GetColorID(colorlbl);
        string sizeID = ShoppingCartAccess.GetSizeID(sizelbl);
        int quantity;
        if (Int32.TryParse(quantityBox, out quantity)) {
             success = success && ShoppingCartAccess.UpdateItem(Pid, quantity, colorID, sizeID);
        }

        else
        {
            success = false;
        }
        statusLabel.Text = success ?
            "Your shopping cart was successfully updated!" :
            "Some quantity updates failed! Please verify your cart!";
        SubList.EditItemIndex = -1;
        SubList.DataBind();
    }

以下是更新的代码:

       public static bool UpdateItem(string productId, int quantity, string colorId, string sizeId)
    {
        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();
        // set the stored procedure name
        comm.CommandText = "ShoppingCartUpdateItem";
        // create a new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@CartID";
        param.Value = shoppingCartId;
        param.DbType = DbType.String;
        param.Size = 36;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@ProductID";
        param.Value = productId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@Quantity";
        param.Value = quantity;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@ColorID";
        param.Value = colorId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);
        // create a new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@SizeID";
        param.Value = sizeId;
        param.DbType = DbType.Int32;
        comm.Parameters.Add(param);

        // returns true in case of success and false in case of an error
        try
        {
            // execute the stored procedure and return true if it executes
            // successfully, and false otherwise
            return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
        }
        catch
        {
            // prevent the exception from propagating, but return false to
            // signal the error
            return false;
        }
    }

我哪里出错?

0 个答案:

没有答案