我怎样才能得到一个DropDownList的选择项,第一次我的页面加载? [英] How can I get the selected item of a dropdownlist, the first time my page loads?

查看:227
本文介绍了我怎样才能得到一个DropDownList的选择项,第一次我的页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个解决方案,以获得在DropDownList中第一个选择项。我想要得到它在页面加载时的第一次。

感谢您提前。

编辑:我请在负载情况下,本方法,但ddlNiveau2保持为空。我认为ddlNiveau1.SelectedValue不访问。

 公共无效FillListNiveau2()
{
    ddlNiveau2.Items.Clear();
    的foreach(在dBAL.GetListNiveau2 VAR项目(ddlNiveau1.SelectedValue))
    {
        ddlNiveau2.Items.Add(item.ToString());
    }
    RemoveDuplicateItems(ddlNiveau2);
}


解决方案

有一个 DataBound事件,将数据绑定到下拉后触发。当你正在分配数据源的下拉菜单中你需要选定项目后,所有的行绑定到下拉列表

 保护无效DropDownList1_DataBound(对象发件人,EventArgs的发送)
{
    DropDownList1.SelectedValue //它存储在某个变量
}

I'm looking for a solution to get the first selected item in a DropDownList. And I want to get it when the page loads for the first time.

Thank you in advance.

Edit: I call this method at the Load-event but ddlNiveau2 remains empty. I think that ddlNiveau1.SelectedValue isn't accessed.

public void FillListNiveau2()
{
    ddlNiveau2.Items.Clear();
    foreach (var item in dBAL.GetListNiveau2(ddlNiveau1.SelectedValue))
    {
        ddlNiveau2.Items.Add(item.ToString());
    }
    RemoveDuplicateItems(ddlNiveau2);
}

解决方案

There is a DataBound event, which fires after the data is bound to the dropdown. As you are assigning the dataSource to your dropdown you need selected item after all the rows binded to dropdown

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    DropDownList1.SelectedValue // store it in some variable
}

这篇关于我怎样才能得到一个DropDownList的选择项,第一次我的页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆