如何找到Repeater Control中的控件?

时间:2012-08-02 05:25:01

标签: asp.net repeater

我的aspx页面中有一个重复控件。我在页面加载事件上绑定数据。它正在显示转发器控件。但是当我尝试在repeater_ItemDataBound事件中找到转发器控件中存在的控件时,我得到System.NullReferenceException:对象引用未设置为对象的实例。

以下是我的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
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试添加

runat="server"

到您的radiobutton输入控件

编辑: 您无法动态地将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
    }
}
相关问题