ASP.NET 中继器内的 HTML 单选按钮 C#

发布于 2024-08-13 04:04:02 字数 631 浏览 5 评论 0原文

我试图简单地在中继器内绑定一个 HTML 输入单选按钮:

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" checked="
<%#`GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))%>" />`

    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

GetSelectedValue 方法只是返回一个字符串“checked”以选择单选按钮,假设中继器有 3 个项目要循环,并且标记何时呈现,如果我检查元素,我看到第一个单选按钮的正确值,即检查=“检查”,但在用户界面上,第三个单选按钮被选中,即使其检查值=“假”,有人可以指导我我做错了什么吗?

I am trying to simply bind a HTML Input radio button inside a repeater :

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" checked="
<%#`GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))%>" />`

    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

The GetSelectedValue method simply returns a string "checked" for the radio button to be selected, lets say the repeater had 3 items to loop and when the markup renders and If I inspect the element I see the correct value for the first radio button i.e. checked="checked" but on the UI the 3rd radio button gets selected even though its checked value="false", can someone guide me as to what i am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月光色 2024-08-20 04:04:02

尝试一下

<%# (bool)Eval("IsDefaultSelection") ? " checked=\"checked\" " : string.Empty %>

基本上,包含 checked 属性会导致 RadioButton 被检查。你想把它拉出来,这样整个东西就会被渲染出来,或者整个东西就会消失。

另请记住,您的所有 RadioButton 都将被命名为 Test1 使用服务器端 RadioButton 可能是个好主意。

Give this a try

<%# (bool)Eval("IsDefaultSelection") ? " checked=\"checked\" " : string.Empty %>

Basically the inclusion on the checked attribute is causing the RadioButton to be checked. You want to pull it out so the entire thing is rendered or the entire thing is absent.

Also keep in mind all your RadioButtons are going to be named Test1 It might be a good idea to use a server side RadioButton.

云淡风轻 2024-08-20 04:04:02

HTML 中的 CHECKED 属性没有与之关联的值。仅当您想选择该属性时才应该使用它。

您想要类似的东西:

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" <%
    if (GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))
        response.write("CHECKED");
%> />
    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

否则您可以简单地使用 ASP.NET INPUT 控件。

The CHECKED attribute in HTML has no value associated with it. You should only use this attribute if you want to select it.

You want something like:

<asp:Repeater ID="Outlets" runat="server" >
 <ItemTemplate>
    <input type="radio" name="Proposal" value="Test1" id="Radio1" <%
    if (GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))
        response.write("CHECKED");
%> />
    <label for="Test1">
              <%#DataBinder.Eval(Container.DataItem, "Label")%>
    </label>

  </ItemTemplate>
</asp:Repeater>

Otherwise you could simply use the ASP.NET INPUT controls.

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