问题描述
你好,我只是一个初学者,需要你的帮助...
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();
}
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(); }
这篇关于如何使用c#基于dropdownlist1的值填充dropdownlist2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!