开发者

ASP.NET C# Repeater - How to pass information back to server without Javascript?

I'm learning C# and working on my first ASP.Net e-commerce website.

I have the following repeater:

<asp:Repeater ID="Car开发者_Python百科tridges" runat="server" OnItemCommand="Cartridges_ItemCommand">
  <ItemTemplate>                            
       <asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton>
  </ItemTemplate>

However, when the code is rendered the repeater produces JavaScript to postback which is no good when populating a shopping cart if the user has JavaScript turned off!

<div class="wrap-cart">
 <div class="cartbuy"><a id="ContentPlaceHolder1_Cartridges_buy_0" href="javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$Cartridges$ctl00$buy&#39;,&#39;&#39;)">Buy</a></div>
 <div class="cartimg"><a href="url.html" class="productsurl"><img src="pic/epson-comp-set50.jpg" alt="product" /></a></div>
 <div class="carttext">
       <p class="cartdesc"><a href="url.html" class="productsurl"><span class="homeprinters">product</span></a></p>
       <p class="cartprice">£x.xx</p>
 </div>

All I want to do is to pass the cartID to a data table. Is there a way round it so JavaScript is not involved at all?

For completeness here's my code behind:

protected void Cartridges_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    cartidtest.Text = "added";
    if (e.CommandName == "AddtoCart")
    {
        string varCartID = (e.CommandArgument).ToString();

        //more code here
    }
}


Sure, you can pass the selected ItemID via QueryString

First, Generate an anchor tag like this:

<a href="NextCheckoutStep.aspx?ItemID=45">AddToCart</a>

This may be accomplished in an ItemTemplate:

<a href='<%# "NextCheckoutStep.aspx?ItemID=" + Eval("ItemID").ToString() %>'>AddToCart</a>

Finally, in NextCheckoutStep.aspx, access:

Request.QueryString["ItemID"]

Notes

  • You can pass information back to the same page. Just replace NextCheckoutStep.aspx with CurrentCheckoutStep.aspx. You will just need to put some routing code into your Page Load (or init) handlers (with postback = false) to react to the various page states that are created by this method. Sometimes a state diagram is helpful in working with this design.
  • Make sure to 100% sanitize your QueryString-Reads, because malicious users will attempt to put nasty values in the query string.
  • You don't have to use the QueryString, you can also get by with the HTTP POST collection; however, you need to sanitize here too, because you can never trust inputs coming from the client.


I would have a link which includes the item id in a querystring. I would expect the user's cartId to be stored in a cookie or in a session variable.

<asp:Repeater ID="Cartridges" runat="server">
<ItemTemplate>                                    
<p><a href="AddToCart.aspx?itemId=<%#Eval("itemID") %>">Buy</a></p>
</ItemTemplate>
</asp:Repeater>


A LinkButton renders as a link that immediately forces a submit, so it needs javascript.

You will have to use a plain HyperLink to render a url like mypage.aspx?id=cartid.


You can use instead of LinkButton

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜