Reputation: 406
I have a repeator control in my aspx page. I am binding data to it on page load event. It is displaying the repeater control. But when I try to find the controls present in repeater control in repeater_ItemDataBound event, I get System.NullReferenceException: Object reference not set to an instance of an object.
Following is the repeator control in my aspx page,
<asp:Repeater ID="rptrSurvey" runat="server" OnItemDataBound="rptrSurvey_ItemDataBound">
<HeaderTemplate>
<table cellpadding='0' cellspacing='0' width='100%' class='tableClass'>
<tr class='trClass' align='right'>
<th class='full'>Id</th>
<th class='full'>Questions</th>
<th class='full'>Answers</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="sr<%#Container.ItemIndex %>" class='trClass' style='width:100%'>
<td id="st1<%#Container.ItemIndex %>" class='first' style='width:5%'> <%#Eval("question_id") %></td>
<td id="st2<%#Container.ItemIndex %>" class='first' style='width:60%'><%#Eval("question_description") %></td>
<div id="Div1" runat="server" Visible='<%# Convert.ToInt32(Eval("question_type_id")) == 1 %>' >
<td id="st3<%#Container.ItemIndex %>" class='last' style='width:35%'>
<input id="rdoYes<%#Container.ItemIndex %>" value="YES" name="yes_no_na<%#Container.ItemIndex %>" type="radio"></input>YES
<input id="rdoNo<%#Container.ItemIndex %>" value="NO" name="yes_no_na<%#Container.ItemIndex %>" type="radio"></input>NO
<input id="rdoNa<%#Container.ItemIndex %>" value="N/A" name="yes_no_na<%#Container.ItemIndex %>" type="radio"></input>N/A
</td>
</div>
<div id="Div2" runat="server" visible='<%# Convert.ToInt32(Eval("question_type_id")) != 1 %>'>
<td id="st4<%#Container.ItemIndex %>" class='last' style='width:35%'>
<textarea id="txtComment<%#Container.ItemIndex %>" cols="1" rows="1" style='width:98%; height:100px'></textarea>
</td>
</div>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Following is the ItemDataBound Event,
protected void rptrSurvey_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
var m = HRLetterDatabase.GetSurveyData(hSurveyId.Value);
foreach (var row in m)
{
if (row.question_type_id == 1)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HtmlInputRadioButton inputYes = (HtmlInputRadioButton)e.Item.FindControl("rdoYes" + e.Item.ItemIndex);
if (row.answer == "YES")
{
inputYes.Checked = true;
//do something with val
}
}
}
}
}
Upvotes: 0
Views: 1743
Reputation: 46
try adding
runat="server"
to your radiobutton input controls
edit: you cannot dynamically bind IDs to your controls.
<input id="rdoYes" value="YES" type="radio" runat="server" />
can be manipulated on the OnItemDataBound event by:
protected void rptrSurvey_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HtmlInputRadioButton inputYes = (HtmlInputRadioButton)e.Item.FindControl("rdoYes");
// set value as required
}
}
Upvotes: 1