2014-07-02 25 views
0

我從數據庫中獲取記錄列表並將其存儲在DataTable,然後將其轉化爲System.Web.UI.WebControls.Table增加無線電控制的C#表

後,添加一個單選按鈕控件System.Web.UI.HtmlControls.HtmlInputRadioButton每個Table行。

,但我在網上收到錯誤的Object reference not set to an instance of an object.tOutput.RenderControl(new HtmlTextWriter(sw));

代碼:

string sReturn = ""; 
Table tOutput; 
HtmlInputRadioButton rdoProperty; 
StringWriter sw = new StringWriter(); 
try 
{ 
    DataTable dtOutput = new DataTable(); 
    dtOutput = IstaDAL.getPropertyList(sProjectID); 

    if (dtOutput.Rows.Count > 0) 
    { 
     tOutput = convertDataTable2HTMLTable(dtOutput, true, true, false); 

     foreach(TableRow trOutput in tOutput.Rows) 
     { 
      if (trOutput.TableSection == TableRowSection.TableBody) 
      { 
       rdoProperty = new HtmlInputRadioButton(); 
       rdoProperty.Attributes["value"] = trOutput.Cells[1].Text; 
       rdoProperty.Attributes["name"] = "rdoProperty"; 
       trOutput.Cells[0].Controls.Add(rdoProperty); 
      } 
     } 

     tOutput.RenderControl(new HtmlTextWriter(sw)); 
     sReturn = sw.ToString(); 
    } 
} 

編輯:

當我試圖進去rdoProperty做了一些研究,我發現他的一個財產get_RenderedNameAttribute導致該異常

堆棧跟蹤:

at System.Web.UI.HtmlControls.HtmlInputRadioButton.get_RenderedNameAttribute() 
+0

是否'convertDataTable2HTMLTable()''返回任何null'條件下,想出了? –

+0

@GrantWinney:不,它不。實際上,它返回'HtmlTable',正如我可以在'trOutput.Cells [1] .Text;'中看到的值每次迭代 – Shaggy

回答

-1

做任何人有更好的解決方案呢?

不使用該

trOutput.Cells[0].Controls.Add(rdoProperty); 

的我現在有了這個

trOutput.Cells[0].Text = "<input type='radio' name='some name'/>"; 
0

試試這個:

if(tOutput!=null) 
{  
using (var htmlWriter = new HtmlTextWriter(sw)) { 
     tOutput.RenderControl(htmlWriter); 
    } 
} 
    sReturn=sw.ToString(); 
+0

不起作用。它會進入'if'循環,但同樣的錯誤。檢查stacktrace的問題編輯 – Shaggy

+0

嘗試在每個iteration.rdoProperty.Attributes [「name」] =「rdoProperty _」+ counter中給出唯一的名稱; – malkam

+0

我猜'name'必須與單選按鈕控件相同? – Shaggy