iT邦幫忙

0

asp.NET DropDownList 連動

  • 分享至 

  • xImage

想問一下 asp.NET DropDownList 連動問題

下面為資料庫 TABLE 欄位

WORD_ID WORK_NAME WORK_S
A 維護儀電技術員 1000
B 維護電機技術員 2000

目前我利用DropDownList下拉式選單 將WORK_NAME的資料顯示,讓使用者去挑選,挑選後希望另外一個欄位自動帶出WORK_S的值(目前TABLE 都是屬於一對一)
想請問我是哪邊寫錯,因為我選完後,另外一個DropDownList會預設第一筆WORK_S的值且下拉後會顯示全部的WORK_S的值,但是我希望是選哪一個就是對應出那個金額

protected void Page_Load(object sender, EventArgs e)
{
try {

    if (!IsPostBack)
    {
        bindWORKName();
        setDDLDefault();

        }
    }
    catch { }
    //這裡不用修改
    //欄位的初始化資料都到SetField Method去做
    SetField(m_versionField);
}

private void setDDLDefault()
{
DropDownList2.Items.Insert(0, new ListItem("請選擇", "-1"));
DropDownList3.Items.Insert(0, new ListItem("請選擇", "-1"));
}
private void bindWORKS()
{
string DBConnectionString = @"Data Source=(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = xxxx)(PORT = xxxx)) ) (CONNECT_DATA =(SERVICE_NAME = xxxx)));User ID=xxxx;Password=xxxx";
OracleConnection conn2 = new OracleConnection(DBConnectionString);
string cmdTxt1 = @"SELECT A.WORK_S
FROM WRK_AREA A " ;
OracleCommand cmd1 = new OracleCommand(cmdTxt1, conn2);
DataSet dt1 = new DataSet();
OracleDataAdapter oaa1 = new OracleDataAdapter(cmd1);
oaa1.Fill(dt1, "ds2");
DropDownList3.DataValueField = "WORK_S";
DropDownList3.DataTextField = "WORK_S";
DropDownList3.DataSource = dt1;
DropDownList3.DataBind();
}
private void bindWORKName()
{
string DBConnectionString = @"Data Source=(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = xxxx)(PORT = xxxx)) ) (CONNECT_DATA =(SERVICE_NAME = xxxx)));User ID=xxxx;Password=xxxx";
OracleConnection conn1 = new OracleConnection(DBConnectionString);
conn1.Open();
string cmdTxt1 = @"SELECT A.WORK_NAME
FROM WRK_AREA A";
OracleCommand cmd1 = new OracleCommand(cmdTxt1, conn1);
DataSet dt1 = new DataSet();
OracleDataAdapter oaa1 = new OracleDataAdapter(cmd1);
oaa1.Fill(dt1, "ds2");
DropDownList2.DataValueField = "WORK_NAME";
DropDownList2.DataTextField = "WORK_NAME";
DropDownList2.DataSource = dt1;
DropDownList2.DataBind();

}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList2.SelectedValue != "-1")
    {
        bindWORKS();
    }
    else
    {
        DropDownList3.Items.Clear();
        DropDownList3.Items.Insert(0, new ListItem("請選擇", "-1"));
    }
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
小魚
iT邦大師 1 級 ‧ 2021-03-26 08:09:46

你是寫在 if (!IsPostBack) 裡面嗎?
那裡面的只有切換網頁的時候會觸發.

0
jack8900
iT邦新手 2 級 ‧ 2021-03-26 08:55:19

先不論對錯...你的指令沒有下where條件,當然都全撈出來啊

private void bindWORKS()
{
    string DBConnectionString = @"Data Source=(DESCRIPTION = (ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = xxxx)(PORT = xxxx)) ) (CONNECT_DATA =(SERVICE_NAME = xxxx)));User ID=xxxx;Password=xxxx";
    OracleConnection conn2 = new OracleConnection(DBConnectionString);
    //where條件???
    string cmdTxt1 = @"SELECT A.WORK_S FROM WRK_AREA A ";
    OracleCommand cmd1 = new OracleCommand(cmdTxt1, conn2);
    DataSet dt1 = new DataSet();
    OracleDataAdapter oaa1 = new OracleDataAdapter(cmd1);
    oaa1.Fill(dt1, "ds2");
    DropDownList3.DataValueField = "WORK_S";
    DropDownList3.DataTextField = "WORK_S";
    DropDownList3.DataSource = dt1;
    DropDownList3.DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList2.SelectedValue != "-1")
    {
        bindWORKS();
    }
    else
    {
        DropDownList3.Items.Clear();
        DropDownList3.Items.Insert(0, new ListItem("請選擇", "-1"));
    }
}
0
海綿寶寶
iT邦大神 1 級 ‧ 2021-03-26 08:58:01

參考這篇

https://ithelp.ithome.com.tw/upload/images/20210326/20001787IcCxrddcHg.png

程式碼 blah blah blah

我要發表回答

立即登入回答