2013-12-23 114 views
2

我想附加文字標籤上的CheckChanged事件,我試過下面的代碼,但它似乎不工作。你能建議我改變什麼嗎? 這裏是我的代碼將字符串附加到標籤

protected void CheckBox2_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox2.Visible = false; 
    DropDownList2.Visible = true; 
    CheckBox3.Visible = true; 
    Label2.Text += "-" + DropDownList2.SelectedValue; 
} 

protected void CheckBox3_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox3.Visible = false; 
    DropDownList3.Visible = true; 
    CheckBox4.Visible = true; 
    Label2.Text += "-" + DropDownList3.SelectedValue; 
} 
+1

'「它似乎不起作用」 - 它是做什麼的? –

+0

您的下拉列表是如何填充的?嘗試'DropDownList3.SelectedValue.ToString()'或'DropDownList3.Text' –

+0

你有沒有運氣解決這個問題? – tjheslin1

回答

1

而不是selectedvalue拉昇Text財產。

Label2.Text += "-" + DropDownList2.Text; 

這將是如下:

protected void CheckBox2_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox2.Visible = false; 
    DropDownList2.Visible = true; 
    CheckBox3.Visible = true; 
    Label2.Text += "-" + DropDownList2.Text; 
} 

protected void CheckBox3_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox3.Visible = false; 
    DropDownList3.Visible = true; 
    CheckBox4.Visible = true; 
    Label2.Text += "-" + DropDownList3.Text; 
} 
+0

我也嘗試過也... 但它也行不通。 –

+0

你有調試代碼??? ...做DropDownList3.SelectedValue給你適當的價值? –

+0

我認爲這個代碼不是完美的bu .value你也會得到價值和它的考慮作爲一個string.so不需要做dropdownlist.text –

0

你試過DropDownList2.SelectedValue.ToString()

或者你可以castSelectedItem追加一個特定的屬性。

0

使用SelectedItem.Text

Label2.Text += "-" + DropDownList3.SelectedItem.Text; 
+0

當我這樣做時它顯示以下錯誤: 對象引用未設置爲對象的實例。 –

+0

當你來到這條線是在那個點選擇的任何項目。 – Rex

0

你最好爲目的使用單StringBuilder:它操作字符串數據,並分配其當前的緩衝區,以標籤的Text財產

+1

雖然沒有明確說明,但問題似乎是'SelectedValue'正在返回的值。所以字符串如何被追加並分配不是問題。至少尚未解決第一個問題。不過'StringBuilder'是一個好主意,使用它的'Append'方法比'+ ='更有效率 – tjheslin1

0

我想你使用:

Label2.Text =Labe2.Text + "-" + DropDownList2.SelectedValue;