How do you assign ID in Repeater1_ItemDataBound (code please) and why are you doing this?
I don't see it makes sense as RepeaterItem implements INamingContainer interface, so that each TextBox will have unique ID. So all you need is to set single ID in template and that's all. But maybe there is some reason you're doing this, clarify please.
The background is I am developing a web site which sells books. This my particular question is about displaying a shopping cart, after a user has chosen several books that he or she wants to buy.
I assign IDs for each of the textboxes in the ItemDataBound as follows:
The reason I am doing this is because I want to keep the product ID (unique identifier for every book) in order to be able to refer to the product.
The textboxes are for quantities of the books the user wants to buy.
So, I need to get the IDs of the textboxes to get the product IDs.
I have tried in the button click event (assuming ProductID = 1):
dedyandy
Member
47 Points
57 Posts
How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 01:01 PM|LINK
I have textboxes in Repeater. I assign dynamically each of the ID of the textboxes in ItemDataBound event.
Could anyone help of how to get the IDs of textboxes in the button click event? I have only one button and it is outside of the Repeater
Please find the the code snippet below.
Thanks,
Andy
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ProductList.aspx.cs" Inherits="ProductList" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
<caption>
</caption>
<tr valign="top">
<td align="left" rowspan="3" style="width: 100px" valign="top">
<asp:HyperLink ID="HyperLinkProductImage" runat="server">HyperLink</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLinkProductName" runat="server">HyperLink</asp:HyperLink>
</td>
<td><asp:CheckBox ID="CheckBoxAddToCart" Text="Add to Cart" runat="server" /></td>
</tr>
<tr valign="top">
<td align="left" style="width: 100px" valign="top">
<asp:Label ID="lblPrice" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
<caption>
</caption>
<tr valign="top">
<td align="left" rowspan="3" style="width: 100px" valign="top">
<asp:HyperLink ID="HyperLinkProductImage" runat="server">HyperLink</asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLinkProductName" runat="server">HyperLink</asp:HyperLink>
</td>
<td><asp:CheckBox ID="CheckBoxAddToCart" Text="Add to Cart" runat="server" /></td>
</tr>
<tr valign="top">
<td align="left" style="width: 100px" valign="top">
<asp:Label ID="lblPrice" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</AlternatingItemTemplate>
<SeparatorTemplate>
<br>
</SeparatorTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:Content>
richiej
Participant
1771 Points
354 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 02:29 PM|LINK
Hi,
Could you be more specific on what you want. I really dont understand what you mean by "get the IDs of the textbox into the Button click event"
Yojimbo2
Member
345 Points
66 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 02:40 PM|LINK
Try
protected void Button1_Click(object sender, EventArgs e) { switch(((TextBox)sender).ID) { case "myTextBox": ... break;... default: ... break; } }ArtemL
Participant
1258 Points
251 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 02:44 PM|LINK
Hi.
How do you assign ID in Repeater1_ItemDataBound (code please) and why are you doing this?
I don't see it makes sense as RepeaterItem implements INamingContainer interface, so that each TextBox will have unique ID. So all you need is to set single ID in template and that's all. But maybe there is some reason you're doing this, clarify please.
I think you could do smth. like:
foreach (RepeaterItem item in Repeater1.Items) { TextBox textBox = (TextBox)item.FindControl("textBoxID"); ... }ArtemL
Participant
1258 Points
251 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 02:46 PM|LINK
Yojimbo2, Don't you think that sender of Button1_Click will always be a Button? [:P]
dedyandy
Member
47 Points
57 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 03:34 PM|LINK
The background is I am developing a web site which sells books. This my particular question is about displaying a shopping cart, after a user has chosen several books that he or she wants to buy.
I assign IDs for each of the textboxes in the ItemDataBound as follows:
TextBox TextBox1 = (TextBox)e.Item.FindControl("TextBox1");TextBox1.ID =
"Q" + ((int)data["ProductID"]).ToString();The reason I am doing this is because I want to keep the product ID (unique identifier for every book) in order to be able to refer to the product. The textboxes are for quantities of the books the user wants to buy. So, I need to get the IDs of the textboxes to get the product IDs.
I have tried in the button click event (assuming ProductID = 1):
TextBox tb = (TextBox) Page.FindControl("Q1"); or TextBox tb = (TextBox) Repeater1.FindControl("Q1");
Response.Write(tb.ID);
But, I got an error message from the "Response.Write(tb.ID);" line saying that it is a null reference.
Hopefully I have made a little clearer for my problem.
Thanks,
Andy.
ArtemL
Participant
1258 Points
251 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 30, 2007 03:50 PM|LINK
Hm...Now I've understood what you're wanting to achieve. But, you're moving slightly in wrong direction.
You don't have to determine product ID by changing ID value control, it breaks loading of ViewState after all, so that you got an issue.
My recommendation is following:
1. Leave ID of control as is in a aspx code.
2. Add to item template hidden element, just like this:
<asp:HiddenField runat=server ID="hdnProductID" Value="<%# Eval("ProductID") %>" />Then, on button click to retrieve product ID do: string prodID = ((HiddenField)e.Item.FindControl("hdnProductID")).Value;
dedyandy
Member
47 Points
57 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 31, 2007 12:22 AM|LINK
I added <asp:HiddenField runat="server" ID="HiddenField1" Value="<%# Eval("ProductID") %>" /> to item template, but I got the following error message:
"The server tag is not well formed."
Also, in the button click event the object "e" does not have Item property, as you have shown me of e.Item.FindControl("hdnProductID").
Andy.
ArtemL
Participant
1258 Points
251 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 31, 2007 07:44 AM|LINK
Replace double quotes with single quotes <asp:HiddenField runat="server" ID="HiddenField1" Value='<%# Eval("ProductID") %>' />
For the event, yes you right, to access Item you have to loop thru all items
dedyandy
Member
47 Points
57 Posts
Re: How to get TextBox IDs produced in Repeater in button click event
Jul 31, 2007 03:22 PM|LINK
Could you show me further of how to get each of the HiddenFields using foreach (RepeaterItem item in Repeater1.Items)?
Is the ProductID in Value='<%# Eval("ProductID") %>' from my database?