2012-08-02 58 views
0

我在我的aspx页面有一个repeator控件。我在页面加载事件中绑定了数据。它显示中继器控制。但是,当我尝试查找Repeater_ItemDataBound事件中的repeater控件中存在的控件时,我得到System.NullReferenceException:未将对象引用设置为对象的实例。如何找到Repeater Control中的控件?

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

尝试添加

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 
    } 
} 
+0

我得到错误 - 'rdoYes <%#Container.ItemIndex%>不是一个有效的标识符。 – 2012-08-02 05:31:55

相关问题