2014-10-01 155 views
0

在下面的代码中,我使用了中继器控制,并且我想从中继器内部的linkbutton click上的下拉列表中获取值。在下面的代码中,当我点击按钮时,dropdownlist返回第一个值。如何从中继器中的下拉列表中获取值

请尽快回复在此先感谢。

ASP.NET标记

<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"); 

     } 
    } 
} 
+0

你有没有绑定中继器的IsPostBack方法之外? – 2014-10-01 06:27:14

+0

没有我结合这样 保护无效的Page_Load(对象发件人,EventArgs的) { 如果 { rptCategory.DataSource = bll.getCategory()(的IsPostBack!); rptCategory.DataBind(); (Request.QueryString [“productID”]!= null) { BindDataList(); bindcartdetail(); } } – 2014-10-01 06:45:57

+0

我没有看到任何代码来设置dropdownlist的值。它是否缺少放在这里? – 2014-10-01 10:30:08

回答

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> 
相关问题