0

我的 aspx 页面中有一个中继器控件。我在页面加载事件中将数据绑定到它。它正在显示中继器控件。但是,当我尝试在 repeater_ItemDataBound 事件中查找中继器控件中存在的控件时,我得到 System.NullReferenceException: Object reference not set to an instance of an object。

以下是我的 aspx 页面中的中继器控件,

<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>

以下是 ItemDataBound 事件,

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
                }
            }
        }
    }
}
4

1 回答 1

1

尝试添加

runat="server"

到您的单选按钮输入控件

编辑:您不能将 ID 动态绑定到您的控件。

<input id="rdoYes" value="YES" type="radio" runat="server" />

可以通过以下方式对 OnItemDataBound 事件进行操作:

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
    }
}
于 2012-08-02T05:29:02.683 回答