2012-04-05 67 views
4

我在我的sql數據庫中有一個名爲「WEB_SEL_SECURITY_QUESTIONS」的存儲過程,它按如下方式返回一個表。如何將數據綁定到HTML選擇框

SEQURITY_QUESTIONS_KEY,KEYCODE,問題

我需要填充一個HTML選擇框用的問題,我從執行上述過程獲得的名單。

這是我曾嘗試

String s = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString(); 
SqlConnection con = new SqlConnection(s); 
con.Open(); 
SqlDataAdapter myAdepter = new SqlDataAdapter("WEB_SEL_SECURITY_QUESTIONS", con); 
myAdepter.SelectCommand.CommandType = CommandType.StoredProcedure; 
DataSet ds = new DataSet(); 
myAdepter.Fill(ds, "WEB_SEL_SECURITY_QUESTIONS"); 
String questions = ds.Tables["WEB_SEL_SECURITY_QUESTIONS"].Rows[]["QUESTION"].ToString(); 
securityQuestion1.DataSource = questions; 
securityQuestion1.DataBind(); 

但這種填充選擇框只有一個記錄,並生成這樣的列表項,每個列表項只有一個字符,只有第一個記錄。

回答

1

你可以嘗試:

securityQuestion1.DataSource = ds.Tables["WEB_SEL_SECURITY_QUESTIONS"]; 
securityQuestion1.DataTextField = "QUESTION"; 
securityQuestion1.DataValueField = "KEYCODE"; // Or whatever the value field needs to be. 
securityQuestion1.DataBind(); 

你的第一次嘗試做的是隻是把從一個問題是單個值,並將它作爲整個數據源。所以你需要瞄準的是讓整個安全問題表成爲數據源,然後給出它提示哪些列用於值和顯示(DataTextField,DataValueField)。

+0

非常感謝你。我不知道這是靈活的訪問和綁定行...完美的工作。再一次感謝你 – Dilan87 2012-04-05 12:38:54