我是靠谱客的博主 美满蜡烛,最近开发中收集的这篇文章主要介绍设置和获取DropDownList的值的简单方法,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

设置和获取DropDownList的值的通用方法

 

1. 设置Dropdwonlist的方法

        /// <summary>
        /// Set DropDownList Value by ddlvalue
        /// 0,value;
        /// 1,text;
        /// -1,selectedIndex
        /// </summary>
        /// <param name="ddl">DropDownList</param>
        /// <param name="ddlvalue">the Value or Text of DropDownList</param>
        /// <param name="mode">0 as SelectedValue,1 as SelectedText,-1 as SelectIndex</param>
        protected void SetDdlValue(DropDownList ddl, string ddlvalue, int mode)
        {
            if (!String.IsNullOrEmpty(ddlvalue))
            {
                if (mode == 0)
                {
                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(ddlvalue));
                }
                else if (mode == 1)
                {
                    int i = ddl.Items.IndexOf(ddl.Items.FindByText(ddlvalue));
                    if (i == -1)
                    {
                        ddl.Items.Insert(0, ddlvalue);
                        ddl.SelectedIndex = 0;
                    }
                    else
                        ddl.SelectedIndex = i;
                }
                else if (mode == -1)
                {
                    ddl.SelectedIndex = int.Parse(ddlvalue);
                }
                else
                {
                    ddl.SelectedIndex = -1;
                }
            }

           else
           {
                 ddl.SelectedIndex = -1;
           }

        }

2.获取DropDownList的方法

        /// <summary>
        /// Get DropDownList Selected Item's value of Selected Item's text
        /// 0,value;
        /// 1 text;
        /// -1,selectedindex
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="mode">0 as SelectedValue,1 as SelectedText,-1 as SelectIndex</param>
        /// <returns></returns>
        protected string GetDdlValue(DropDownList ddl, int mode)
        {
            if (mode == 0)
                return ddl.SelectedValue;
            else if (mode == 1)
                return ddl.SelectedItem.Text.ToString();
            else
                return ddl.SelectedIndex.ToString();
        }

最后

以上就是美满蜡烛为你收集整理的设置和获取DropDownList的值的简单方法的全部内容,希望文章能够帮你解决设置和获取DropDownList的值的简单方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部