码迷,mamicode.com
首页 > 其他好文 > 详细

DropDownList 设定选中项值

时间:2020-02-24 09:42:14      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:htm   tail   etl   ddl   detail   repo   abi   col   null   

        private void BindGoodName(DropDownList ddlGoodsName)
        {
            string sqlwhere = string.Empty;
            if (!string.IsNullOrEmpty(Request["id"])&& Convert.ToInt32(Request["id"]) > 0)
            {
                sqlwhere = " and Id <> " + Convert.ToInt32(Request["id"]);
            }

            ddlGoodsName.DataSource = goods.GetList("  g.GoodsID not in (select GoodsId from  CRM_IntegralRule where TypeId=3 "+ sqlwhere + " and RuleType=1)");
            ddlGoodsName.DataTextField = "GoodsName";
            ddlGoodsName.DataValueField = "GoodsId";
            ddlGoodsName.DataBind();
            ddlGoodsName.Items.Insert(0, new ListItem(Resources.Admin.Item_PleaseSelect, ""));
        }

设定下拉项被选中

ListItem x = ddlGoodsName.Items.FindByValue(model.GoodsId.Value.ToString());
if (x != null) x.Selected = true;

 

一、在绑定数据源后,设置选中的值

(1)   dropdowslist.selectedIndex=索引值(数字);
 (2) dropdownlist.Items.findbyvalue(你的值).selected=true  
 (3) dropdownlist.Items.findbytext(你的值).selected=true

 
asp.net页面中,如下方法给DropDownList赋值:

 dpType.SelectedIndex = dpType.Items.IndexOf(dpType.Items.FindByText(m_TradeReport.R_CateName));
或

dpType.SelectedIndex = dpType.Items.IndexOf(dpType.Items.FindByValue(m_TradeReport.R_CateID));
这两个可以避免以下方法有时候不能设置值的情况:

 dpType.SelectedValue = Convert.ToString(m_TradeReport.R_CateID);

 

参考:https://blog.csdn.net/wangganggang90/article/details/8468027

https://www.cnblogs.com/downmoon/archive/2011/03/28/1998077.html

DropDownList 设定选中项值

标签:htm   tail   etl   ddl   detail   repo   abi   col   null   

原文地址:https://www.cnblogs.com/shy1766IT/p/12355490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!