将Web表单中的值存储到变量中

时间:2016-04-19 08:28:29

标签: asp.net webforms

我有一个datalist,它有一个taxt框和一个按钮。单击按钮我需要将文本框中的值插入数据库。但它一直在插入0

public partial class Product : System.Web.UI.Page
{
    int id;
    int quantity;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void productDisplayList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["id"] = Request.QueryString["id"];
        Session["id"] = Convert.ToInt16(Request.QueryString["id"]);
    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("addToCart1"))
        {
               int = Convert.ToInt16(e.Item.FindControl("Qty"));
        }

        if (e.CommandName == "addToCart")
        {

            AddToCart.addToCart(Convert.ToInt16(Session["id"]), Convert.ToInt16(Session["qty"]));
        }
    }
}

.aspx

<asp:ObjectDataSource ID="productDisplayList" runat="server" SelectMethod="getData" TypeName="ProductDisplayList" OnSelecting="productDisplayList_Selecting">
    <SelectParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
<asp:DataList ID="DataList2" runat="server" DataSourceID="productDisplayList" Width="490" OnItemCommand="DataList2_ItemCommand">
    <ItemTemplate>
        <asp:Label ID="Label4" class="productName" runat="server" Text='<%# Eval("productName") %>'></asp:Label>
        <table class="nav-justified">
            <tr>
                <td><asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("productImage") %>'  Width="300" Height="150" /></td>
                <td>
                    <table class="inner-nav-justified">
                        <tr> 
                            <td>
                                <p>Manufacture </p>
                            </td>
                            <td>
                                <asp:Label ID="Label1" runat="server" class="product" Text='<%# Eval("productManufacture") %>'></asp:Label>
                            </td>
                        </tr>
                        <tr>
                             <td>
                                 <p>Location </p>
                             </td>
                            <td>
                                 <asp:Label ID="Label2" runat="server" class="product" Text='<%# Eval("productLocation") %>' ></asp:Label>
                            </td> 
                        </tr>
                        <tr>
                            <td>
                                 <p>price </p>
                             </td>
                            <td> 
                                <asp:Label ID="Label3" runat="server" class="product" Text='<%# Eval("productPrice") %>' ></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                 <asp:TextBox ID="QtyTextBox"  class="form-control input-sm" CommandName="addToCart1"  runat="server" Width="50" PlaceHolder="Qty"></asp:TextBox>
                            </td>
                            <td>
                                <asp:Button ID="Button1" CssClass="btn btn-default btn-sm" CommandName="addToCart" runat="server" Text="Add" />
                            </td>
                        </tr>

                    </table>
                </td>
            </tr>
        </table>
         <div class="productDetailDesc">
             <p><b>Product Description</b></p>
                <asp:Label ID="Label5" runat="server" class="productDesc" Text='<%# Eval("productDesc") %>' ></asp:Label>

        </div>
    </ItemTemplate>

1 个答案:

答案 0 :(得分:0)

你在哪里绑定datalist。??如果你在PageLoad绑定它,那么你绑定在IsPostback .. ??

因为您的代码几乎看起来正确。如果您在PageLoad内绑定但未绑定IsPostback,那么您的Qty文本框始终采用初始值。

我知道这应该是评论,但我还不能评论XD