2013-12-23 34 views
1

我嘗試從C#如何從C#

<select> 
    <option value="volvo">Volvo</option> 
    <option value="saab" selected>Saab</option> 
    <option value="opel">Opel</option> 
    <option value="audi">Audi</option> 
</select> 

我的主要目標實現下面的HTML代碼編寫設置中選擇選項下拉是做下面一行從C#

<option value="saab" selected>Saab</option> 

到目前爲止,我下面的代碼做了

StringWriter stringwriter = new StringWriter(); 
HtmlTextWriter writer = new HtmlTextWriter(stringwriter); 
DataTable dt1 = BAL.setDropDown(tablename, id_col, value_col, hotel_id); 
//let say selected_value is 1 
if (dt1.Rows.Count > 0) 
{ 
    foreach (DataRow row in dt1.Rows) 
    { 
     writer.RenderBeginTag(HtmlTextWriterTag.Option); 
     if(row[0].ToString() ==1) 
     { 
      // i want to add selected on option tag here!!! 
     } 
     writer.AddAttribute(HtmlTextWriterAttribute.Value, row[0].ToString()); 
     writer.Write(row[1].ToString()); 
     writer.RenderEndTag(); 
    } 
} 

回答

2

你可以這樣寫:

writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected"); 

它將呈現

<option value="saab" selected="selected">Saab</option> 

其是由瀏覽器可以接受的。