当前位置:编程学习 > C#/ASP.NET >>

DropDownList值问题,在线等!

protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            string typeIdStr = Request.QueryString["typeId"];
            this.drpChannel.DataSource = ChannelManager.GetChannelName();
            this.drpChannel.DataTextField = "channelName";
            this.drpChannel.DataValueField = "channelId";
            this.drpChannel.DataBind();

            this.drpCountry.DataSource = CountryManager.GetCountryName();
            this.drpCountry.DataTextField = "countryName";
            this.drpCountry.DataValueField = "countryId";
            this.drpCountry.DataBind();
            int typeId = Convert.ToInt32(typeIdStr);
            string countryName = CountryManager.GetCountryByTypeId(typeId).CountryName;
            string channelName = ChannelManager.GetChannelNameByTypeId(typeId).ChannelName;
            //this.drpCountry.SelectedItem.Text = countryName;
            //this.drpChannel.SelectedItem.Text = channelName;
            for (int i = 0; i < drpChannel.Items.Count; i++)
            {
                if (drpChannel.Items[i].Text == channelName)
                {
                    drpChannel.Items[i].Selected = true;
                }
            }
            for (int i = 0; i < drpCountry.Items.Count; i++)
            {
                if (drpCountry.Items[i].Text == countryName)
                {
                    drpCountry.Items[i].Selected = true;
                }
            }
            this.txtCost.Text = Convert.ToString(CostManager.GetCostByTypeId(typeId).Cost);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int channelId = Convert.ToInt32(this.drpChannel.SelectedItem.Value);
        int countryId = Convert.ToInt32(this.drpCountry.SelectedItem.Value);

        string channelName = this.drpChannel.SelectedItem.Text;
        string countryName = this.drpCountry.SelectedItem.Text;


为什么无论怎么选择dropdownlist下的值,都只显示是第一个? --------------------编程问答-------------------- 给DropDownList赋值最好用SelectedValue
你可以获取到countryId
然后直接his.drpCountry.Selectedvalue=countryId; --------------------编程问答-------------------- 修改完了 绑定下 --------------------编程问答--------------------
引用 1 楼 airch 的回复:
给DropDownList赋值最好用SelectedValue
你可以获取到countryId
然后直接his.drpCountry.Selectedvalue=countryId;


楼上正解了 --------------------编程问答--------------------
引用 3 楼 weiyanli20080 的回复:
给DropDownList赋值最好用SelectedValue
你可以获取到countryId
然后直接his.drpCountry.Selectedvalue=countryId;
++ --------------------编程问答-------------------- Up++
引用 1 楼 airch 的回复:
给DropDownList赋值最好用SelectedValue
你可以获取到countryId
然后直接his.drpCountry.Selectedvalue=countryId;
--------------------编程问答-------------------- drpChannel.Items[i].Selected = true;........


设置Selectedvalue或者SelectedIndex --------------------编程问答--------------------  /// <summary>
        /// 选中指定列表框和选中值的列表项
        /// </summary>
        /// <param name="list"></param>
        /// <param name="selVal"></param>
        /// <param name="isText"></param>
        public void SelDropDownItem(DropDownList list, string selVal, bool isText)
        {
            if (string.IsNullOrEmpty(selVal))
            {
                return;
            }

            ListItem selItem = null;
            if (isText)
            {
                selItem = list.Items.FindByText(selVal);
            }
            else
            {
                selItem = list.Items.FindByValue(selVal);
            }

            if (selItem != null)
            {
                list.SelectedIndex = -1;
                selItem.Selected = true;
            }
        } --------------------编程问答-------------------- LZ真的在线等吗? --------------------编程问答-------------------- 一个是item.text,一个是values

注意ispostback
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,