-----------------------------------------------DDL的载入就激发的方法-------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1_SelectedIndexChanged(sender, e);
    }
 -----------------如果绑定的数据里没有此选项,那么Bind()就会导致一个错误,如何避免呢? ----------------------
                         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 )


                   
-----------------------------------------------DDL的载入就激发的方法-------------------------------------
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1_SelectedIndexChanged(sender, e);
    }
 -----------------如果绑定的数据里没有此选项,那么Bind()就会导致一个错误,如何避免呢? ----------------------
                         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 )


                   

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2022-01-09
  • 2021-05-03
  • 2021-07-02
  • 2021-05-10
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-23
  • 2021-12-01
相关资源
相似解决方案