更改中继器内部的 WebControl ID

发布于 2024-07-21 07:22:49 字数 614 浏览 7 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所谓啦 2024-07-28 07:22:49

我认为您最好使用中继器内对 ID 的内置支持。 如果目标是为其分配一个 ID,以便在绑定数据后轻松找到正确的控件,您可以尝试如下操作:

<asp:Repeater ID="Repeater1" runat="server>
    <ItemTemplate>
        <asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
        <asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

然后,在代码中,您可以循环遍历 Repeater 中的项目,直到找到标签您正在寻找:

foreach (RepeaterItem curItem in Repeater1.Items)
{
    // Due to the way a Repeater works, these two controls are linked together.  The questionID
    // label that is found is in the same RepeaterItem as the DropDownList (and any other controls
    // you might find using curRow.FindControl)
    var questionID = curRow.FindControl("QuestionID") as Label;
    var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}   

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:

<asp:Repeater ID="Repeater1" runat="server>
    <ItemTemplate>
        <asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
        <asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:Repeater>

Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:

foreach (RepeaterItem curItem in Repeater1.Items)
{
    // Due to the way a Repeater works, these two controls are linked together.  The questionID
    // label that is found is in the same RepeaterItem as the DropDownList (and any other controls
    // you might find using curRow.FindControl)
    var questionID = curRow.FindControl("QuestionID") as Label;
    var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}   

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.

以酷 2024-07-28 07:22:49

您可以从标记文件中删除该控件,并挂钩转发器的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文