如何使用c#基于dropdownlist1的值填充dropdownlist2? [英] how to populate dropdownlist2 based on value from dropdownlist1 using c#?

查看:77
本文介绍了如何使用c#基于dropdownlist1的值填充dropdownlist2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我只是一个初学者,需要你的帮助...

DROPDOWNLIST1:我有一个表cat,其中包含column-id,category和categoryid ...我成功填充了它的值来自数据库。

DROPDOWNLIST2:还有另一个名为products的表,其中columns-id,name,categoryid.i需要根据我从DROPDOWNLIST1检索的categoryid填充我的DROPDOWNLIST2

在我的代码中我在下面的代码行中收到错误

  int  id =  int  .Parse(DropDownList1.SelectedValue.ToString()); 





这里是我的代码

 受保护  void  Page_Load(  object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
bindcatdropdown();

}
}
受保护 void bindcatdropdown( )
{

DataTable table = new DataTable();
使用(SqlConnection con = new SqlConnection(constr))
{
string sql = 从cat中选择category,categoryid按类别命令;
使用(SqlCommand cmd = new SqlCommand(sql,con))
{
使用(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(table);
}
}

}
DropDownList1.DataSource = table;
DropDownList1.DataBind();
DropDownList1.Items.Insert( 0 new ListItem( - 选择 - 0));
}



 受保护  void  DropDownList1_SelectedIndexChanged( object  sender,EventArgs e)
{
DataTable table = new DataTable();
// int id = Convert.ToInt32(DropDownList1.SelectedValue);
int id = int .Parse(DropDownList1.SelectedValue.ToString());
使用(SqlConnection con = new SqlConnection(constr))
{
string sql = 从产品中选择名称,categoryid在哪里categoryid = @ cid ORDER BY categoryid;
使用(SqlCommand cmd = new SqlCommand(sql,con))
{
使用(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue( @ cid,id);
da.Fill(table);
}
}

}
DropDownList2.DataSource = table;
DropDownList2.DataBind();
}

解决方案

以下列方式尝试:



  string  cat_id = DropDownList1.SelectedValue.Tostring(); 
int id;
if (!( int .TryParse(cat_id.ToString(), out id)))
return ;





而不是

 int id = Convert.ToInt32(DropDownList1.SelectedValue); 





希望它适合你。 :)


hello, im just a beginner and need your help...
DROPDOWNLIST1: i have a table say "cat" with columns-id,category and categoryid... i successfully populated its value from the database.
DROPDOWNLIST2: Also have another table called "products" with columns-id,name,categoryid.i need to populate my DROPDOWNLIST2 based on categoryid i retrieve from DROPDOWNLIST1
In my code im getting an error at the below line of code

int id = int.Parse(DropDownList1.SelectedValue.ToString());



here is my code

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

        DataTable table = new DataTable();
        using (SqlConnection con = new SqlConnection(constr))
        {
            string sql = "select category,categoryid from cat ORDER BY categoryid";
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(table);
                }
            }

        }
        DropDownList1.DataSource = table;
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
       }


protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       DataTable table = new DataTable();
       //int id = Convert.ToInt32(DropDownList1.SelectedValue);
       int id = int.Parse(DropDownList1.SelectedValue.ToString());
       using (SqlConnection con = new SqlConnection(constr))
       {
           string sql = "select name,categoryid from products WHERE categoryid=@cid ORDER BY categoryid";
           using (SqlCommand cmd = new SqlCommand(sql, con))
           {
               using (SqlDataAdapter da = new SqlDataAdapter(cmd))
               {
                   cmd.Parameters.AddWithValue("@cid", id);
                   da.Fill(table);
               }
           }

       }
       DropDownList2.DataSource = table;
       DropDownList2.DataBind();
   }

解决方案

try it in the following manner:

string cat_id=DropDownList1.SelectedValue.Tostring();
  int id;
  if(!(int.TryParse(cat_id.ToString(),out id)))
  return;



instead of

int id = Convert.ToInt32(DropDownList1.SelectedValue);



Hope it works for you. :)


这篇关于如何使用c#基于dropdownlist1的值填充dropdownlist2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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