我正在調(diào)用一種方法來填充 asp.net 網(wǎng)絡(luò)表單中的下拉菜單。該方法調(diào)用檢索名稱列表的存儲(chǔ)過程。它還從數(shù)據(jù)庫中檢索成員 ID。所有這一切都很好,但是,我需要成員 ID 字段隨著下拉菜單中的每個(gè)關(guān)聯(lián)名稱而更改。例如,我從下拉列表中選擇名稱 Barney Fife,它返回成員 ID '1'。問題是當(dāng)我從下拉列表中選擇不同的名稱時(shí) - 會(huì)員 ID 沒有改變。這是我檢索數(shù)據(jù)的 C# 方法: public void GetMemberName() { try { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("dbo.spGetMemberNames", con)) { con.Open(); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { sda.Fill(ds); // ds is declared in the scope. cmbNames.DataSource = ds; cmbNames.DataTextField = "FormattedName"; cmbNames.DataValueField = "FormattedName"; cmbNames.DataBind(); } using (SqlDataReader read = cmd.ExecuteReader()) { while (read.Read()) { lblMemberID.Text = (read["MemberID"].ToString()); // Places the member ID associated with the name in a label. } } } } } catch (Exception ex) { lblError.Text = ex.ToString(); } }這是我的存儲(chǔ)過程:CREATE PROCEDURE [dbo].[spGetMemberNames]ASBEGIN SELECT FirstName + ' ' + LastName [FormattedName],MemberID FROM tblMembers ORDER BY LastNameEND我是否需要調(diào)用一個(gè)完全不同的方法和存儲(chǔ)過程來連接到數(shù)據(jù)庫并根據(jù) DropDown SelectedIndexChange 上的 FormattedName(來自我的存儲(chǔ)過程)查找成員 ID?
2 回答

HUH函數(shù)
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
這太容易了。這是解決方案:
protected void cmbNames_SelectedIndexChanged(object sender, EventArgs e)
{
string value = cmbNames.SelectedValue;
lblMemberID.Text = value;
}
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)
0/150
提交
取消