如何从HTML SELECT中获取选择的项目名称? [英] How to get select item name from html SELECT?
问题描述
< SELECT id =MySelectionname =MySelectionrunat =serverDataValueField =IDDataTextField =Name>< / SELECT>
数据是通过设置其属性DataSource来提供的:
MySelection.DataSource = IEnumerable集合数据
假设数据存储在数据库中的表中:
- 选项A
- 选项B
- 选项C
- 选项D
从 MySelection.Value
,我可以得到ID值,例如1,2,3。
从 MySelection。名称
,我会得到我的选择,而不是像选项A这样的选定项目名称
如何获取选定的项目Name? / p>
有没有理由不使用ASP.NET DropDownList?
为了从select元素中获取文本,您应该可以使用:
MySelection.Items [ MySelection.SelectedIndex]。文本;
Use Html element SELECT in aspx page like:
<SELECT id="MySelection" name="MySelection" runat="server" DataValueField="ID" DataTextField="Name"></SELECT>
Data is feed by setting its property DataSource:
MySelection.DataSource = IEnumerable collection Data
Suppose data is stored in table in DB like:
- Option A
- Option B
- Option C
- Option D
From MySelection.Value
, I can get the ID value, like 1,2,3.
From MySelection.Name
, I will get "MySelection", not the selected item name like "Option A"
How to get the selected item Name?
Is there a reason that you aren't using the ASP.NET DropDownList?
In order to get the text from a select element you should be able to use:
MySelection.Items[MySelection.SelectedIndex].Text;
这篇关于如何从HTML SELECT中获取选择的项目名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!