从数据库ASP DropDownList的数据绑定 [英] ASP dropdownlist databind from database

查看:100
本文介绍了从数据库ASP DropDownList的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用2的DropDownList。第一次为国第二的国家。如果我从1英尺DropDownList中选择印度:第二个是自动绑定印度的所有状态从数据库中自动。

我用 country_tbl 的国家,并与1号的DropDownList和 india_tbl us_tbl绑定 sri_tbl 绑定有关这些国家的状态。

请帮助我。我应该怎么办?

我的code是如下:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!Page.IsPostBack)
    {
        方法一();
    }
}保护无效方法1()
{
    字符串S1 =数据源= ALOK-PC \\\\ SQLEX $ P $干燥综合征;数据库= mysite的;集成安全=真;
    SqlConnection的CON =新的SqlConnection(S1);
    字符串s2 =选择国家*;
    CMD的SqlCommand =新的SqlCommand(S2,CON);
    con.Open();
    SqlDataReader的博士= cmd.ExecuteReader();
    DropDownList1.DataTextField =名;
    DropDownList1.DataValueField =名;
    DropDownList1.DataSource =博士;
    DropDownList1.DataBind();
    con.Close();
    dr.Close();
}保护无效methodInd()
{
    字符串S1 =数据源= ALOK-PC \\\\ SQLEX $ P $干燥综合征;数据库= mysite的;集成安全=真;
    SqlConnection的CON =新的SqlConnection(S1);
    字符串s2 =选择印度*;
    CMD的SqlCommand =新的SqlCommand(S2,CON);
    con.Open();
    SqlDataReader的博士= cmd.ExecuteReader();
    DropDownList2.DataTextField =名;
    DropDownList2.DataValueField =名;
    DropDownList2.DataSource =博士;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}保护无效methodpak()
{
    字符串S1 =数据源= ALOK-PC \\\\ SQLEX $ P $干燥综合征;数据库= mysite的;集成安全=真;
    SqlConnection的CON =新的SqlConnection(S1);
    字符串s2 =选择从巴基斯坦*;
    CMD的SqlCommand =新的SqlCommand(S2,CON);
    con.Open();
    SqlDataReader的博士= cmd.ExecuteReader();
    DropDownList2.DataTextField =名;
    DropDownList2.DataValueField =名;
    DropDownList2.DataSource =博士;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}保护无效methodsri()
{
    字符串S1 =数据源= ALOK-PC \\\\ SQLEX $ P $干燥综合征;数据库= mysite的;集成安全=真;
    SqlConnection的CON =新的SqlConnection(S1);
    字符串s2 =选择斯里兰卡*;
    CMD的SqlCommand =新的SqlCommand(S2,CON);
    con.Open();
    SqlDataReader的博士= cmd.ExecuteReader();
    DropDownList2.DataTextField =名;
    DropDownList2.DataValueField =名;
    DropDownList2.DataSource =博士;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}保护无效submit_Click(对象发件人,EventArgs的发送)
{}保护无效DropDownList1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    如果(DropDownList1.SelectedItem.Text ==印)
    {
        methodInd();
    }
    否则,如果(DropDownList1.SelectedItem.Text ==巴)
    {
        methodpak();
    }
    否则,如果(DropDownList1.SelectedItem.Text ==斯里兰卡)
    {
        methodsri();
    }
}


解决方案

您的做法是错误的,你不应该对每个国家的国家单独的表,所以你可以有一个查询和一个方法绑定到你的国家名单

您的模式改为

  CREATE TABLE国家

  ID INT,
  国家的名字,
  主键(ID)
)Country_State

  ID INT,
  STATE_NAME,
  COUNTRY_ID,
  主键(ID)

该COUNTRY_ID是连接返回到该国的外键

 国家
------------------------
ID名称
------------------------
印度1
2巴基斯坦
country_state
-----------------------------------
ID名称COUNTRY_ID
------------------------------------
1新德里1
2孟加拉国1
3一些印度人1
4 S1_Pakistan 2
5 S2_Pakistan 2

您查询

 选择编号,由国名选择ID,名称从country_states其中COUNTRY_ID = @id


  

呼叫只是一个绑定所有的州法


 保护无效DropDownList1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
     字符串COUNTRY_ID = DropDownList1.SelectedValue;
     BindStatesByCountry(COUNTRY_ID);
}保护无效methodsri(字符串countryId)//改变这BindStatesByCountry
{
    字符串S1 =数据源= ALOK-PC \\\\ SQLEX $ P $干燥综合征;数据库= mysite的;集成安全=真;
    SqlConnection的CON =新的SqlConnection(S1);
    con.Open();    字符串s2 =从country_states那里COUNTRY_ID = @ countryId选择*;
    CMD的SqlCommand =新的SqlCommand(S2,CON);
    cmd.Parameters.AddWithValue(@ countryId,countryId);
    SqlDataReader的博士= cmd.ExecuteReader();
    DropDownList2.DataTextField =名;
    DropDownList2.DataValueField =名;
    DropDownList2.DataSource =博士;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

希望这有助于

I am using 2 dropdownlist. 1st for country and 2nd for states. If I select India from 1ft dropdownlist, the 2nd one is automatically bind all the states of India automatically from database.

I have used country_tbl for countries and bind with 1st dropdownlist and india_tbl, us_tbl, sri_tbl for binding states regarding these countries.

Please help me. What should I do?

My code is as follows:

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

protected void method1()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from country";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList1.DataTextField = "name";
    DropDownList1.DataValueField = "name";
    DropDownList1.DataSource = dr;
    DropDownList1.DataBind();
    con.Close();
    dr.Close();
}

protected void methodInd()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from india";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void methodpak()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from pakistan";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void methodsri()
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    string s2 = "select * from srilanka";
    SqlCommand cmd = new SqlCommand(s2, con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

protected void submit_Click(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedItem.Text=="india")
    {
        methodInd();
    }
    else if (DropDownList1.SelectedItem.Text=="pakistan")
    {
        methodpak();
    }
    else if (DropDownList1.SelectedItem.Text=="srilanka")
    {
        methodsri();
    }
}

解决方案

Your approach is wrong, you should not have separate tables for states of each country so you can have one query and one method to bind to your state list

Change your schema to

CREATE TABLE Country 
(
  id int,  
  country_name,
  primary key(id)
)

Country_State 
(
  id  int, 
  state_name, 
  country_id,
  primary key(id)
)

The country_id is the Foreign key that links back to the country

country
------------------------
id  Name
------------------------
1   India
2   Pakistan


country_state
-----------------------------------
id  Name              country_id
------------------------------------
1   Delhi             1
2   Bangladesh        1
3   Some Indians      1
4   S1_Pakistan       2
5   S2_Pakistan       2

Your queries

Select Id, Name from Country

Select Id, Name From country_states Where country_id = @id

Call just one method to bind all states

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
     string country_id =  DropDownList1.SelectedValue;
     BindStatesByCountry(country_id);
}

protected void methodsri(string countryId) //change this to BindStatesByCountry
{
    string s1 = "data source=ALOK-PC\\SQLEXPRESS;database=MySite;integrated security=true";
    SqlConnection con = new SqlConnection(s1);
    con.Open();

    string s2 = "select * from country_states where country_id=@countryId";
    SqlCommand cmd = new SqlCommand(s2, con);
    cmd.Parameters.AddWithValue("@countryId", countryId);
    SqlDataReader dr = cmd.ExecuteReader();
    DropDownList2.DataTextField = "name";
    DropDownList2.DataValueField = "name";
    DropDownList2.DataSource = dr;
    DropDownList2.DataBind();
    con.Close();
    dr.Close();
}

Hope this helps

这篇关于从数据库ASP DropDownList的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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