问题
Introduction
I am working on a college project where I need to build a shopping website.
I am currently building a "Manage Shopping Cart" page with the following structure:
What is important for my question is the "products". The "products" is a ASP.Net Repeater that only uses the ItemTemplate
control. The repeater is then populated with a list of products in a user's cart.
What is my problem?
The particular area I am having trouble with is the "Save" Button. The user can change the quantity of each product they want; however, they must click the "Save" button for the information to go to the database.
The trouble is accessing the correct quantity textbox inside my Save button's onclick event.
How can I access the correct text box in all of my Save button's OnClick Events?
What have I tried?
I was initially thinking of using the WebControl.FindControl method. Of course though, the FindControl requires an ID and I would need different IDs for each textbox.
In order to the fix this ID problem, I tried...
firstly...
Tried binding the productId to the textbox id.
<asp:TextBox ID='quantity<%#Eval("productId") %>' runat="server" TextMode="Number" min="1" Text='<%#Eval("selectedQuantity") %>' max='<%#Eval("availableQuantity") %>' />
I then found out that is not is possible; because, when setting an ID this way, you must use "simple ids" (Example: "button1"). Supposedly I could set the ID in the code-behind though.
Secondly...
I then tried using the Repeater's OnItemCreated Event to dynamically set the ID of the TextBox to "quantityX", where X is an Product ID.
What I did in is bind the productId into a WebControl attribute:
<asp:TextBox ID='quantity' productId='<%#Eval("productId") %>' runat="server" TextMode="Number" min="1" Text='<%#Eval("selectedQuantity") %>' max='<%#Eval("availableQuantity") %>' />
After this, I then using the FindControl()
method to get TextBoxs with an ID of "quantity". After finding the TextBox, I would take the productId attribute and append its value to the TextBox ID.
protected void productsList_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
TextBox quantity = (TextBox)e.Item.FindControl("quantity");
quantity.ID = String.Format("quantity{0}", quantity.Attributes["productId"]);
}
}
I then found out that the productId attribute did not have a value; because, the DataBinding has not occurred. In otherwords, Eval("productId")
was not running.
thirdly...
I took the same approach as the one before with 1 difference. I used the Repeater's OnItemDataBound
event instead of the OnItemCreated
Event.
protected void productsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item)
{
TextBox quantity = (TextBox)e.Item.FindControl("quantity");
quantity.ID = String.Format("quantity{0}", quantity.Attributes["productId"]);
}
}
This partially worked; however, there was one problem. The product with the biggest Product ID was skipped. (E.X, if I had products 4, 32, and 68. The Product ID of 68 would be skipped)
As you can see in the image below, we have 3 products. I edited some text the picture to clearly point out the TextBoxs (as they are rendered).
The IDs present are 1, 32 and 34. If you look at the ID attribute of 1 and 32, you will see they have "quantity" with their Product ID appended.
Now look at the ID attribute of 34. The ID attribute is only "quantity".
Now I am kinda of stuck.
What am I using?
- Visual Studio 2017
- .NET Framework 4.6.1
- 2-Tier WebForms Project
- Google Chrome
回答1:
To get text from textbox in your save button click use below code:
// get item on where button is clicked
RepeaterItem item = (RepeaterItem)((Button) sender).NamingContainer;
// get correct textbox from the item
TextBox txtQuantity = (TextBox)item.FindControl("quantity");
string qtyText = txtQuantity.Text;
In your third approach your're only checking DataItems but you have to check it on AlternatingItems also:
if (e.ItemType == ListItemType.Item || e.ItemType == ListItemType.AlternatingItem)
来源:https://stackoverflow.com/questions/46794933/how-to-access-specific-controls-in-a-asp-net-repeater