Click here to Skip to main content
15,883,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
HTML
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
Posted
Updated 4-Oct-15 20:52pm
v3
Comments
DamithSL 4-Oct-15 23:39pm    
how you bind DropDownList?

in your page load method do as below
C#
//Bind DropDownList first
BindDropDownList(); 
//now set the selectedindex 
DropDownList1.SelectedIndex = 0; // select index of DropDownList
//Now you can call other data bindings which depend on DropDownList selected index
LoadCahrt();
 
Share this answer
 
Comments
Member 11999641 5-Oct-15 0:02am    
hi DamithSL, have tried on this method but doesn't work :(
so on the page load method, I bind the dropdown list first, then set selected index and bind other methods that depend on the selected index, but still the same result.
DamithSL 5-Oct-15 0:04am    
can you updated the question with new code which you have tried?
Member 11999641 5-Oct-15 2:54am    
hi DamithSL, I have updated my question with new code thanks!
DamithSL 5-Oct-15 3:00am    
you haven't use selectedItem when you loading chart control.
Umm, It's a wild guess.

try to bind the dropdown before the page loads.
Write your dropdown bindin in Page.PreInit[^] and set the selectedIndex = 0.

And on Page.Load call the LoadChart();

-KR
 
Share this answer
 
You should bind the related methods after dropdown loaded in page load event at the sametime you need to call the related methods on dropdownselectedIndex event too.

C#
Protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Bind_DropDown();
        Bind_RelatedMethods();
    }
}
protected void ddl_OnSelectedIndexChanged(object sender, EventArgs e)
{
    Bind_RelatedMethods();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900