将项目从 SQL 表插入引导下拉列表 [英] Inserting items into bootstrap-dropdown from SQL Table

查看:17
本文介绍了将项目从 SQL 表插入引导下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事 asp.net 项目,我的语言背后的代码是 c#.我有一个引导程序下拉列表,我想在其中从 SQL 表中获取项目.有没有人会在这方面帮助我.提前致谢.

操作、另一个操作和其他项目应来自数据库(类别)表.在服务器端使用c#语言是非常可观的.

解决方案

在 WebForms 中有一些方法可以做到这一点,但首先您需要使下拉菜单的 DIV 元素可从代码隐藏.

看到这个片段了吗?

请注意,我添加了 2 个属性:id="myDropdownMenu"runat="server".

此后,您可以转到代码隐藏以开始从数据源填充菜单.

据我所知,至少有两种方法可以做到这一点.

通过操作 InnerHtml 属性,像这样:

 private void DisplayMenuByConstructingHtmlTags(List menuList){var menuHtml = "";foreach(menuList 中的字符串 menuText){menuHtml += ""+ menuText + "</a>
";}myDropdownMenu.InnerHtml = menuHtml;}

或者,将菜单添加为子控件,如下所示:

 private void DisplayMenuByAddingChildControls(List menuList){foreach(menuList 中的字符串 menuText){var linkMenu = new HyperLink() { CssClass = "dropdown-item", NavigateUrl = "#", Text = menuText };myDropdownMenu.Controls.Add(linkMenu);}}

由您决定,选择哪一个.

顺便说一句,为了完成这个例子,您可以尝试从 Page_Load 事件中调用这些方法之一,如下所示:

根据您的要求,我修改了示例,添加了到数据库表的连接.所以,这是加载数据的模块:

 私人列表LoadMenuFromTable(){string connectionString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ToString();var retVal = new List();使用 (var connection = new SqlConnection(connectionString)){使用 (var cmd = new SqlCommand("SELECT menu_text FROM Table_1", connection)){连接.打开();使用 (var reader = cmd.ExecuteReader()){而 (reader.Read()){retVal.Add((string)reader["menu_text"]);}}}}返回 retVal;}

这里是你应该如何调用模块:

 protected void Page_Load(object sender, EventArgs e){var menu = LoadMenuFromTable();DisplayMenuByAddingChildControls(menu);//或 DisplayMenuByConstructingHtmlTags(menu);}

哦,记得导入这两个库以使这个示例工作:

使用 System.Configuration;使用 System.Data.SqlClient;

希望有帮助.

I am working on asp.net project and my code behind language is c#. I have one bootstrap dropdown in which i want to get the items from SQL Table. is there anyone who will help me in this regard. thanks in advance.

<li class="nav-item dropdown">
    <a class="btn btn-light dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        Category
    </a>
    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="#">Action</a>
        <a class="dropdown-item" href="#">Another action</a>
        <a class="dropdown-item" href="#">Something else here</a>
    </div>
</li>

Action, Another Action and Something else items should come from database (Category) Table. Using c# language on server side is much appreciable.

There are some ways to do it in WebForms, but first you need to make the DIV element of the dropdown menu accessible from the Code Behind.

See this snippet?

<ul class="nav nav-tabs">
    <li class="nav-item dropdown">
        <a class="btn btn-light dropdown-toggle" href="#" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Category
        </a>
        <div id="myDropdownMenu" runat="server" class="dropdown-menu" aria-labelledby="navbarDropdown">
        </div>
    </li>
</ul>

Note that I added 2 attributes : id="myDropdownMenu" and runat="server".

After this you can go to Code Behind to start populating the menu from a data source.

At least there are 2 ways to do this, as far as I know.

By manipulating the InnerHtml property, like this :

    private void DisplayMenuByConstructingHtmlTags(List<string> menuList)
    {
        var menuHtml = "";

        foreach (string menuText in menuList)
        {
            menuHtml += "<a class="dropdown-item" href="#">" + menuText + "</a>
";
        }

        myDropdownMenu.InnerHtml = menuHtml;
    }

Or, by adding the menu as the child controls, like this :

    private void DisplayMenuByAddingChildControls(List<string> menuList)
    {
        foreach (string menuText in menuList)
        {
            var linkMenu = new HyperLink() { CssClass = "dropdown-item", NavigateUrl = "#", Text = menuText };
            myDropdownMenu.Controls.Add(linkMenu);
        }
    }

It's your call, which one to choose.

Btw, just to complete this example, you may try to call one of those methods from the Page_Load event, like this :

EDIT :

By your request, I've modified the samples by adding a connection to a table in a database. So, this is the module to load the data :

    private List<string> LoadMenuFromTable()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["YourConnectionStringName"].ToString();

        var retVal = new List<string>();
        using (var connection = new SqlConnection(connectionString))
        {
            using (var cmd = new SqlCommand("SELECT menu_text FROM Table_1", connection))
            {
                connection.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        retVal.Add((string)reader["menu_text"]);
                    }
                }
            }
        }
        return retVal;
    }

And here's how you should call the module :

    protected void Page_Load(object sender, EventArgs e)
    {
        var menu = LoadMenuFromTable();

        DisplayMenuByAddingChildControls(menu);
        // or DisplayMenuByConstructingHtmlTags(menu);
    }

Oh, and remember to import these two libraries to make this sample works :

using System.Configuration;
using System.Data.SqlClient;

Hope it helps.

这篇关于将项目从 SQL 表插入引导下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆