如何从数据库加载chechbox文本并以表格形式显示? [英] how to load chechbox text from database and display in form?

查看:69
本文介绍了如何从数据库加载chechbox文本并以表格形式显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我试图从数据库加载复选框文本..



here i am trying to load a checkbox text from database..

conn.Open();
    SqlCommand chk = new SqlCommand("select activity_names from activity_tbl", conn);
            SqlDataAdapter da = new SqlDataAdapter(chk);
          
            DataSet dst = new DataSet();
            da.Fill(dst);
            chk_yoga.Text = dst.Tables[0].Rows[0][1].ToString();
conn.Close();

推荐答案

您不必使用数据表来执行此操作。这将完成这项工作:



You don''t have to use a datatable to do this. This will do the job:

var sql = "select top 1 activity_names from activity_tbl";
var cmd = new SqlCommand(sql, conn);
conn.Open();
chk_yoga.Text = cmd.ExecuteScalar().ToString();
conn.Close();





编辑:

首先,阅读全​​部activitytypes:




First of all, read all the activitytypes:

var activity_names = new List<string>();
var cmd = new SqlCommand("select activity_names from activity_tbl", conn);
conn.Open();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
    activity_names.Add(dr.GetString(0));
}
conn.Close();



现在你的所有活动类型都在一个字符串列表中。



然后你可以继续生成你的复选框:



首先你添加一个占位符。使用了plhMyPlaceholder的ID


Now you have all your activitytypes in a list of string.

Then you can go ahead and generate your checkboxes:

First you add a Placeholder in your aspx-page. With an Id of plhMyPlaceholder

<asp:PlaceHolder runat="Server" ID="plhMyPlaceholder">



然后为该占位符添加每个ativitytype的复选框:


Then you add a checkbox for each ativitytype to that placeholder:

// Loop through all your activitytypes
foreach (var name in activity_names)
{
    // Create a new checkbox
    var chk = new CheckBox();
    chk.Text = name;

    // Add the checkbox to your placeholder.
    plhMyPlaceholder.Controls.Add(chk);

    // You could add som more controls to your placeholder. A br-tag maybe?
    plhMyPlaceholder.Controls.Add(new LiteralControl("<br/>"));
}


这篇关于如何从数据库加载chechbox文本并以表格形式显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆