C# 在 asp.net 中以编程方式添加单选按钮列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18190522/
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
Add radio button list items programmatically in asp.net
提问by user544079
I have a radio button list whose items I need to add on Page_Load
我有一个单选按钮列表,我需要添加其中的项目 Page_Load
aspx code
aspx代码
<asp:radioButtonList ID="radio1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
</asp:radioButtonList>
code behind
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList radioList = (RadioButtonList)Page.FindControl("radio1");
radioList.Items.Add(new ListItem("Apple", "1"));
}
After the control reaches radioList.Items.Add
控制到达后 radioList.Items.Add
I keep getting the Object reference not set to instance of an object
error
我不断收到Object reference not set to instance of an object
错误
What am I doing wrong?
我究竟做错了什么?
采纳答案by Guigui
You don't need to to do a FindCOntrol. As you used the runat="server" attributes, just get the reference of your RadioList via its name "radio1"
您不需要执行 FindCONtrol。当您使用 runat="server" 属性时,只需通过其名称“radio1”获取 RadioList 的引用
protected void Page_Load(object sender, EventArgs e)
{
radio1.Items.Add(new ListItem("Apple", "1"));
}
回答by Matt B-L
By using
通过使用
RadioButtonList radioList = (RadioButtonList)Page.FindControl("radio1");
radioList.Items.Add(new ListItem("Apple", "1"));
you are not adding your list on the control on your page, but on an un-instanciated Radiobuttonlist called radioList.
您不是在页面上的控件上添加列表,而是在名为 radioList 的未实例化的 Radiobuttonlist 上添加列表。
If the page is reachable from the class, use
如果该页面可从类访问,请使用
radio1.Items.Add(new ListItem("Apple", "1"));
回答by sk2185
you must add !ispostback
你必须添加 !ispostback
if (!IsPostBack)
{
radio1.Items.Add(new ListItem("Apple", "1"));
}
回答by Martin Sansone - MiOEE
As an alternative to using the < asp:**> tools -
I needed to reuse a radio option which relies on a lot of jQuery integration in the site. (Also wanted to avoid just CSS hiding the content within the html code of the aspx page.)
作为使用 < asp:**> 工具的替代方案-
我需要重用一个单选选项,该选项依赖于站点中的大量 jQuery 集成。(还想避免仅 CSS 将内容隐藏在 aspx 页面的 html 代码中。)
The radio buttons needed only appear in an 'edit' page depending on security ACU level logic within the codebehind and rendered with currently stored item value data found in the db. So I used the following:
所需的单选按钮仅出现在“编辑”页面中,具体取决于代码隐藏中的安全 ACU 级别逻辑,并使用在数据库中找到的当前存储的项目值数据呈现。所以我使用了以下内容:
string RadioOnChk1 = (db.fieldChecked == true) ? "checked='checked'" : "";
string RadioOnChk2 = (db.fieldChecked == false) ? "checked='checked'" : "";
if (ACU > 3)
{
// Create radio buttons with pre-checked
StringBuilder RadioButtns = new StringBuilder(); // Form input values
{
RadioButtns.Append("<p><label><input type=\"radio\" id=\"radiocomm1\" name=\"custmComm\" value=\"1\"");
RadioButtns.Append(RateIncChk1 + "/>Included or </label>");
RadioButtns.Append("<label><input type=\"radio\" id=\"radiocomm2\" name=\"custmComm\" value=\"2\"");
RadioButtns.Append(RateIncChk2 + "/>Excluded</label>");
RadioButtns.Append("</p>");
}
htmlVariable = (RadioButtns.ToString());
}
It works.. Is this a wrong way of going about it?
它有效..这是一种错误的处理方式吗?