最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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

I have the following repeater:

<asp:Repeater ID="Cartridges" 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-p-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 pleteness 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
    }
}

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

I have the following repeater:

<asp:Repeater ID="Cartridges" 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-p-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 pleteness 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
    }
}
Share Improve this question edited Jun 11, 2011 at 18:07 Brian Webster 30.9k51 gold badges157 silver badges227 bronze badges asked Jun 11, 2011 at 17:46 ComfortablyNumbComfortablyNumb 1,45610 gold badges38 silver badges65 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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 acplished 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 ing 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)

  1. 暂无评论