简体   繁体   English

ASP.NET C#动态从存储的proc添加项目到列表

[英]ASP.NET C# Add items to list from stored proc dynamically

I would like to have my accounts shown like below (In a List) 我希望我的帐户显示如下(在列表中)

<div id=AccountDiv" class"account-table">

<div class="color-1">
<h3>Account Information</h3>
<ul>
    <li style="text-align: left" >Electric: Austin Energy - 10265</li>
    <li style="text-align: left">Gas: Texas Gas Service - 15754892 </li>
    <li style="text-align: left">Propane: Propane Austin - 577-12</li>
</ul>
</div>

<div>

I would like to pull each account information from a database dynamically. 我想动态地从数据库中提取每个帐户信息。 I'm new to asp.net so I wrote the below 我是asp.net的新手,所以写了以下内容

<div id=AccountDiv" class"account-table">
<asp:Panel ID="AccountDiv" runat="server" CssClass="account-table">
<div class="color-1">
<h3>Account Information</h3>
<ul>

</ul>
</div>
<asp:Panel>
<div>

This is what I wrote back-end. 这就是我写的后端。

void PopulateAccountInformation()
    {

        DataAccess da = new DataAccess();
        da.AddParameter("SiteID", SiteID, DataAccess.SQLDataType.SQLInteger, 4);
        SiteHeader = da.runSPDataSet("GetSiteAccountList");

       for (int i = 0; i < SiteHeader.Tables[0].Rows.Count; i++)
        {
            Label LabelAccount = new Label();

            LabelAccount.Text = "Account Number: " + SiteHeader.Tables[0].Rows[i]["Account Number"].ToString();
            AccountDiv.Controls.Add(LabelAccount);
            AccountDiv.Controls.Add(new LiteralControl("<br />"));

        }


    }

It displays the information like: 它显示如下信息:

Account Number: 3 786 843-7 帐号:3786843-7

Account Number: 6 520 325-9 帐号:6520325-9

Account Number: 80-000178814-0162499-3 帐号:80-000178814-0162499-3

Account Number: 100269408-Swr 帐号:100269408-Swr

Account Number: 100034269 帐号:100034269

Account Number: 100269408 帐号:100269408

Which is nice, but How to I write it so it makes a list for each Account? 哪个很好,但是如何编写它以便为每个帐户列出一个列表?

Thanks 谢谢

Add a Literal control to your page like this: 像这样向您的页面添加文字控件:

<asp:Literal ID="litContent" runat="server"></asp:Literal>

Then your function as following (not tested): 然后您的功能如下(未经测试):

void PopulateAccountInformation()
{
    DataAccess da = new DataAccess();
    da.AddParameter("SiteID", SiteID, DataAccess.SQLDataType.SQLInteger, 4);
    SiteHeader = da.runSPDataSet("GetSiteAccountList");
    string content = "<ul>";
    for (int i = 0; i < SiteHeader.Tables[0].Rows.Count; i++)
    {
       content +="<li style=\"text-align: left\" >" + SiteHeader.Tables[0].Rows[i]["Account Number"].ToString() + "</li>";

    }
    content += "</ul>";
    this.litContent.Text = content;
}

Look into using a Repeater or DataList control. 研究使用Repeater或DataList控件。 Those are made to display data, you don't need to manually add controls like you're trying to do. 这些都是用来显示数据的,您无需像尝试那样手动添加控件。

You want to use the asp built-in server control ListBox . 您要使用asp内置服务器控件ListBox Like so: 像这样:

<asp:ListBox ID="lstAccounts" runat="server"></asp:ListBox>

Then in your code-behind, add the elements like so: 然后在您的代码隐藏中,添加如下元素:

void PopulateAccountInformation()
    {

        DataAccess da = new DataAccess();
        da.AddParameter("SiteID", SiteID, DataAccess.SQLDataType.SQLInteger, 4);
        SiteHeader = da.runSPDataSet("GetSiteAccountList");

        foreach(DataRow account in SiteHeader.Tables[0].Rows)
        {
            this.lstAccounts.Items.Add(new ListItem("Account Number: " + account["Account Number"].ToString()));
        }
    }

This should work. 这应该工作。

Markup: 标记:

<div id=AccountDiv" class"account-table">
<div class="color-1">
<h3>Account Information</h3>
<asp:Repeater ID="rptrAccounts" runat="server">
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    Account Number:&nbsp;<%# Eval("Account Number") %>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
</asp:Repeater>
</div>
</div>

Code behind: 后面的代码:

void PopulateAccountInformation()
{

    DataAccess da = new DataAccess();
    da.AddParameter("SiteID", SiteID, DataAccess.SQLDataType.SQLInteger, 4);
    SiteHeader = da.runSPDataSet("GetSiteAccountList");

    rptrAccounts.DataSource = SiteHeader.Tables[0];
    rptrAccounts.DataBind();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM