ASP.NET Repeater and Input Button issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Queez
    New Member
    • Jul 2007
    • 24

    ASP.NET Repeater and Input Button issue

    I have an issue with an ASP repeater which is seriously frustrating me.

    In simple terms, I have an ASP repeater which is meant to display a list of items for a wedding list, including the name of the item, how many are available and a button to take the user to a "purchase" form where they select how many they wish to purchase. Once they're done, the page is reloaded and the new "number available" is displayed. Sounds simple, huh?

    Well it damn well should be, but unfortunately, my partner and I have been working on this for 3 days and we cannot work out a way to get the item ID in the following line:

    Code:
    <input type="button" onclick="window.open('AnotherPage.aspx?ID=<%#DataBinder.Eval(Container.DataItem, "ItemID")%>');" ... />
    The fundamental problems are:

    - The #DataBinder code includes a double-quoted "ItemID". It doesn't seem to matter which way we try and arrange the quotes on the onclick command, we cannot get it to work (e.g. single quotes on the onclick, double quotes in the window.open, vice-versa, no quotes on the on-click etc.) with errors generally coming down to improperly formatted html etc.

    - The repeater severely limits the use of getElementById and other useful javascript methods because, obviously, any elements within the repeater will be repeated

    We have managed to get around this before by using the single quotes on the attribute value, but that only works when there's no javascript, e.g.:

    Code:
    ... class='<%#DataBinder.Eval(Container.DataItem, "ClassName")%>'...
    We managed to get the code working in Internet Explorer by using a custom attribute called itemnumber, e.g.:

    Code:
    <input itemnumber='<%#DataBinder.Eval(Container.DataItem, "ClassName")%>' onclick="window.open('AnotherPage.aspx?ID=' + this.itemnumber);" />
    However, this then doesn't work in Firefox because the custom attribute is not recognised (nothing happens).

    We thought perhaps using a legitimate attribute would work, so we tried using the "id" attribute, but aparently element ids cannot be produced dynamically.

    Does anyone know either a way to fix this so it works or a better way of doing what I'm trying to achieve? We basically need some way to send the ID of the current item to the "purchase" form when the user clicks the button.

    We have tried doing the same things using an <asp:button /> as well, but run into similar problems with that.

    Eagerly awaiting a response.

    ~ Q
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    You should be able to accomplish this using the repeater's ItemDataBound event. Also make your button an asp button control.

    Code:
     
    repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    { 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    //This code assumes you are getting the value of your ItemId from a dataset.
    Button btn = (Button)e.Item.FindControl("Button1");
    btn.CommandName = "Purchase";
    btn.CommandArgument = dataSet.Tables[0].Rows[e.Item.ItemIndex]["ItemId"];
    }
    }
    //Now in the repeater's ItemCommand event we will use a label control to show the new window. Place the label control outside of your repeater and set the Visible property to false. We will also use the CommandArgument of the button control as the querystring value.
    Code:
     
    public void repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    { 
    if(e.CommandName == "Purchase")
    {
    string URL = "AnotherPage.aspx?ItemId=" + e.CommandArgument;
    Label1.Visible = true;
    Lable1.Text = "<script>window.open(URL)</script>";
    }
    }
    Let me know if you have any problems with this code.

    Nathan

    Comment

    • Queez
      New Member
      • Jul 2007
      • 24

      #3
      Nathan,

      Thanks for your swift and concise response. Am I right in thinking that, using the suggested method, I won't need to use any <%#DataBinder.E val... %> sections in my aspx files? I always did think using the <%..%> tags was a bit ASP classic!

      Thanks again.

      ~ Q

      Comment

      • nateraaaa
        Recognized Expert Contributor
        • May 2007
        • 664

        #4
        Originally posted by Queez
        Nathan,

        Thanks for your swift and concise response. Am I right in thinking that, using the suggested method, I won't need to use any <%#DataBinder.E val... %> sections in my aspx files? I always did think using the <%..%> tags was a bit ASP classic!

        Thanks again.

        ~ Q
        You could use the DataBinder tag if you wanted to set the CommandArgument of your button in the html.

        [CODE=html]<asp:Button runat="server" CommandName="Pu rchase" CommandArgument ='<%#DataBind.E val(Container, "DataItem.ItemI d") %>' />[/CODE]

        Otherwise the code example I submitted before will set the CommandArgument value in the code behind and you will not need to use the DataBinder tag.

        Nathan

        Comment

        Working...