如何使用 Repeater 的 ItemIndex 从字典中检索值?
我得到了一个需要修改的页面,它有一个带有 ItemTemplate 的 Repeater,其中包含用于输入人员地址的字段。每个地址字段看起来像这样
<input type="text" id="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
name="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>" />
您可以看到我们将转发器的 ItemIndex 附加到文本框的 html id 和名称字段的末尾。
在代码隐藏中,我们有一个名为 DataProxy 的类,它基本上是一个基于 Request.Params 构建的字典,我们通常使用它来用用户输入的内容重新填充表单。例如,他们将数据放在表单上并提交,但验证失败,因为几个字段为空,但我们希望重新填充他们之前输入的内容。我们会这样做:
<input type="text" name="myTextBox" id="myTextBox"
value="<%: DataProxy["myTextBox"].ToString() %>" />
通常这工作得很好,但它在这个中继器中不起作用,我相信这是因为中继器的 DataBind 发生在我们已经在 DataProxy 字典中搜索了我们的值之后。这是我正在使用的代码:
<input type="text" id="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
name="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
value="<%# DataProxy["line1" + (((RepeaterItem)Container).ItemIndex + 1).ToString()] %>" />
有没有一种方法可以使用 Repeater 中的 ItemIndex 属性访问我的 DataProxy 字典?
请注意,我的中继器在 Page_Load() 中进行数据绑定。
I was given a page that I need to modify and it has a Repeater with an ItemTemplate that contains the fields to enter a person's address. Each address field looks something like this
<input type="text" id="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
name="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>" />
You can see that we are appending the ItemIndex of the repeater onto the end of the html id and name fields for the text box.
In the Code-behind we have a class called DataProxy which is basically a dictionary that is built off of Request.Params and we normally use it to re-populate a form with what a user entered. For example, they put data on a form, submit it, but validation fails because a couple of fields are empty, but we want to re-populate what they had previously entered. We would do that like this:
<input type="text" name="myTextBox" id="myTextBox"
value="<%: DataProxy["myTextBox"].ToString() %>" />
Normally this works great, but it's not working inside this repeater and I believe it is because the DataBind for the repeater occurs after we have already searched the DataProxy dictionary for our value. This is the code I'm using:
<input type="text" id="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
name="line1<%# ((RepeaterItem)Container).ItemIndex + 1%>"
value="<%# DataProxy["line1" + (((RepeaterItem)Container).ItemIndex + 1).ToString()] %>" />
Is there a way that I can access my DataProxy dictionary with the ItemIndex property from the Repeater?
Just a note, my repeater is being Data-bound in Page_Load().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将字典转换为列表,
但是,您可能希望确保数据已排序,以便中继器的显示一致。
Convert the dictionary to a list,
However, you will probably want to ensure the data is sorted so your repeater is consistently displayed.