如何从转发器中的下拉列表中获取值

时间:2014-10-01 06:17:25

标签: c# asp.net

在下面的代码中我使用了转发器控件,我希望从转发器内部的链接按钮单击下拉列表中获取值。在下面的代码中,当我点击按钮然后下拉列表返回第一个值。

请尽快回复。提前致谢。

ASP.NET MARKUP

<asp:Repeater ID="rptProduct" runat="server" OnItemCommand="rptProduct_ItemDataBound">
    <ItemTemplate>
        <div class="span6">
            <h3><%# Eval("ProductName")%>
            </h3>
            <hr class="soft" />
            <form class="form-horizontal qtyFrm">
                <div class="control-group">
                    <label class="control-label">
                        <span>Select Quantity</span>
                    </label>
                    <div class="controls">
                        <asp:DropDownList ID="ddlQuantity" class="span1" runat="server">
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem>5</asp:ListItem>
                        </asp:DropDownList>
                        <asp:LinkButton ID="lnkAddCart" runat="server" class="btn btn-large btn-primary pull-right" CommandName="Add">Add to cart 
                            <i class=" icon-shopping-cart"></i>
                        </asp:LinkButton>
                    </div>
                </div>
                <asp:Label ID="lblError" Visible="false" class="alert alert-block alert-error" runat="server" Text=""></asp:Label>
            </form>
            <hr class="soft clr" />
            <p><%#Eval("Description")%>
            </p>
            <a class="btn btn-small pull-right" href="#detail">More Details</a>
            <br class="clr" />
            <a href="#" name="detail"></a>
            <hr class="soft" />
        </div>
    </ItemTemplate>
</asp:Repeater>

C#

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
        rptCategory.DataSource = bll.getCategory(); 
        rptCategory.DataBind(); 
        if (Request.QueryString["productID"] != null) 
        { 
             BindDataList(); 
             bindcartdetail(); 
        } 
    }
}

protected void rptProduct_ItemDataBound(object sender, RepeaterCommandEventArgs e)
{
    DropDownList ddlQuantity = (DropDownList)e.Item.FindControl("ddlQuantity");
    Label lblError = ((Label)e.Item.FindControl("lblError"));
    Image photo = (Image)e.Item.FindControl("imgProduct");

    if (e.CommandName == "Add")
    {
         string qty = ddlQuantity.SelectedValue;
         Session["proQty"] = qty;
         Session["photo"] = photo.ImageUrl;

         cart();
    }
}

protected void cart()
{
    int productID = Convert.ToInt32(Request.QueryString["productID"]);

    DataTable dtproduct = new DataTable();
    dtproduct = bll.getProductbyID(productID);

    string proCode = dtproduct.Rows[0][3].ToString();
    string proName = dtproduct.Rows[0][4].ToString();

    int quantity = Convert.ToInt32(Session["proQty"]);
    string photo = Session["photo"].ToString();
    // SESSION ID
    SessionIDManager manager = new SessionIDManager();

    string OldID = Context.Session.SessionID;

    if (OldID == "")
    {
         string sessionid = manager.CreateSessionID(Context);

         bool redirected = false;
         bool isAdded = false;
         manager.SaveSessionID(Context, sessionid, out redirected, out isAdded);
         Session["sessionid"] = sessionid;
         Session["proid"] = productID;
         Session["proCode"] = proCode;
         Session["proname"] = proName;

         DataTable dtinfo = new DataTable();
         dtinfo = bll.getCartInfo(proCode, sessionid);
         // PRODUVT IS AVLB OR NOT

         if (dtinfo.Rows.Count > 0)
         {
               int proquantity = Convert.ToInt32(dtinfo.Rows[0][3].ToString());
               proquantity = proquantity + quantity;

               bll.updateCart(proquantity, proCode, sessionid);
               Response.Redirect("Product.aspx");

         }
         else
         {
               bll.addCart(proCode, proName, quantity, sessionid, photo);
               Response.Redirect("Product.aspx");

         }

    }
    else
    {
         string sessionid = OldID;
         Session["sessionid"] = sessionid;
         Session["proid"] = productID;
         Session["proCode"] = proCode;
         Session["proname"] = proName;

         DataTable dtinfo = new DataTable();
         dtinfo = bll.getCartInfo(proCode, sessionid);

         if (dtinfo.Rows.Count > 0)
         {
              int proquantity = Convert.ToInt32(dtinfo.Rows[0][3].ToString());
              proquantity = proquantity + quantity;

              bll.updateCart(proquantity, proCode, sessionid);
              Response.Redirect("Product.aspx");

         }
         else
         {
              bll.addCart(proCode, proName, quantity, sessionid, photo);
              Response.Redirect("Product.aspx");

         }
    }
}

1 个答案:

答案 0 :(得分:0)

<div class="controls">
     <asp:DropDownList ID="ddlQuantity" class="span1" runat="server">
         <asp:ListItem Value="1">1</asp:ListItem>
         <asp:ListItem Value="2">2</asp:ListItem>
         <asp:ListItem Value="3">3</asp:ListItem>
         <asp:ListItem Value="4">4</asp:ListItem>
         <asp:ListItem Value="5">5</asp:ListItem>
     </asp:DropDownList>
     <asp:LinkButton ID="lnkAddCart" runat="server" class="btn btn-large btn-primary pull-right" OnClick="lnkAddCart_click">Add to cart <i class=" icon-shopping-cart"></i> </asp:LinkButton>
</div>