2015-03-31 111 views
0

我正在嘗試做一個汽車維修項目,而且我有一個小問題。該項目是這樣的:dropdownlist從SQL數據庫中選擇

4種不同的汽車維修計劃。

CarA (MirrorA, etc etc) 
CarB (MirrorB, etc etc) 
CarC (MirrorC, etc etc) 
CarD (MirrorD, etc etc) 

我想要做的是,當我選擇了一個Car(從DropDownList),程序選擇正確的維護計劃的車!

SqlCommand cmd = new SqlCommand("Select id, description from accauto_maps", con); 
con.Open(); 
DropDownList1.DataSource = cmd.ExecuteReader(); 
DropDownList1.DataTextField = "description"; 
DropDownList1.DataValueField = "id"; 
DropDownList1.DataBind(); 

現在我卡住了。

+0

你有自動回設置爲true?這是你所有的代碼還是更多? – Maximus2012 2015-03-31 17:51:14

+0

在相同的代碼塊中查看DropDownList和SqlCommand會使我的眼睛受傷。 – granadaCoder 2015-03-31 18:38:26

+0

你能詳細說一下,你的小問題是什麼?你到底在哪裏? – wonderbell 2015-03-31 19:01:05

回答

0

現在你需要設置的SelectedValue在此列表中 和/或 定義事件OnSelectedIndexChanged在那裏你會處理用戶的選擇

下面的例子中我們是怎麼做的:

<asp:DropDownList ID="ddlStatus" AutoPostBack="True" runat="server" DataSourceID="EDSLookStatus" 
    DataValueField="cd" 
    DataTextField="lookupName" 
    OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" /> 
<asp:TextBox ID="txtBxStatus" runat="server" Text='<%# Bind("status") %>' Visible="False" /> 

這是使用方法:添加了與「狀態」鏈接的隱形文本框。當DDL變化值其設置新值這個txtBx:

protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList ddl = FormViewClient.FindControl("ddlStatus") as DropDownList; 
    TextBox txtBx = FormViewClient.FindControl("txtBxStatus") as TextBox; 
    if (ddl != null && txtBx != null) 
    { txtBx.Text = ddl.SelectedValue; } 
} 

設置選擇的值:

<asp:FormView ... OnDataBound="FormViewClient_DataBound" > 

protected void FormViewClient_DataBound(object sender, EventArgs e) 
{ 
    ... 
    DropDownList ddl = FormViewClient.FindControl("ddlStatus") as DropDownList; 
    TextBox txtBx = FormViewClient.FindControl("txtBxStatus") as TextBox; 
    if (ddl != null && txtBx != null) 
    { ddl.SelectedValue = txtBx.Text; } 
    ... 
} 
+0

我知道我必須這樣做,我只是不知道該怎麼做 - 你可以更好地解釋嗎? tks的答案我知道即時通訊不好,因爲你們,但它的答案-.- ty – davidz 2015-04-01 08:50:14

+0

我做了一個OnSelectedIndexChanged,但我onlu可以使用1 tabel ...但我有2 tabels我需要tabel與(id「主鍵「,描述)與表格B(行」主鍵「,地圖,提示,僱主)的關係,現在我需要做的是,當我選擇表格」描述「時,它顯示我地圖,提示,僱主。關係在(表格A id和tabel B Map) – davidz 2015-04-01 10:00:30

+0

tks工作得很好xD – davidz 2015-04-02 10:19:17