Reputation: 9225
I have an ASPX page with a asp:BulletedList
control which I would like to populate from code-behind using C#.
Here is my SQL Table (TBook
):
LOCATION
New Rochelle
Purchase
Rye
Scarsdale
White Plains
Yonkers
My ASP page looks like this so far:
<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&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&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&tags[]=14&tags[]=66&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&tags[]=5&tags[]=66&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&tags[]=3&tags[]=66&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&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>
The C# code so far looks like this:
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();
}*/
}
I have the following code which adds the LI based on DataSource:
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);
}
The above code uses a DataSource. How can I modify it so it adds the number of LI based on the number of rows in the table, similar to the commented out HTML code in my ASPX page?
Link = "#"
Name = Table Row Value
Count = Table Row Number
Upvotes: 0
Views: 774
Reputation: 3129
Here is what I came up with for you, the only problem is that you wanted a number on the right as well, this solution won't do that. However you can use a repeater, that will allow you to have your number to the right. Anyhow this is some idea to get you through till someone gives a better solution...
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();
Upvotes: 1