Asp.net BulletedList control tutorial
As i said in my last post, i'm developing an asp.net cms system for web programming class. First time, i hated asp.net. I couldn't take beginning classes in uni. so it was really hard to start asp.net with no experiance about it. Later when i gain more experiance about it, asp.net is really easy and enjoyable web programming platform.
No need to say more words about it, let's take a look at bulletedlist control usage in asp.net.
Add an bulletedlist control to project. In source you will see
<asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Square"
DisplayMode="LinkButton" onClick="ItemsBulletedList_Click" Target="_self" >
</asp:BulletedList>
part. BulletedList ID is id of control, list style square, display mode we use is linkbutton, onclick event handler funcion is ItemsBulletedList_Click and target is self. You can manage these options in properties of control in desing tab.
You can fill bulleted list with manuel but we'll get items from database. I have a table in database named "categories" and i want to get all categories from table and list it in bulletedlist control. The important part of data binding is BulletedList Text Field and BulletedList Value Field
So i have a listAllCats function and i call it in page_load.
protected void listCategories()
{
try
{
String connectionStr = "Data Source=.;Initial Catalog=cms;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionStr);
con.Open();
SqlCommand cmd = new SqlCommand("listAllCats", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "cats");
BulletedList1.DataSource = ds.Tables["cats"].DefaultView;
BulletedList1.DataTextField = "name";
BulletedList1.DataValueField = "id";
BulletedList1.DataBind();
con.Close();
}
catch (Exception)
{
Response.Write("Cannot list categories!");
}
}
As you can see i connected to database, called my stored procedure and binded data from my data set to bulletedlist control.
Now we need an event handler function for items when clicked on named ItemsBulletedList_Click. I have a view_category.aspx page with catid query string variable and when we click on a category (item) in bulleted list i want to list news in that category. Lets see how it is:
protected void ItemsBulletedList_Click(object sender, BulletedListEventArgs e)
{
Response.Redirect("view_category.aspx?catid=" + Convert.ToInt32(BulletedList1.Items[e.Index].Value));
}
You see, it's easy to add your categories in sidebar menu with bulletedlist control in asp.net
Thanks for reading.










