概述
设置和获取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的值的简单方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复