DropDownList根据选定值获取相应ID
我在.net中使用DropDownList,想使用它的SelectedIndexChanged 事件,根据下拉框选择项从数据库获取值,所以我将AutoPostBack设置为true了,这样做每次只能获取下拉框的第一个值,我想问一下怎样才能根据选择的值取得相应项的ID值呢
[解决办法]
javascript:alert(document.getElementById("dropdownlist1").value);
[解决办法]
首次加载时候绑定下拉,然后再取值的时候就是娶到你所选的了
page_load
{
if(!ispostback)
{
、、、此处绑定下拉
}
}
[解决办法]
绑定数据放到如下代码里
if(!ispostback)
{
//绑—ropDownList代码
}
页面
<td align="left" style="width: 340px">
<asp:DropDownList ID="ddlSellerType" runat="server">
</asp:DropDownList>
</td>
后台
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<SellerType> sellertype = isellerService.GetSellersType();
ddlSellerType.DataSource = sellertype;
ddlSellerType.DataTextField = "Name";
ddlSellerType.DataValueField = "SellerTypeID";
ddlSellerType.DataBind();
}
}