ASP.NET 动态更改中继器项内的下拉控件 ID
有人可以告诉我如何让它发挥作用吗? 我想区分中继器控件内的下拉控件。 我现在了解了生命周期以及缓冲区是如何写入的,但是我的选择是什么? 这是发生的情况
代码文件
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
Aspfile
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
<asp:ListItem Selected="True" Value="" Text=""/>
<asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
<asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
<asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
<asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
<asp:ListItem Selected="False" Value="Other" Text="Other"/>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
返回此错误
解析器错误消息:“AdTitle<%# Container.ItemIndex %>” 不是有效的标识符。
Can someone tell me how I can get this to work. I want to distinguish dropdown controls inside a repeater control. I understand now about the lifecyle and how the buffer is already writen, but what are my alternatives? Here is what happens
Code File
Dim repeatTimes((TotalAdInsured - 1)) As Integer
myRepeater.DataSource = repeatTimes
myRepeater.DataBind()
Aspfile
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="AdTitle<%# Container.ItemIndex %>" runat="server">
<asp:ListItem Selected="True" Value="" Text=""/>
<asp:ListItem Selected="False" Value="Miss" Text="Miss"/>
<asp:ListItem Selected="False" Value="Ms" Text="Ms"/>
<asp:ListItem Selected="False" Value="Mrs" Text="Mrs"/>
<asp:ListItem Selected="False" Value="Mr" Text="Mr"/>
<asp:ListItem Selected="False" Value="Other" Text="Other"/>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Returns this error
Parser Error Message: 'AdTitle<%# Container.ItemIndex %>' is not a valid identifier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不需要。 以下是一些可能对您有所帮助的示例代码:
标记:
代码隐藏:
You don't need to. Here's some sample code that might help you out:
Markup:
Codebehind:
您无法通过这样的标记在转发器内创建唯一 ID。 但是,您可以通过使用 FindControl 来检索代码隐藏中的下拉菜单复读机。
You cannot create a Unique ID inside a repeater through the markup like that. You can however retrieve the dropdown in the codebehind by using FindControl on the Repeater.
好吧......简而言之,您无法以当前正在做的方式做您想做的事情。
我可以看到你想要做什么,但我不太明白为什么......
中继器控件数据源中的每个项目将包含 1 个项目,因此只需调用 DropDownList
ID="AdTitle"
就可以了,因为这将是与下排的一个。为了让它们返回服务器端,您只需迭代中继器和中继器中的项目即可。
FindControl("AdTitle")
获取该项目的特定 DropDownList。如果绝对有必要让 ID 递增,则需要以编程方式执行此操作(可能在
ItemDataBound
事件上创建 DropDownList 并将其添加到 ItemTemplate 中。Ok... in a nutshell you can't do what you're trying to do in the manner you are currently doing it.
I can see what your trying to do, but I don't really understand Why...
A repeater control will contain 1 item, per item in your datasource, so it's perfectly fine to just call your DropDownList
ID="AdTitle"
as that will be a different'AdTitle'
from the one in the next row.To get them back server side, you would just iterate through the Items in your Repeater &
FindControl("AdTitle")
to get the specific DropDownList for that item.If it's absolutely necessary to have the IDs incrementing, you'll need to do this programmatically (probably on the
ItemDataBound
event to create a DropDownList and add it to your ItemTemplate.扩展 BC 的答案:我最近遇到了同样的问题,尽管有一个复选框,但我需要设置 ID,因为我在保留选择时使用了该 ID。 有点笨重,但我是在项目数据绑定时做到的。
这是标记:
我设置并绑定了此类的集合,
在 Page_Load 中执行必要的操作
,然后在此处设置 ID:
因此,当点击提交按钮时,我可以循环并获取已检查的 ID 并保存:
Expanding on BC's answer: I recently had the same issue, albeit with a checkbox, I needed to set the ID as I used that when persisting choices. A bit clunky but I did it when on Item Data Bound.
Here's the markup:
I set up and bound a collection of this class
Do the necessary in Page_Load
Followed by, this where I set ID:
So when the submit button is hit I can loop thru and grab the Id of those checked and save:
您可以向 Repeater.ItemDataBound 添加事件处理程序,并使用对项容器的引用在处理程序内创建控件。
You could add an event handler to Repeater.ItemDataBound and create the control within the handler using the reference to the item container.