1

I have a repeater on my page, page1, which displays product details in each item. Each item has "select this" button and I post to page2. I want to display which product selected on page2. Sounds simple but couldn't solve it for hours. Any idea? My itemtemplate on page1 looks like this:

<ItemTemplate>
<ul>
<li><%#Eval("Product_Name") %></li>
<li><%#Eval("Product_Code") %></li>
...
<li>
<asp:Button ID="btnSubmit" runat="server" Text="Select this" PostBackUrl="page2"/></li>
</ul>
</ItemTemplate>

Only solution came to my mind getting button number with Request.Form but this will make me add one more query to get nth product. Thanks for help

4

3 回答 3

1

不确定它是否适用于您的情况,但通常,这个问题可以通过使用 ItemCommand 模式来解决:

首先订阅ItemCommand转发器的事件(与订阅ItemCreated/相同ItemDataBound

yourRepeater.ItemCommand += yourRepeater_ItemCommandHandler;

处理程序在哪里:

protected void ItemCommandHandler(object source, RepeaterCommandEventArgs e)
{
  if(e.CommandName=="SelectThis")
  {
      var productCode = e.CommandArgument;
      //...
  }
}

然后,在您的标记中:

<asp:Button ID="btnSubmit" runat="server" Text="Select this" CommandName="SelectThis" CommandArgument='<%#Eval("Product_Code")%>' />

发布到另一个页面时从未尝试过这种模式。希望这无论如何都会有所帮助。

于 2013-11-13T11:02:41.643 回答
0

PostBack必要吗?如果不是,我建议你用这种方法解决这个问题:

将您的替换asp:Buttonasp:HyperLink并传递 productId 或 productCode 在QueryString. 这是一个例子:

<ItemTemplate>
    <ul>
        .
        .
        <li>
            <asp:HyperLink ID="hlSelectProduct" runat="server" Text="Select this" NavigateUrl='<%# string.Format("~/Page2.aspx?Product_Code={0}", Eval("Product_Code")) %>' />
        </li>
    </ul>
</ItemTemplate>

在 page2.aspx 上,只需使用 Product_Code 从数据库中检索产品

于 2013-11-13T11:03:13.237 回答
0

我不能使用查询字符串,因为我不希望所有内容都显示在 url 上。我尝试了 ItemCommand 但它需要页面才能发布。出于同样的原因,我也不能使用 onclientclick。

但是,我设法通过使用 Jquery 解决了这个问题。我在中继器之外创建了一个隐藏字段,并创建了一个 jquery 函数来处理按钮点击。我将产品 id 值放在每个按钮的 rel 属性中,并通过该函数将其分配给 hiddentext 的 val。Jquery 函数在页面发布之前确实有效,并且由于我的按钮都有 postbackurl 地址,所以它没有问题。

于 2013-11-13T13:21:42.087 回答