473,473 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ASP.NET Repeater and Input Button issue

24 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. <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.:

Expand|Select|Wrap|Line Numbers
  1. ... 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.:

Expand|Select|Wrap|Line Numbers
  1. <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
Mar 16 '08 #1
3 3396
nateraaaa
663 Recognized Expert Contributor
You should be able to accomplish this using the repeater's ItemDataBound event. Also make your button an asp button control.

Expand|Select|Wrap|Line Numbers
  1.  
  2. repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  3. if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  4. {
  5. //This code assumes you are getting the value of your ItemId from a dataset.
  6. Button btn = (Button)e.Item.FindControl("Button1");
  7. btn.CommandName = "Purchase";
  8. btn.CommandArgument = dataSet.Tables[0].Rows[e.Item.ItemIndex]["ItemId"];
  9. }
  10. }
  11.  
//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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. public void repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
  3. if(e.CommandName == "Purchase")
  4. {
  5. string URL = "AnotherPage.aspx?ItemId=" + e.CommandArgument;
  6. Label1.Visible = true;
  7. Lable1.Text = "<script>window.open(URL)</script>";
  8. }
  9. }
  10.  
Let me know if you have any problems with this code.

Nathan
Mar 17 '08 #2
Queez
24 New Member
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.Eval... %> sections in my aspx files? I always did think using the <%..%> tags was a bit ASP classic!

Thanks again.

~ Q
Mar 17 '08 #3
nateraaaa
663 Recognized Expert Contributor
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.Eval... %> 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.

Expand|Select|Wrap|Line Numbers
  1. <asp:Button runat="server" CommandName="Purchase" CommandArgument='<%#DataBind.Eval(Container, "DataItem.ItemId") %>' />
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
Mar 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Moist | last post by:
Hi, I know this may be a trivial question but I wonder if it is possible to load a new HTML page (without opening a new window) by just clicking an input button. Is there a way to do this...
2
by: Stephen Noronha | last post by:
Hi all, I have an input button <input type="button" id="Button2" name="Button2" value="More" onclick="openPage()"> this input button onclick opens another page. this "More" button is for...
2
by: Stephen Noronha | last post by:
Hi all, I have an input button <input type="button" id="Button2" name="Button2" value="More" onclick="openPage()"> this input button onclick opens another page. this "More" button is for...
1
by: sonicsoul | last post by:
Hi, i wanted to define default global styles that would be used by 90% of controls. I would like to differentiate input text controls from input button controls. is there a way to specify this in...
3
by: vishnu | last post by:
Hi, I am using HTML input file button to browse the file and just am updating the file name in sql server table.But now i want to browse the file from the remote file share path ..How can i...
4
by: Chris | last post by:
Hi, i 'm experimenting with postback and i tried that with a button server control and an Html input button but with runat="server". The button server control causes a postback, but not the...
3
by: Valli | last post by:
Hi, I want to add an image to a html type input button. How can I do this? I have used background image style . But its not working. My target is to show an image in the button can anyone...
18
by: bning | last post by:
Hmm this forum really doesn't give you long enough to type in your question before logging you out.. well here goes my second attempt: I'm trying to teach myself javascript with dom scripting and...
10
by: bgold12 | last post by:
Hey, I know of two ways to place a link on an HTML page; (1) use the anchor element, and (2) use javascript to respond to an event (such as onclick) and set the location. I recently started...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.