首页 > 解决方案 > 如何将我的 HTML 标签与 ASP.Net 单选按钮相关联?

问题描述

我有一个已将图像链接到的 HTML 标签,因此当您单击图像时,图像会突出显示并选择单选按钮(在图像下方)。我正在尝试将单选按钮转换为 ASP 单选按钮,以便我可以在后面的代码中引用它,但无法确定如何关联标签。

我认为这可能是因为它是 ASP 的 HTML,它可能不起作用,所以我尝试使用 ASP 标签,但据我所知,标签不允许您在其中包含图像。

这就是它的工作方式:

<div class="form-holder form-holder-2">
    <input type="radio" class="radio" name="radio1" id="p1" value="p1">
        <label class="plan-icon plan-1-label" for="p1">
            <img src="images/frmMM1.png" alt="p1">
        </label>
    <div class="plan-total">
        <span class="plan-title">Managing your money</span>
        <p class="plan-text">Placeholder</p>
    </div>
</div>

这是它现在的样子:

<div class="form-holder form-holder-2">
    <asp:RadioButton ID="rbMM1" AssociatedControlID="rbMM1" ClientIDMode="Static" runat="server" CssClass="radio" GroupName="rbMM" Checked="true" />
        <label class="plan-icon plan-1-label" for="rbMM1" >
            <img src="images/frmMM1.png" alt="p1">
        </label>
    <div class="plan-total">
        <span class="plan-title">Managing your money</span>
        <p class="plan-text">Placeholder</p>
    </div>
</div>

运行应用程序时,最初的一切看起来都一样,但单选按钮不可点击(来自图像)。有没有更好的方法可以实现上述目标?

标签: htmlasp.netasp.net-mvcvisual-studio

解决方案


请尝试runat="server"为您的标签添加属性。

<label class="plan-icon plan-1-label" for="rbMM1" runat="server">
    <img src="images/frmMM1.png" alt="p1">
</label>

这样我就可以在类后面的代码中使用该标签的值。让我知道它是否适合您。


推荐阅读