如何在运行时绑定DropDown? [英] How can Bind DropDown at Run Time ?

查看:78
本文介绍了如何在运行时绑定DropDown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我的问题是我正在创建一页,并且运行时绑定了一个DropDown. DropDown值正确地来自数据库,它向我显示正确的格式,但是当我单击提交"按钮时,它变为空,并且像波纹管代码

Dear All,

My Problem is I was Create One page and runtime bind a DropDown. DropDown values Comes from database Properly, and it shows me Proper Format but when i Click Submit Button at That Time It Become Null and like Bellow Code

protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
  {
    LoadCompany();
  }
}

public void LoadCompany()
{
 try
 {
  DataSet ds = Dbcompany.GetAllCompany(ConfigurationManager.AppSettings["ConnectionString"]);
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            ListItem li = new ListItem();
                            li.Value = ds.Tables[0].Rows[i]["CompanyId"].ToString();
                            li.Text = ds.Tables[0].Rows[i]["CompanyName"].ToString();
                            ddl_company.Items.Add(li);
                            
                        }
                    }
                }
            }
 }
}

protected void btn_add_Click(object sender, EventArgs e)
{
 //When buttin_Click at that time ddl_company DropDown Become Null so How Can Solwed this.. 
}



如果我从Page_Load中删除Page.IsPostBack条件,那么所有时间下拉填充了,所以当公司选择所有时间1 ..


请帮我,这是我的严重问题...



If I Remove Page.IsPostBack Condition From Page_Load then all Time DropDown was Fill So When Selected Company all Time 1st..


Pleas Help Me, it''s My serious Problem...

推荐答案

尝试一下:

Try This :

       if (!IsPostBack)
       {
           FillDropDownList();
       }


     private void FillDropDownList()
    {
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select fileds  FROM tablename", connection object);
        myda.Fill(ds);
        drop_category.DataSource = ds;
        drop_category.DataValueField = "fileds";
        drop_category.DataBind();
        drop_category.Items.Insert(0, new ListItem("Select", "0"));
    }

Hope this will help you. if not please post it.


DropDownList的EnableViewState属性是否设置为true?
Is the EnableViewState property of the DropDownList set to true?


在调用loadcompany set时,然后选择下拉列表index = 0

when you call loadcompany set then dropdownlist selected index=0

if (!Page.IsPostBack)
  {
    LoadCompany();
ddl_company.SelectedIndex = 0;
  }


我认为此事将得到解决.


i think after this it will be solved.


这篇关于如何在运行时绑定DropDown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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