c# - 如何在中继器内动态按钮/我总是得到相同的值

标签 c# asp.net button dynamic repeater

在我制作的网站主页上,我使用 repeater 和 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 传送到带有 session 的下一页,并列出相关的产品 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>

最佳答案

在我看来,您使用了很多不必要的代码。您从标签中获取产品 ID 作为字符串,然后在循环 Repeater 时执行数据库查询以获取相同的 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");
}

关于c# - 如何在中继器内动态按钮/我总是得到相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45439276/

相关文章:

c# - 一种检查是否进行了 Paypal 交易的方法

c# - 适用于 Windows 8.1 的推送通知

jquery - 如何通过将下拉 div 放置在相关控件的上方或下方来在屏幕上显示?

javascript - 获取单击按钮的值/属性作为 event.data,带有 .on ("click"...)

c# - 如何使用 C# 裁剪扫描图像?

c# - Xamarin 表单 : how to implement platform specific code

c# - Visual Studio 2015 - 添加对类库 C# ASP.NET 的引用

java - 在 java webservice 和 asp.net 客户端 Web 应用程序之间发送的数据库对象的序列化

android - 如何创建等宽的按钮?

javascript - 如何捕获动态创建的按钮的点击? jQuery