2012-08-02 110 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

相關問題