posted by 네코냥이 2014. 12. 9. 16:03

I have a radio button list whose items I need to add on Page_Load

aspx code

<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

I keep getting the Object reference not set to instance of an object error

What am I doing wrong?

share|improve this question
2 
Is radio1 inside another asp.net control like a repeater or something like that? –  Claudio Redi Aug 12 '13 at 15:10
3 
You can access the radioButtonList from code behind just calling the ID radio1.Items.Add(new ListItem("Apple", "1")); –  Fals Aug 12 '13 at 15:11 
   
If its on same page why dont you directly write radio1.Items.Add? If not please edit your question. –  KuzgunAug 12 '13 at 15:12
   
@Claudio Redi: It is embedded inside a series of <div> elements –  user544079 Aug 12 '13 at 15:13
   
Yes, I can access it now using radio1.Items.Add –  user544079 Aug 12 '13 at 15:14

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"

protected void Page_Load(object sender, EventArgs e)
{
    radio1.Items.Add(new ListItem("Apple", "1"));
}
share|improve this answe


'.NET > C#' 카테고리의 다른 글

C# console application keep alive. 콘솔 꺼짐 방지  (146) 2016.04.08
[Visual Studio] 검색 정규식  (0) 2014.12.12
Static Constructor in C  (0) 2014.09.06
실버라이트 개요  (0) 2014.05.13
[C#] PCL 라이브러리 (Portable Class Library)  (0) 2014.05.13