从C#代码创建LI到ASPX页面

时间:2014-05-08 19:52:00

标签: c# html asp.net

我有一个带有asp:BulletedList控件的ASPX页面,我希望使用C#从代码隐藏中填充。

这是我的SQL表(TBook):

LOCATION
New Rochelle
Purchase
Rye
Scarsdale
White Plains
Yonkers

到目前为止,我的ASP页面看起来像这样:

<div style="width: 80%; margin: 0 auto; border: 1px solid #A7DFFF; background: #DDDDDD;">
        <div style="width: 100%; background: #0181C7; height: 50px; line-height: 50px;">
            <span style="color: #fff;text-shadow: 0px 1px 0px #999, 0px 2px 0px #888, 0px 3px 0px #777, 0px 4px 0px #666, 0px 5px 0px #555, 0px 6px 0px #444, 0px 7px 0px #333, 0px 8px 7px #001135;font: 35px 'ChunkFiveRegular';">LOCATIONS</span>
        </div>
        <div style="height: 15px;"></div>
        <asp:BulletedList class="ulLocation" id="ulLocation_selector" runat="server">  
        </asp:BulletedList>
        <!--<ul class="ulLocation" id="ulLocation2_selector" runat="server">
            <li class="liSubLocation active" data-trait-id="9">
                <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=66" class="premote trait-link large btn" data-trait-id="9">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">New Rochelle</span>
                    <span class="count">6</span>
                </a>
            </li>
            <li class="liSubLocation active" data-trait-id="12">
                <a href="/locations/new-york/neighborhoods?tags[]=66&amp;tags[]=9" class="premote trait-link large btn" data-trait-id="12">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">Purchase</span>
                    <span class="count">6</span>
                </a>
            </li>
            <li class="liSubLocation" data-trait-id="14">
                <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=14&amp;tags[]=66&amp;tags[]=9" class="premote trait-link large btn" data-trait-id="14">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">Rye</span>
                    <span class="count">4</span>
                </a>
            </li>
            <li class="liSubLocation" data-trait-id="5">
                <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=5&amp;tags[]=66&amp;tags[]=9" class="premote trait-link large btn" data-trait-id="5">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">Scarsdale</span>
                    <span class="count">6</span>
                </a>
            </li>
            <li class="liSubLocation" data-trait-id="3">
                <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=3&amp;tags[]=66&amp;tags[]=9" class="premote trait-link large btn" data-trait-id="3">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">White Plains</span>
                    <span class="count">5</span>
                </a>
            </li>
            <li class="liSubLocation active" data-trait-id="66">
                <a href="/locations/new-york/neighborhoods?tags[]=12&amp;tags[]=9" class="premote trait-link large btn" data-trait-id="66">
                    <span class="check"><i class="icon icon-ok"></i></span>
                    <span class="name">Yonkers</span>
                    <span class="count">6</span>
                </a>
            </li>
        </ul>-->
    </div>

到目前为止,C#代码如下所示:

string connectionString="Data Source=myServBook;InitialCatalog=MyDB; UserID=srvdb;" + 
                        "Password=servdbtest0980";

    protected void Page_Load(object sender, EventArgs e)
    {
        /*using(SqlConnection sqlConnection=new SqlConnection(connectionString))
{
     string insertStatement="SELECT FROM [MyDB].[dbo].[TBook]";
     SqlCommand sqlCommand=new SqlCommand(insertStatement,sqlConnection);
     /* ADD THE LI HERE */
     /*sqlConnection.Open();
     sqlCommand.ExecuteNonQuery();
}*/
    }

我有以下代码,它基于DataSource添加LI:

DataTable dt = getData(); //Insert your datasource here

    foreach(DataRow row in dt.Rows){
        HtmlGenericControl li = new HtmlGenericControl("li");
        li.Attributes.Add("data-trait-id", row["TraitID"].ToString());

        HtmlAnchor a = new HtmlAnchor();
        a.Attributes.Add("data-trait-id", row["TraitID"].ToString());

        HtmlGenericControl span1 = new HtmlGenericControl("span");
        span1.Attributes.Add("class", "name");
        span1.InnerText = row["Name"].ToString();
        a.Controls.Add(span1);

        HtmlGenericControl span2 = new HtmlGenericControl("span");
        span2.Attributes.Add("class", "count");
        span2.InnerText = row["Count"].ToString();
        a.Controls.Add(span2);

        li.Controls.Add(a);
        ulSpecialty_selector.Controls.Add(li);
    }

上面的代码使用了DataSource。如何修改它以便根据表中的行数添加LI的数量,类似于我的ASPX页面中注释掉的HTML代码?

Link = "#"
Name = Table Row Value
Count = Table Row Number

1 个答案:

答案 0 :(得分:1)

以下是我提出的问题,唯一的问题是你想要一个右边的数字,这个解决方案不会这样做。但是,您可以使用转发器,这样您就可以使用右侧的号码。无论如何,这是一个让你通过直到有人提供更好解决方案的想法...

        cblTest.DataSource = "YourDataTableOrDataset";
        cblTest.DataValueField = "YourIDColumnFromYourTable";
        cblTest.DataTextField = "YourColumnName";
        cblTest.RepeatDirection = RepeatDirection.Horizontal;//Option of vertical
        cblTest.RepeatColumns = 5; //or however many you need i.e how many columns you want to go across before repeating
        cblTest.CssClass = "YourCSSClass"; //for styling the checkboxlist
        cblTest.DataBind();