2017-08-01 58 views
2

在我做網站的主頁,我拉了產品從數據庫中與中繼器和如下EVAL:如何動態按鈕直放站內/我總是得到相同價值

<div class="col-md-12 grid-gallery overflow-hidden"> 
    <div class="tab-content"> 
     <ul class="grid masonry-items"> 
      <asp:Repeater ID="RptrProductInfo" runat="server" DataSourceID="SqlDtSourceProductInfo"> 
       <ItemTemplate> 
        <li class="<%#Eval("productcategory") %>"> 
         <figure> 
          <div class="gallery-img"><asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click"><img src="<%#Eval("prouctimage") %>" alt="" /></asp:LinkButton></div> 
          <figcaption> 
           <asp:Label ID="LblProductID" runat="server" Text='<%#Eval("productid") %>'></asp:Label> 
           <h3><%#Eval("productname") %></h3> 
           <p>Ürün hakkında detaylı bilgi için tıklayınız.</p> 
          </figcaption> 
         </figure> 
        </li> 
       </ItemTemplate> 
      </asp:Repeater> 
      <asp:SqlDataSource ID="SqlDtSourceProductInfo" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT [productid], [productname], [productcategory], [productimage] FROM [product]"></asp:SqlDataSource> 
     </ul> 
    </div> 
</div> 

我是什麼試圖做的是:當用戶使用LinkBut​​ton點擊產品時,它會將產品的ID轉移到下一個會話頁面,並列出相關產品ID和產品的其他屬性(產品圖片,說明,價格等) 。但點擊我點擊的產品只會收到第一個產品ID是ID號1.例如,我點擊第三個產品,然後相同的ID信息出現在另一個頁面上(ID 1)。無論我點擊什麼,我總是會獲得ID 1。經過一點研究;我瞭解到我需要使LinkBut​​ton動態。但我不知道該怎麼做。我的主頁aspx.cs代碼如下:

protected void LinkButton1_Click(object sender, EventArgs e) 
{ 
    foreach (RepeaterItem item in RptrProductInfo.Items) 
    { 
     Label lblName = (Label)item.FindControl("LblProductID"); 
     if (lblName != null) 
     { 
      string mesaj = ""; 
      DataRow drLogin = function.GetDataRow("SELECT productid FROM product WHERE productid='" + lblName.Text + "'"); 
      if(drLogin == null) 
      { 
       mesaj = "<script>alert('Error!');</script>"; 
       Response.Write(mesaj); 
      } 
      else 
      { 
       Session["productid"] = drLogin["productid"].ToString(); 
       Response.Redirect("product.aspx"); 
      } 
     } 
    } 
} 

我認爲真正的問題是代碼不知道我點擊了哪一個。我怎麼解決這個問題?

這是我product.aspx頁:

<section> 
    <div class="container"> 
     <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> 
      <ItemTemplate> 
       <div class="row"> 
        <div class="col-md-6 col-sm-12 zoom-gallery sm-margin-bottom-ten"> 
         <a href="<%#Eval("productimage") %>"><img src="<%#Eval("productimage") %>" alt=""/></a> 
        </div> 
        <div class="col-md-5 col-sm-12 col-md-offset-1"> 
         <span class="product-name-details text-uppercase font-weight-600 letter-spacing-2 black-text"><%#Eval("productname") %></span> 
         <p class="text-uppercase letter-spacing-2 margin-two">Stok Durumu: <%#Eval("stock") %></p> 
         <div class="separator-line bg-black no-margin-lr margin-five"></div> 
         <p><%#Eval("description") %></p> 
         <span class="price black-text title-small"><%#Eval("price") %></span> 
         <div class="col-md-9 col-sm-9 no-padding margin-five"> 
          <a class="highlight-button-dark btn btn-medium button" href="shop-cart.html"><i class="icon-basket"></i> Add To Cart</a> 
         </div> 
        </div> 
       </div> 
      </ItemTemplate> 
     </asp:Repeater> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT [productid], [productname], [stock], [description], [price], [productimage] FROM [product] WHERE ([productid] = @productid)"> 
     <SelectParameters> 
      <asp:SessionParameter Name="productid" SessionField="productid" Type="Int32" /> 
     </SelectParameters> 
     </asp:SqlDataSource> 
    </div> 
</section> 

回答

1

它看起來對我說,你使用了很多不必要的代碼。你得到的產品ID爲來自標籤的字符串,然後做一個數據庫查詢得到同樣的ID而循環復讀......

如果我是你,我會用OnCommand代替OnClick併發送正確的ID爲一個CommandArgument

所以在ASPX LinkBut​​ton的改變

<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" 
    CommandArgument='<%# Eval("productid") %>'>LinkButton</asp:LinkButton> 

然後在後面的代碼

protected void LinkButton1_Command(object sender, CommandEventArgs e) 
{ 
    Session["productid"] = e.CommandArgument.ToString(); 
    Response.Redirect("product.aspx"); 
}