C# 是否可以以编程方式隐藏 radiobuttonlist 的特定列表项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15791936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to hide a particular list item of radiobuttonlist programmatically?
提问by
I have used gridview in my aspx page.. In that I have a list with six radio buttons in a single cell aligned horizontally.
我在我的 aspx 页面中使用了 gridview .. 在那个列表中,我有一个在单个单元格中水平对齐的六个单选按钮的列表。
I need the 1st radio button to be hidden. How to achieve that programmatically?
我需要隐藏第一个单选按钮。如何以编程方式实现这一目标?
<asp:GridView ID="CrowdRatingGrid" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="4" OnPageIndexChanging="CrowdRatingGrid_PageIndexChanging" ViewStateMode="Enabled">
<PagerSettings Mode="Numeric" PageButtonCount="4" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Rating
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList runat="server" ID="Rating" SelectedValue='<%# Bind("rating_id") %>'
RepeatDirection="Horizontal">
<asp:ListItem Value="0" />
<asp:ListItem Value="1" />
<asp:ListItem Value="2" />
<asp:ListItem Value="3" />
<asp:ListItem Value="4" />
<asp:ListItem Value="5" />
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I need the List Item with value=0 to be hidden. How would I achieve this?
我需要隐藏 value=0 的列表项。我将如何实现这一目标?
回答by Mike Perrenoud
Yes, you can hide one by setting its Enabled
property to false
:
是的,您可以通过将其Enabled
属性设置为来隐藏一个false
:
Rating.Items[0].Enabled = false;
Editing based on comment by OP. To completely get rid of it you'll need to do this:
根据 OP 的评论进行编辑。要完全摆脱它,您需要这样做:
Rating.Items.RemoveAt(0);
and then when you want it back you'll need to do this:
然后当你想要它回来时,你需要这样做:
Rating.Items.Insert(0, "0");
回答by Mike Devenney
EDIT: Ok, I dug into this a little deeper and see how .NET renders the RadioButtonList control when you set it to Horizontal (it's a table with <td>
's for each item).
编辑:好的,我对此进行了更深入的研究,看看当您将 RadioButtonList 控件设置为 Horizontal 时,.NET 如何呈现 RadioButtonList 控件(它是一个表,<td>
每个项目都有's)。
Try this:
尝试这个:
var bli = Rating.Items[ 0 ];
bli.Attributes.Add( "hidden", "hidden" );
回答by Ramraj
It's working fine.
它工作正常。
RadioButtonList.Items[index].Enable=True/False;
Ex:
前任:
rdTypePackage.Items[1].Enabled = false;
回答by Ravi Gehlot
radioListID.Items[index].Enabled = true/false;
It will work 100%.
它将 100% 工作。
回答by Gideon Mulder
Old thread, though to achieve this you can do:
旧线程,但要实现此目的,您可以执行以下操作:
//Ensure the item is disabled
rblRating.Items[0].Enabled = false;
//Ensure the item is not selected
rblRating.Items[0].Selected = false;
//next row of code is hiding your radio button list item through css
rblRating.Items[0].Attributes[HtmlTextWriterStyle.Visibility] = "hidden";
回答by Jeff Mergler
ListItem li = Rating.Items(0);
Rating.Items.Remove(li);