【问题标题】:How to data bind to a HTML selectbox如何将数据绑定到 HTML 选择框
【发布时间】:2012-04-19 04:00:54
【问题描述】:

我的 sql 数据库中有一个名为“WEB_SEL_SECURITY_QUESTIONS”的存储过程,它返回如下表。

SEQURITY_QUESTIONS_KEY、KEYCODE、QUESTION

我需要用执行上述过程得到的问题列表填充一个 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(); 

但这只会用一条记录填充选择框,并以这样一种方式生成列表项,即每个列表项只有一个字符,并且只有第一条记录。

【问题讨论】:

    标签: c# asp.net drop-down-menu data-binding


    【解决方案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)。

    【讨论】:

    • 哇,非常感谢。我不知道灵活的访问和绑定行......工作得很好。再次感谢您
    猜你喜欢
    • 2019-01-24
    • 2018-11-13
    • 1970-01-01
    • 2018-12-31
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多