Hi Members,
I have a dropdownlist, when the page is loaded, I want to set the first option of the dropdownlist to be selected. Then I will create a string, get the first option text, and create charts based on the first option text, then display the charts on the web. User does not have to click on anything, when the page is first loaded correct charts should be shown based on the first option text in the dropdownlist.
This is my codes:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownList();
DropDownList.ClearSelection();
DropDownList.SelectedIndex = 1;
BindChart();
}
}
//How I populate the dropdownlist: Based on what is selected from a listbox, I will bind those options into the dropdownlist
public void BindDropDownList()
{
DateTime choosenDate = DateTime.MinValue;
using (SqlConnection conn = new SqlConnection(dbConn))
{
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
{
//Lost to hold the values
List<DateTime> listCopy = new List<DateTime>();
DateTime dt;
string values = String.Join(", ", ListBox1.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Text));
if (values.Contains("Select All"))
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
if (DateTime.TryParse(li.Text, out dt))
{
listCopy.Add(dt);
}
}
}
else
{
//Loop through each items in listbox and then add it to list
foreach (ListItem li in ListBox1.Items)
{
//check if item is selected
if (li.Selected == true)
{
//add items to list
listCopy.Add(DateTime.Parse(li.Text));
}
}
}
//compare and sort so that the latest date comes on top
listCopy.Sort((x, y) => y.CompareTo(x));
//clear the items in dropdownlist
DropDownList.Items.Clear();
//set the datasource to dropdownlist
DropDownList.DataSource = listCopy;
//set the dateformatstring in dropdownlist
DropDownList.DataTextFormatString = "{0:dd-MMM-yyyy}";
//Bind the dropdownlist
DropDownList.DataBind();
//how I generate the chart based on dropdownlist selected option
public void BindChart()
{
//Create new TabContainer
AjaxControlToolkit.TabContainer container = new AjaxControlToolkit.TabContainer();
container.ID = "TabContainer";
container.EnableViewState = false;
container.Tabs.Clear();
string selectedItem= DropDownList.SelectedItem.ToString();
if (ListBox1.SelectedValue == "Select All")
{
foreach (ListItem item in ListBox1.Items)
{
if (item.Text == "Select All")
{
continue;
}
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel();
panel.HeaderText += item.Text;
container.Tabs.Add(panel);
Label tabContent = new Label();
tabContent.ID += item.Text;
tabContent.Text += item.Text;
DataTable tg = new DataTable();
DataRow dr;
tg.Columns.Add(new DataColumn("DATE"));
tg.Columns.Add(new DataColumn("STATUS", typeof(string)));
tg.Columns.Add(new DataColumn("TITLE", typeof(string)));
tg.Columns.Add(new DataColumn("TYPE", typeof(string)));
tg.Columns.Add(new DataColumn("MAX", typeof(int)));
tg.Columns.Add(new DataColumn("MIN", typeof(int)));
tg.Columns.Add(new DataColumn("AVG", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE25", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE50", typeof(int)));
tg.Columns.Add(new DataColumn("PERCENTILE75", typeof(int)));
foreach (GridViewRow gvr in GridView1.Rows)
{
if (gvr.Cells[2].Text == tabContent.Text.ToString() && gvr.Cells[0].Text == chartJobRunDate.ToString())
{
dr = tg.NewRow();
dr["DATE"] = gvr.Cells[0].Text;
dr["STATUS"] = gvr.Cells[1].Text;
dr["TITLE"] = gvr.Cells[2].Text;
dr["TYPE"] = gvr.Cells[3].Text;
dr["MAX"] = int.Parse(gvr.Cells[4].Text);
dr["MIN"] = int.Parse(gvr.Cells[5].Text);
dr["AVG"] = int.Parse(gvr.Cells[6].Text);
dr["PERCENTILE25"] = int.Parse(gvr.Cells[7].Text);
dr["PERCENTILE50"] = int.Parse(gvr.Cells[8].Text);
dr["PERCENTILE75"] = int.Parse(gvr.Cells[9].Text);
tg.Rows.Add(dr);
}
}
if (tg.Rows.Count > 0)
{
Chart Chart1= new Chart();
Chart1.DataSource = tg;
Chart1.Series.Add(new Series());
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
List<object> lst = tg.AsEnumerable().ToList<object>();
foreach (DataRow row in tg.Rows)
Chart1.Series[0].Points.AddXY(row["STATUS"], new object[] { row["MAX"], row["MIN"], row["AVG"], row["PERCENTILE25"], row["PERCENTILE50"], row["PERCENTILE75"] });
Chart1.Series[0].ChartType = SeriesChartType.BoxPlot;
Title title = new Title();
title.Text = (tg.Rows[0]["TITLE"].ToString());
Chart1.Titles.Add(title);
//create chartareas
ChartArea ca = new ChartArea();
ca.AxisX = new Axis();
ca.AxisY = new Axis();
ca.AxisY.MajorGrid.Enabled = false;
Chart1.ChartAreas.Add(ca);
//databind
Chart1.DataBind();
Chart1.Visible = true;
panel.Controls.Add(Chart1);
}
}
}
}
}
Question: How to set the
first option of the dropdownlist as
selected, then use
string to retrieve the
text of this first option, then create
chart based on this first option, and display the chart out when the page is
first loaded without having to click on anything?
Appreciate if someone can help me on this, thanks a lot!! :)
Regards,
Felicia