如何访问 ASP.Net 中继器中的特定控件 [英] How to access specific controls in a ASP.Net Repeater

查看:17
本文介绍了如何访问 ASP.Net 中继器中的特定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

我正在做一个大学项目,我需要建立一个购物网站.

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:

对我的问题来说重要的是产品".产品"是只使用 ItemTemplate 控件的 ASP.Net 中继器.然后将用户购物车中的产品列表填充到转发器中.

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.

我的问题是什么?

我遇到问题的特定区域是保存"按钮.用户可以改变他们想要的每种产品的数量;但是,他们必须单击保存"按钮才能将信息转到数据库.

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.

问题是访问我的保存按钮的 onclick 事件中的正确数量文本框.

The trouble is accessing the correct quantity textbox inside my Save button's onclick event.

如何在所有保存按钮的 OnClick 事件中访问正确的文本框?

How can I access the correct text box in all of my Save button's OnClick Events?

我尝试了什么?

我最初考虑使用 WebControl.FindControl 方法.当然,FindControl 需要一个 ID,我需要为每个文本框设置不同的 ID.

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.

为了解决这个ID问题,我尝试了...

In order to the fix this ID problem, I tried...

首先...

尝试将 productId 绑定到文本框 id.

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") %>' />

然后我发现这是不可能的;因为,以这种方式设置 ID 时,您必须使用简单 ID"(例如:button1").据说我可以在代码隐藏中设置 ID.

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.

其次...

然后我尝试使用 Repeater 的 OnItemCreated 事件将 TextBox 的 ID 动态设置为quantityX",其中 X 是产品 ID.

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.

我所做的是将 productId 绑定到 WebControl 属性中:

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") %>' />

此后,我使用 FindControl() 方法获取 ID 为数量"的文本框.找到 TextBox 后,我将获取 productId 属性并将其值附加到 TextBox ID.

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"]);
    }
}

然后我发现 productId 属性没有值;因为,DataBinding 还没有发生.换句话说,Eval("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.

第三...

我采用了与之前相同的方法,但有 1 个不同之处.我使用了中继器的 OnItemDataBound 事件而不是 OnItemCreated 事件.

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"]);
    }
}

这部分起作用了;然而,有一个问题.产品 ID 最大的产品被跳过.(例如,如果我有产品 4、32 和 68.产品 ID 68 将被跳过)

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)

如下图所示,我们有 3 种产品.我编辑了图片中的一些文本以清楚地指出文本框(在渲染时).

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).

显示的 ID 为 1、32 和 34.如果您查看 1 和 32 的 ID 属性,您会看到它们带有附加了产品 ID 的数量".

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.

现在看34的ID属性,ID属性只有数量".

Now look at the ID attribute of 34. The ID attribute is only "quantity".

现在我有点卡住了.

我在用什么?

  • Visual Studio 2017
  • .NET 框架 4.6.1
  • 2-Tier WebForms 项目
  • 谷歌浏览器

推荐答案

要从保存按钮中的文本框中获取文本,请单击使用以下代码:

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;

在您的第三种方法中,您只检查 DataItems,但您还必须在 AlternatingItems 上检查它:

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)

这篇关于如何访问 ASP.Net 中继器中的特定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆