C# 如何在中继器内设置动态按钮/I始终获得相同的值
在我制作的网站主页上,我使用repeater和eval从数据库中提取产品,如下所示:C# 如何在中继器内设置动态按钮/I始终获得相同的值,c#,asp.net,button,dynamic,repeater,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" D
<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>
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");
}
}
}
}
Stok Durumu:
在我看来,您正在使用大量不必要的代码。从标签中以字符串形式获取产品ID,然后在循环中继器时执行数据库查询以获取相同的ID
如果我是你,我会使用OnCommand
而不是OnClick
,并将正确的ID作为CommandArgument
发送
因此,在aspx中,将LinkButton更改为
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command"
CommandArgument='<%# Eval("productid") %>'>LinkButton</asp:LinkButton>
<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");
}