我试图从c#
获得以下html代码<select>
<option value="volvo">Volvo</option>
<option value="saab" selected>Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
我的主要目标是从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();
}
}
答案 0 :(得分:2)
你可以这样写:
writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
它将呈现
<option value="saab" selected="selected">Saab</option>
这是浏览器可以接受的。