简体   繁体   中英

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

<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

Account Number: 6 520 325-9

Account Number: 80-000178814-0162499-3

Account Number: 100269408-Swr

Account Number: 100034269

Account Number: 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. 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 . 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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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