使用 RadioButtonList 时可以控制标签吗?

发布于 2024-08-13 14:17:39 字数 1172 浏览 11 评论 0原文

这个

protected void Page_Load(object sender, EventArgs e) {
    RadioButtonList1.Items.Add(new ListItem("London","1"));
    RadioButtonList1.Items.Add(new ListItem("Paris", "2"));
}

和这个

   <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatLayout="Flow">
   </asp:RadioButtonList></div>

会产生类似这样的 HTML

   <input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="1" />
   <label for="RadioButtonList1_0">London</label>
   <input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="2" />
   <label for="RadioButtonList1_1">Paris</label>

,但我真正想要的是这个,注意 标记中的类。

    <input type="radio" name="Event" id="whatever" value="1" /> 
    <label for="London" class="London">London</label> 
    <input type="radio" name="Event" id="whatever" value="2" /> 
    <label for="Paris" class="Paris">Paris</label> 

我可以将 cssClass 添加到自动生成的 标签?

This

protected void Page_Load(object sender, EventArgs e) {
    RadioButtonList1.Items.Add(new ListItem("London","1"));
    RadioButtonList1.Items.Add(new ListItem("Paris", "2"));
}

and this

   <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatLayout="Flow">
   </asp:RadioButtonList></div>

produce HTML something like this

   <input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="1" />
   <label for="RadioButtonList1_0">London</label>
   <input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="2" />
   <label for="RadioButtonList1_1">Paris</label>

but what I really want is this, note the class in the <label> tag.

    <input type="radio" name="Event" id="whatever" value="1" /> 
    <label for="London" class="London">London</label> 
    <input type="radio" name="Event" id="whatever" value="2" /> 
    <label for="Paris" class="Paris">Paris</label> 

Can I add a cssClass to the automatically generated <label> tag?

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

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

发布评论

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

评论(2

魔法唧唧 2024-08-20 14:17:39

您可以从 RadioButtonList 继承,如图所示 此处此处

最后一个链接可能更符合您的要求。

您应该只需要重写 RenderItem 方法,如下所示:

protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
    RadioButton radioButton = new RadioButton();
    radioButton.Page = this.Page;
    radioButton.GroupName = this.UniqueID;
    radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
    radioButton.Text = this.Items[repeatIndex].Text;            
    radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
    radioButton.Checked = this.Items[repeatIndex].Selected;
    radioButton.TextAlign = this.TextAlign;
    radioButton.AutoPostBack = this.AutoPostBack;
    radioButton.TabIndex = this.TabIndex;
    radioButton.Enabled = this.Enabled;

    radioButton.CssClass = InnerCssClass;
    radioButton.Style.Add(HtmlTextWriterStyle.BackgroundColor, this.Items[repeatIndex].Text);

    radioButton.RenderControl(writer);

    // add new label
    Label radioLabel = new Label();
    radioLabel.Text = this.Items[repeatIndex].Text;
    radioLabel.CssClass = this.Items[repeatIndex].Text;
    radioLabel.AssociatedControlID = this.ClientID + "_" + repeatIndex.ToString();
    radioLabel.RenderControl(writer);
}

注意,我实际上尚未编译上面的代码,但应该足以指导您正确的方向:)

You could inherit from RadioButtonList, like shown here or here.

The last link is probably more in line with what you want.

You should only need to override the RenderItem method, like so:

protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
    RadioButton radioButton = new RadioButton();
    radioButton.Page = this.Page;
    radioButton.GroupName = this.UniqueID;
    radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
    radioButton.Text = this.Items[repeatIndex].Text;            
    radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
    radioButton.Checked = this.Items[repeatIndex].Selected;
    radioButton.TextAlign = this.TextAlign;
    radioButton.AutoPostBack = this.AutoPostBack;
    radioButton.TabIndex = this.TabIndex;
    radioButton.Enabled = this.Enabled;

    radioButton.CssClass = InnerCssClass;
    radioButton.Style.Add(HtmlTextWriterStyle.BackgroundColor, this.Items[repeatIndex].Text);

    radioButton.RenderControl(writer);

    // add new label
    Label radioLabel = new Label();
    radioLabel.Text = this.Items[repeatIndex].Text;
    radioLabel.CssClass = this.Items[repeatIndex].Text;
    radioLabel.AssociatedControlID = this.ClientID + "_" + repeatIndex.ToString();
    radioLabel.RenderControl(writer);
}

Note, I have not actually compiled above code, but should be enough to guide you in a proper direction :)

耳钉梦 2024-08-20 14:17:39

我不是 ASP.NET 专业人士,但我确实知道您可以通过使用子选择器轻松控制具有已知 ID 的元素内 LABEL 的显示。

#myElementID LABEL {
  padding: 5px;
}

I'm not an ASP.NET pro, but I do know that you can easily control the presentation of the LABEL's inside your an element with a known ID by using child selectors.

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