{
DropDownList1_SelectedIndexChanged(sender, e);
}
ListItem li = DDLInvoiceType.Items.FindByValue("Value") as ListItem;
if (li != null)
{
li.Selected = true;
}else li.SelectedIndex=-1;
法二:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value值"));
就是如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1.
------------------------------------------------------------------------------------------------------
DDL的设置初始选定项:
DDL.Items.FindByValue("初始选定项").Selected=true;
或者 DDL.Items.FindByText("初始选定项").Selected=true;
DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByValue("初始选定项"));
或者 DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByText("初始选定项"));
DDL的各种绑定方式:
一、在页面初始化时候将集合绑定到DropDownList
public void Page_Load(Object src.EventArgs e)
{
ArrayList arrValue = new ArrayList();
arrValue.add("kk");
arrValue.add("dd");
arrValue.add("aa");
arrValue.add("cc");
//将数组绑定到DropDownList控件的DataSource属性
ddl.DataSource = arrValue;
ddl.DataBind();
}
//实现
选项有:<asp:DropDownList )
{
DropDownList1_SelectedIndexChanged(sender, e);
}
ListItem li = DDLInvoiceType.Items.FindByValue("Value") as ListItem;
if (li != null)
{
li.Selected = true;
}else li.SelectedIndex=-1;
法二:
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value值"));
就是如果通过FindByValue没有找到指定项则为null,而Items.IndexOf(null)会返回-1.
------------------------------------------------------------------------------------------------------
DDL的设置初始选定项:
DDL.Items.FindByValue("初始选定项").Selected=true;
或者 DDL.Items.FindByText("初始选定项").Selected=true;
DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByValue("初始选定项"));
或者 DDL.SelectedIndex=DDL.Items.IndexOf(DDL.Items.FindByText("初始选定项"));
DDL的各种绑定方式:
一、在页面初始化时候将集合绑定到DropDownList
public void Page_Load(Object src.EventArgs e)
{
ArrayList arrValue = new ArrayList();
arrValue.add("kk");
arrValue.add("dd");
arrValue.add("aa");
arrValue.add("cc");
//将数组绑定到DropDownList控件的DataSource属性
ddl.DataSource = arrValue;
ddl.DataBind();
}
//实现
选项有:<asp:DropDownList )