Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net中以编程方式添加单选按钮列表项_C#_Asp.net - Fatal编程技术网

C# 在asp.net中以编程方式添加单选按钮列表项

C# 在asp.net中以编程方式添加单选按钮列表项,c#,asp.net,C#,Asp.net,我有一个单选按钮列表,其中的项目需要添加到Page\u Load aspx代码 <asp:radioButtonList ID="radio1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal"> </asp:radioButtonList> 在控件到达radioList.Items.Add后 我一直得到未设置为对象实例的对象引用 错误 我做错了什么?您不需要执行FindCOntrol。使用r

我有一个单选按钮列表,其中的项目需要添加到
Page\u Load

aspx代码

<asp:radioButtonList ID="radio1" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
</asp:radioButtonList>
在控件到达radioList.Items.Add后

我一直得到未设置为对象实例的
对象引用
错误


我做错了什么?

您不需要执行FindCOntrol。使用runat=“server”属性时,只需通过其名称“radio1”获取放射科医生的参考

利用

RadioButtonList radioList = (RadioButtonList)Page.FindControl("radio1");
radioList.Items.Add(new ListItem("Apple", "1"));
您不是在页面的控件上添加列表,而是在名为radioList的未实例化Radiobuttonlist上添加列表

如果可以从类访问该页面,请使用

radio1.Items.Add(new ListItem("Apple", "1"));

你必须加上!ispostback

if (!IsPostBack)
    {
        radio1.Items.Add(new ListItem("Apple", "1"));
    }

作为使用asp:***>工具的替代方法-
我需要重用一个单选选项,它依赖于站点中大量的jQuery集成。(还希望避免CSS将内容隐藏在aspx页面的html代码中。)

单选按钮只需出现在“编辑”页面中,具体取决于codebehind中的安全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());
}
string RadioOnChk1=(db.fieldChecked==true)?“checked='checked'”:;
字符串RadioOnChk2=(db.fieldChecked==false)?“checked='checked'”:;
如果(ACU>3)
{
//创建带有预选中项的单选按钮
StringBuilder RadioButtns=新建StringBuilder();//表单输入值
{
RadioButtons.追加(“包括或”);
RadioButtons.附加(“除外”);
RadioButtons.追加(“

”); } htmlVariable=(RadioButtns.ToString()); }

它起作用了。。这是一种错误的处理方式吗?

radio1
在另一个asp.net控件中,如中继器或类似的东西吗?您只需调用ID radio1.Items.Add(new ListItem(“Apple”,“1”))即可从代码后面访问RadioButton列表;如果在同一页上,为什么不直接写radio1.Items.Add?如果没有,请编辑您的问题。@Claudio Redi:它嵌入在一系列元素中,我现在可以使用radio1.Items.AddThis part:RadioButtonList radioList=(RadioButtonList)Page.FindControl(“radio1”);仍然不起作用。在使用“多视图”防止多次添加项目时,这一点非常重要。谢谢你,伙计!
if (!IsPostBack)
    {
        radio1.Items.Add(new ListItem("Apple", "1"));
    }
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());
}