从表中的SQL数据库中检索复选框名称(值)为ASCX文件 [英] Retrieving check box name (value) from a table in SQL database into ascx file

查看:279
本文介绍了从表中的SQL数据库中检索复选框名称(值)为ASCX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在我的SQL数据库名为Importtabs表选项卡名称。而不是硬编码的标签名称我正在引进我想要从上述表中的名称,并将它们设置为在ASCX一边我复选框列表值。这里是我先前使用​​硬codeD版:

I have tab names stored in a table named 'Importtabs' in my SQL database. Instead of hard coding the name of tabs i am importing i want to retrieve the names from the above mentioned table and set them as values for my checkbox list on the ascx side. Here is the hardcoded version that i was earlier using:

<div style="overflow: auto;"> 
<asp:CheckBoxList ID="CheckBoxList1" BorderStyle="None" runat="server" RepeatColumns="3">
<asp:ListItem>All Temporary Differences</asp:ListItem>
<asp:ListItem>All Permanent Differences</asp:ListItem>
<asp:ListItem>All BS Only Differences</asp:ListItem>
<asp:ListItem>RTA Temp</asp:ListItem>
<asp:ListItem>RTA Perm</asp:ListItem>
<asp:ListItem>RTA Temp Other</asp:ListItem>
<asp:ListItem>RTA Perm Other</asp:ListItem>
<asp:ListItem>RTA Other Expense</asp:ListItem>
</div>

我怎样才能做类似的事情,如上图所示,但使用从Importtabs'表在我的数据库的名字(所有永久性差异'等'的所有暂时性差异)。请帮助。

How can i do a similar thing as shown above but using the names ( 'All Temporary Differences', 'All Permanent Differences' etc ) from the 'Importtabs' table in my Database. Please help.

推荐答案

假设你已经知道如何从数据库中检索数据,这code可用于将数据放到了的CheckBoxList

Assuming you already know how to retrieve data from a database, this code can be used to put that data into the CheckBoxList

protected void Page_Load(object sender, EventArgs e)
{
    // do not rebind the list if this is a postback; user input would be lost 
    if (this.IsPostBack)
    {
        return;
    }
    CheckBoxList1.DataSource = GetValues();
    CheckBoxList1.DataBind();
}

private string[] GetValues()
{
    // get data from database, with 'select <columnName> from Importtabs'
    // populate array
    // return values as a string[]
}

这会导致很多次到数据库,所以你可能要运行的GetValues​​()方法一次,然后缓存结果。

That will result in a lot of hits to your database, so you might want to run the GetValues() method once and then cache the results.

链接缓存和<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx相对=nofollow>的SqlConnection

这篇关于从表中的SQL数据库中检索复选框名称(值)为ASCX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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