如何使用ASP.NET和C#从代码后台文件设置HTML Select
控件的选定值?
有FindByText
和FindByValue
函数可用:
ListItem li = Select1.Items.FindByText("Three");
ListItem li = Select1.Items.FindByValue("3");
li.Selected = true;
HTML:
<select id="selUserFilterOptions" runat="server">
<option value="1">apple</option>
<option value="2">orange</option>
<option value="3">strawberry</option>
</select>
C#:
string fruitId = selUserFilterOptions.Value.ToString();
.SelectedIndex
和 Items
属性。 - aspiring试试这个:
for (int i=0; i<=Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Value = valueToSelect)
{
Select1.Items[i].Selected = true;
// Try this too - http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlselect.selectedindex(v=VS.90).aspx
//Select1.SelectedIndex = i;
}
}
var selectedText = Select1.Items[Select1.SelectedIndex].Text.Trim();
Select1
是你的 HTML 选择控件的 ID。