我努力實現以下目標:數據綁定FormView控件與DropDownList的問題
消除
FormView’s
默認編輯鏈接按鈕。使用當前數據和其他DDL數據選擇在
Page_Load
上以編輯模式顯示FormView
。
目前我有以下的DDL綁定到基於當前數據庫值id = x
等:
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("xxx") %>'
DataSourceID="xxx"
DataTextField="xxx"
DataValueField="xxx"
ondatabound="DropDownList1_DataBound">
通過C#代碼,我這樣做是爲了增加額外的DDL列表項選擇:(其是浪費我的能量,因爲數據已經在數據庫中!)
protected void DropDownList1 _DataBound(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1");
DropDownList1.Items.Insert(1, new ListItem("0 - 1 km/h", "0"));
DropDownList1.Items.Insert(2, new ListItem("2 - 5 km/h", "1"));
DropDownList1.Items.Insert(3, new ListItem("6 - 11 km/h", "2"));
DropDownList1.Items.Insert(4, new ListItem("12 - 19 km/h", "3"));
DropDownList1.Items.Insert(5, new ListItem("20 - 28 km/h", "4"));
}
有一定要有更好的辦法!
請問'DropDownList1'沒有得到來自'DataSource'已鏈接的結果嗎?爲什麼你必須手動填充它? – mellamokb
所以,很明顯,你想從數據庫中動態地填充'DropDownList'?如果是這樣的話,爲什麼要手動添加它們,以及代碼從數據庫中拉出來的位置?在Stackoverflow **更多**信息總是更好:) – Brian
嗨布萊恩 - DDL確實包含從數據源查詢單個數據項。我手動添加的項目是一個額外的數據集,我想添加到DDL中。因此,用戶可以看到他們的原始記錄項目,並且可能通過額外的DDL項目(我手動添加的項目)選擇一個新項目(用於記錄更新)。手動添加的數據存在,但我不知道如何將其綁定到DDL。 – BJC