更改中继器内部的 WebControl ID
<ItemTemplate>
<asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
<asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
<asp:ListItem value="1" text="Yes" />
<asp:ListItem value="0" text="No" />
</asp:DropDownList>
<ItemTemplate>
这大致就是我想要做的。 显然,实现是错误的,但我找不到任何关于如何在实践中实现这一点的信息。 任何帮助表示赞赏。
编辑:我想要做的正是为此中继器中的每个项目添加一个 DropDownList,并在提交表单后,使用每个是/否答案的 ID 输入到数据库中。 我使用的 SqlDataReader 有两个字段:问题内容和问题 ID。
<ItemTemplate>
<asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
<asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
<asp:ListItem value="1" text="Yes" />
<asp:ListItem value="0" text="No" />
</asp:DropDownList>
<ItemTemplate>
This is roughly what I'm trying to do. Obviously, the implementation is faulty, but I can't find any information on how I'd go about this in practice. Any help is appreciated.
Edit: What I'm trying to do exactly is add a DropDownList for each item in this Repeater, and upon submission of the form, use the ID's of each Yes/No answer to input into a database. The SqlDataReader that I'm using has two fields: The question content and the questionID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您最好使用中继器内对 ID 的内置支持。 如果目标是为其分配一个 ID,以便在绑定数据后轻松找到正确的控件,您可以尝试如下操作:
然后,在代码中,您可以循环遍历 Repeater 中的项目,直到找到标签您正在寻找:
Repeater 基本上由 RepeaterItems 的集合组成。 RepeaterItems 使用 ItemTemplate 标签指定。 每个 RepeaterItem 都有自己的一组控件,根据 Repeater 的本质,这些控件彼此关联。
假设您正在从数据库中提取中继器数据。 每个 Repeater 项代表查询结果中单独行的数据。 因此,如果将 QuestionID 分配给标签,将 QuestionName 分配给 DropDownList,标签中的 ID 将与下拉列表中的名称匹配。
I think you'd be better off using the built in support for IDs inside a Repeater. If the goal is to assign it an ID to make it easy to find the proper control after the data has been bound you might try something like this:
Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:
A Repeater basically consists of a collection of RepeaterItems. The RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls that are, by the very nature of a Repeater, associated with each other.
Say you're pulling the Repeater data from a database. Each Repeater item represents data from an individual row in the query results. So if you assign the QuestionID to a label and the QuestionName to a DropDownList, the ID in the label would match up with the name in drop down.
您可以从标记文件中删除该控件,并挂钩转发器的 onItemDataBound 事件吗? 在这种情况下,您应该能够“手动”创建下拉控件,分配您想要的任何 ID。
Could you remove the control from the markup file, and hook the repeater's onItemDataBound event. In that event, you should be able to create the dropdown control "manually", assigning whatever ID you want.