Shadouspan
Shadouspan

Reputation: 263

How to Dynamic Button Inside the Repeater / I Always Got Same Value

On the homepage of the website I made, I pull the products from the database with the repeater and eval as below:

<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>

What I am trying to do is: When the user clicks on the product with LinkButton, it will transfer the product's id to the next page with the session and list the related product id and other properties of the product (product image, description, price etc.). But clicking on which product I click will only receive the first product id is id number 1. For example I click third product, then same id info appears on the other page (ID 1). I always get ID 1 whatever I click. After a little research; I learned that I need to make the LinkButton dynamic. But I don't know how to do that. My homepage aspx.cs code as below:

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

I think the real problem is code don't know which one I clicked. How can I solve this problem?

And this is my product.aspx page:

<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>

Upvotes: 2

Views: 677

Answers (1)

VDWWD
VDWWD

Reputation: 35514

It looks to me that you are using a lot of unnecessary code. You get the product ID as a string from a Label, then do a database query to get the same id while looping the Repeater...

If I were you I would use OnCommand instead of OnClick and send the correct ID as a CommandArgument.

So in the aspx change the LinkButton to

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

And then in code behind

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

Upvotes: 1

Related Questions