Link to home
Start Free TrialLog in
Avatar of djshohit
djshohit

asked on

Being able to choose to create 2 or 1 Dundas chart depending on the click value in a Datalist

I have a DataList when clicked creates a Dundas chart on the same page. Depending on the item clicked in the Dundas chart, how would I create 2 charts, or one chart?

Thanks
Avatar of djshohit
djshohit

ASKER

Here's the code I am working with.
<asp:DataList ID="DataList1" runat="server" DataKeyField="KM_ReportCountry" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                        <asp:HyperLink ID="lnkTable" runat="server" NavigateUrl='<%# "~/Default.aspx?reportName=" + DataBinder.Eval(Container.DataItem, "reportname") + "&xval1=" + DataBinder.Eval(Container.DataItem, "xaxis1") + "&yval1=" + DataBinder.Eval(Container.DataItem, "yaxis1")  %>'>
                        <%# DataBinder.Eval(Container.DataItem,"report") %>
        </asp:HyperLink>
 
 
            </ItemTemplate>
        </asp:DataList><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Global KPIConnectionString %>"
        
                    SelectCommand="procGetReports" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="CountryListDropDown" Name="Country" 
                            PropertyName="SelectedValue" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
 
<br />
               
                <br />
            </td>
            <td>
                <dcwc:Chart ID="Chart1" runat="server" Height="423px" Palette="Pastel" 
                    Width="773px">
                    <Legends>
                        <dcwc:Legend Name="Default">
                        </dcwc:Legend>
                    </Legends>
                    <Series>
                        <dcwc:Series BorderColor="64, 64, 64" Name="Series1" ShadowOffset="1">
                        </dcwc:Series>
                        <dcwc:Series BorderColor="64, 64, 64" Name="Series2" ShadowOffset="1">
                        </dcwc:Series>
                    </Series>
                    <ChartAreas>
                        <dcwc:ChartArea Name="Default">
                        </dcwc:ChartArea>
                    </ChartAreas>
                </dcwc:Chart>
            </td>
        </tr>
    </table>
 
 
----------------------- C# Code
 
protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["reportName"] != null)
        {
            //create the datatable
            DataTable dt = new DataTable();
            //string connectionString = ConfigurationManager.ConnectionStrings["qeConnectionString1"].ConnectionString;
           // string connection = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
           SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
           connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
            connection.Open();
 
            SqlCommand sqlCmd = new SqlCommand("SELECT * FROM " + Request.QueryString["reportName"].ToString(), connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);
 
 
            //bind to dundas
            Chart1.DataSource = dt;
            Chart1.DataBind();
 
            Chart1.Series.Add("Totals");
            Chart1.Series["Totals"].ValueMemberX = Request.QueryString["xval1"].ToString();
            Chart1.Series["Totals"].ValueMembersY = Request.QueryString["yval1"].ToString();
 
        }
    }

Open in new window

I think we first need to be sure that we include the other x and y values in your query string.  Right now, we only have xval1 and yval1.  You should add xval2 and yval2....which will come from your sproc result set.
Am I write in assuming that the second chart uses the same table for data, but it charts them on a different x and y?  That way you have two charts with the same data, but two ways of looking at it?
 
I think I got the query string to get in the two values for the second table. The table only maps to the column names in the other tables (from where we are getting the actual data to map the graphs).

My problem is that in some cases, there are no x2 and y2 values, in which case you need only 1 graph instead of two.

P.S. I also noted that in the query string, the column names of the table (which I have as XAxis1, YAxis1, etc) dont have to be case sensitive. Thats a first I have seen.
ItemTemplate>
                        <asp:HyperLink ID="lnkTable" runat="server" NavigateUrl='<%# "~/Default.aspx?reportName=" + DataBinder.Eval(Container.DataItem, "ReportName") + "&xval1=" + DataBinder.Eval(Container.DataItem, "XAxis1") + "&yval1=" + DataBinder.Eval(Container.DataItem, "YAxis1")+ "&xval2=" + DataBinder.Eval(Container.DataItem, "XAxis2") + "&yval2=" + DataBinder.Eval(Container.DataItem, "YAxis2") %>'>
                        <%# DataBinder.Eval(Container.DataItem,"Report") %>
                        
        </asp:HyperLink>
 
 
            </ItemTemplate>

Open in new window

Am I write in assuming that the second chart uses the same table for data, but it charts them on a "different x and y?  That way you have two charts with the same data, but two ways of looking at it?"

The two charts have data values from the same table but different columns. So there are two different columns that are present in the same table for two different charts. But they both have ths same x axis (the value it is being compared to), which is months.
I hope this isnt too complicated. It should be pretty simple for you though :). Let me know if you have any questions :D. And again, thank you for your help.
If you dont have values for x2 and y2, then its easy to tell that you wont need to do a second graph.  We can figure this out easily from our dataset.
if(dt.Rows[0]["Xaxis2"] != System.DBNull.Value){
Chart2.Visible = true;
//bind second dundas chart with x2 and y2.
}
I havent really done this, but I would think that you should create the two dundas charts and set their visible properties to false.  Then when you find the appropriate x and y vals....set them to visible and bind.  Make sense?
Whoops...been a long day.  Dont check the datatable...check the querystring.  So it should be:
if(Request.QueryString["xval2"] != null){
//visible = true;
//bind chart2
}
I noticed your question asked about binding the chart.  Are you having issues with getting the datatable to bind, like we have in the codebehind?
            Chart1.DataSource = dt;
            Chart1.DataBind();

            Chart1.Series.Add("Totals");
            Chart1.Series["Totals"].ValueMemberX = Request.QueryString["xval1"].ToString();
            Chart1.Series["Totals"].ValueMembersY = Request.QueryString["yval1"].ToString();
 

Let me try that out!
Umm. If I have 2 charts, I will need 2 areas to bind it. Could you help me out with some code to create two areas?
So binding the first chart worked?
Also, since the chart will be in one particular area of the application (area meaning the front end of the application), is it possible (or a good idea) to have overlapping each other?
yea binding is fine. :).
Not sure what you mean by overlapping.
If we want to check for binding two charts,  I think we can try the code in the snippet.

           Chart1.DataSource = dt;
           Chart1.DataBind();
 
           Chart1.Series.Add("Totals");
           Chart1.Series["Totals"].ValueMemberX = Request.QueryString["xval1"].ToString();
           Chart1.Series["Totals"].ValueMembersY = Request.QueryString["yval1"].ToString();
 
if(Request.QueryString["xval2"] != null){
 
           Chart2.DataSource = dt;
           Chart2.DataBind();
           Chart2.Visible = true; 
 
           Chart2.Series.Add("Totals");
           Chart2.Series["Totals"].ValueMemberX = Request.QueryString["xval2"].ToString();
           Chart2.Series["Totals"].ValueMembersY = Request.QueryString["yval2"].ToString();
 
 
} else {
        Chart2.Visible = false; //now we wont see an empty chart on the page.
}

Open in new window

Error: The name 'Chart2' does not exist in the current context.
that is what I mean. I think i need to have a chart2 tag in the .aspx page right?
 


That is correct.  Just drag one onto your form.  It should be named Chart2 by default.
I think I didnt explain correctly. Let me explain better:
When we have 2 x and 2 y values, the chart should look something like this.
http://i34.tinypic.com/2nhj97k.jpg
When we have just one x and y value, the chart should look like a normal dundas chart, with one x and one y value.
Hope that makes things a little clear!
My company blocked that site you listed above.  Can you attach the jpg?
It sounds like you want one chart that charts two sets of data?  We have the first one working...now you want to add another dataset to the same chart?
Exactly. I dont know if we need 2 datasets (I guess you would know better about it). Basically the 2 charts come from the same table but different columns. the YAxis1 and YAxis2 store the name of those columns.
appscreen.JPG
So instead of having Chart2, can we add like another area in the chart to show 2 charts? I dont know how to add that code.
2 areas, 1 chart.  Both are bar graphs based on the same table with the same x(month) value, but different y(column) values?
BINGO!
no wait. One bar graph, one Line graph. The top graph is line graph, the botton graph is bar graph! Sorry.
Since we have the x and y values already, this sounds like we just need to figure out how to chart the two areas based on those values.  Instead of typing most of this out.  Try visiting this link and let me know if you need help integrating our vals with the technique.
 
http://www.dundas.com/Products/Chart/NET/Why/WhitePapers/ChartAreas.aspx 
Yea I saw that. My problem here was that this chart changes according to values passed in it. So I dont know if this would work for it.
It would.  You just have to create your second chart area based on the x and y vals we have coming in from the querystring.  
I guess I would need help to integrate that with my code. because anywhere in the code, it doesnt allow you to add X and Y values. Maybe I'm talking like a noob here. Could you help me with integrating this with my code?
You can set chartarea props using something like I have below.  Also, we need to attach each x,y series to a different chart area.  Does the code below give you any direction?
The first code block is about setting the props fo a chart area.
The second code block is for adding series to a chart.
The third code block is for attaching series to a chart area.

    // Set primary x-axis properties
    Chart1.ChartAreas["Default"].AxisY.MinorGrid.Interval = 0.25;
    Chart1.ChartAreas["Default"].AxisY.MajorGrid.Interval = 0.5;
    Chart1.ChartAreas["Default"].AxisY.LabelStyle.Interval = 0.5;
 
 
 
            // Add a series to the chart
            Series series = Chart1.Series.Add("Series1");
            series.ChartArea = "Default";
            series.ChartType = charts[Chart1.Series.Count-1];
            series.BorderWidth = 2;
 
 
 
    // Attach the first series to a chart area.
    Chart1.Series["Series1"].ChartArea = "Chart Area 1";
    
    // Attach the second series to a chart area.
    Chart1.Series["Series2"].ChartArea = "Chart Area 2";

Open in new window

Error: the name chart doesnt exist in current context.
Wont this work instead of the line 11 you posted above?
Chart1.Series["Series11"].ChartArea = "ChartArea1
Sure.
I would assume your code looks similar to the snippet below.  
By the way, Im leaving for the weekend about 3:00 EST and I wont be back online until Wed next week.

Chart1.DataSource = dt;
Chart1.DataBind();
 
Series totals = Chart1.Series.Add("Totals");
totals.ChartArea = "ChartArea1";
 
 
totals.ValueMemberX = Request.QueryString["xval1"].ToString();
totals.ValueMembersY = Request.QueryString["xval1"].ToString();
 
if(Request.QueryString["xval2"] != ""){
 
Series totals2 = Chart1.Series.Add("Totals2");
totals2.ChartArea = "ChartArea2";
 
totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
totals2.ValueMembersY = Request.QueryString["xval2"].ToString();
 
 
}

Open in new window

Whoops...second lines concerning Y vals should read:
totals.ValueMembersY = Request.QueryString["yval1"].ToString();

...
totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
 

Have you gotten any of this to work?
I think there is a problem in getting the graphs to come out right. It isnt filtering the data by country. So I tried a different method. Having 2 charts with an if else statement in the .aspx code. The problem is that I cant have a query where the name of the table is being input from a Query String.
 
I am attaching the code I have written. How can I get the data to be filtered by the country? Also shouldnt the C# code be in the click event of the dataset, instead of the page Load? i dont know how to capture the click event (if that is the case we'll use) of the dataset so might need help with that.

           <%if (Request.QueryString["yval2"] != "")
              {%>
                 
                <dcwc:Chart ID="Chart2" runat="server" BackColor="WhiteSmoke" 
                    BackGradientEndColor="White" BackGradientType="DiagonalLeft" 
                    BorderLineColor="26, 59, 105" BorderLineStyle="Solid" 
                    DataSourceID="YesX2Y2Value" Height="422px" Palette="Dundas" Width="764px">
                    <Legends>
                        <dcwc:Legend Alignment="Far" BackColor="White" BorderColor="26, 59, 105" 
                            Docking="Top" Enabled="False" LegendStyle="Column" Name="Default" 
                            ShadowOffset="2">
                        </dcwc:Legend>
                    </Legends>
                    <UI>
                        <Toolbar BorderColor="26, 59, 105">
                            <BorderSkin PageColor="Transparent" SkinStyle="Emboss" />
                        </Toolbar>
                    </UI>
                    <Titles>
                        <dcwc:Title Name="Title1">
                        </dcwc:Title>
                    </Titles>
                    <Series>
                        <dcwc:Series BorderColor="26, 59, 105" BorderWidth="2" ChartType="Line" 
                            Name="Series1" ShadowOffset="2"  ValueMemberX="Month">
                        </dcwc:Series>
                        <dcwc:Series ChartArea="Chart Area 1" Name="Series2" ValueMemberX="Month" 
                            XValueType="DateTime">
                        </dcwc:Series>
                    </Series>
                    <ChartAreas>
                        <dcwc:ChartArea BackColor="White" BorderColor="26, 59, 105" BorderStyle="Solid" 
                            Name="Default" ShadowOffset="2" AlignWithChartArea="Chart Area 1">
                            <AxisY>
                                <MajorGrid LineColor="Silver" />
                                <MinorGrid LineColor="Silver" />
                                <LabelStyle Format="N" />
                            </AxisY>
                            <AxisX>
                                <MajorGrid LineColor="Silver" />
                                <MinorGrid LineColor="Silver" />
                            </AxisX>
                            <AxisX2>
                                <MajorGrid LineColor="Silver" />
                                <MinorGrid LineColor="Silver" />
                            </AxisX2>
                            <AxisY2>
                                <MajorGrid LineColor="Silver" />
                                <MinorGrid LineColor="Silver" />
                            </AxisY2>
                        </dcwc:ChartArea>
                        <dcwc:ChartArea Name="Chart Area 1" BorderStyle="Solid" ShadowOffset="2">
                            <AxisY>
                                <LabelStyle Format="N" />
                            </AxisY>
                            <AxisX Margin="False">
                                <MajorTickMark Style="None" />
                                <LabelStyle Format="M" />
                            </AxisX>
                        </dcwc:ChartArea>
                    </ChartAreas>
                    <BorderSkin FrameBackColor="CornflowerBlue" 
                        FrameBackGradientEndColor="CornflowerBlue" PageColor="Control" 
                        SkinStyle="Emboss" />
                </dcwc:Chart><%}
              else
              {%>
              <dcwc:Chart ID="Chart1" runat="server" DataSourceID="Nox2y2Value" 
                    Palette="Pastel">
                    <Legends>
                        <dcwc:Legend Enabled="False" Name="Default">
                        </dcwc:Legend>
                    </Legends>
                    <Series>
                        <dcwc:Series BorderColor="64, 64, 64" BorderWidth="2" 
                            Name="Series1" ShadowOffset="1" ValueMemberX="Month">
                        </dcwc:Series>
                    </Series>
                    <ChartAreas>
                        <dcwc:ChartArea Name="Default">
                        </dcwc:ChartArea>
                    </ChartAreas>
                </dcwc:Chart>
                 <%} %>
                
            
            </td>
        </tr>
    </table>
    <asp:SqlDataSource ID="Nox2y2Value" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Global KPIConnectionString %>" 
        SelectCommand="SELECT @Col1, @Col2 
FROM @Table1 
WHERE Country=@Country">
        <SelectParameters>
            <asp:QueryStringParameter Name="Col1" QueryStringField="xval1" 
                DefaultValue="Month" />
            <asp:QueryStringParameter Name="Col2" QueryStringField="yval1" />
            <asp:QueryStringParameter Name="Table1" QueryStringField="reportName" 
                DefaultValue="" />
            <asp:ControlParameter ControlID="CountryListDropDown" Name="Country" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="YesX2Y2Value" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Global KPIConnectionString %>" 
        
        SelectCommand="SELECT @X1, @Y1, @Y2 FROM @TableName WHERE Country = @Country">
        <SelectParameters>
            <asp:QueryStringParameter Name="X1" QueryStringField="xval1" 
                DefaultValue="Month" />
            <asp:QueryStringParameter DefaultValue="" Name="Y1" QueryStringField="yval1" />
            <asp:QueryStringParameter DefaultValue="" Name="Y2" QueryStringField="yval2" />
            <asp:QueryStringParameter Name="TableName" QueryStringField="reportName" />
            <asp:ControlParameter ControlID="CountryListDropDown" Name="Country" 
                PropertyName="SelectedValue" />
        </SelectParameters>
    </asp:SqlDataSource>
 
------------------
 
 protected void Page_Load(object sender, EventArgs e)
    {
        
 
       
        if (Request.QueryString["yval2"] != "")
        {
            
            Chart2.Series["Series1"].ValueMemberX = Request.QueryString["xval1"];
            Chart2.Series["Series1"].ValueMembersY = Request.QueryString["yval1"];
          
            Chart2.Series["Series2"].ValueMemberX = Request.QueryString["xval2"];
            Chart2.Series["Series2"].ValueMembersY = Request.QueryString["yval2"];
        }
 
        else
        {
            Chart2.Series["Series1"].ValueMemberX = Request.QueryString["xval1"];
            Chart2.Series["Series1"].ValueMemberX = Request.QueryString["yval1"];
        }

Open in new window

I would do it in the page load.  There isnt a need to do a click event.  When you click on the link it essentially reloads the page, then you test for the querystring vars.  Depending on what vars are present, you display the graph(s). (See the snippet)
Does this method not work?
 

protected void Page_Load(object sender, EventArgs e){
if(Request.QueryString["xval1"] != ""){
 
     DataTable dt = new DataTable();
     dt = somemethodtogetdata
 
     Chart1.DataSource = dt; //dt is the data you need to chart
     Chart1.DataBind();
 
     Series totals = Chart1.Series.Add("Totals");
     totals.ChartArea = "ChartArea1";
 
 
     totals.ValueMemberX = Request.QueryString["xval1"].ToString();
     totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
     if(Request.QueryString["xval2"] != ""){
 
          Series totals2 = Chart1.Series.Add("Totals2");
          totals2.ChartArea = "ChartArea2";
 
          totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
          totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
     }
} 
}

Open in new window

But it isnt gonna take values and filter by country. Thats why the first code worked, but it didnt filter any results which resulted in a wierd graph. There needs to be a where clause somewhere, right?
A where clause would filter.  Thats where your previous dropdown selections come in handy for the datacall that populates your grid.
dt = methodToSelectWithWhereClause
Make sense?
Ok I guess I might need help with that method too. Sorry to bug you so much. I have attached the code below. I tried using the where clause and it gave me errors. Could you help me with that? I think we're almost done here.
 
 

 protected void Page_Load(object sender, EventArgs e)
    {
        
 
       if(Request.QueryString["xval1"] != ""){
 
     DataTable dt = CallDataTable();
     
 
     Chart1.DataSource = dt; //dt is the data you need to chart
     Chart1.DataBind();
 
     Series totals = Chart1.Series.Add("Totals");
     totals.ChartArea = "ChartArea1";
 
 
     totals.ValueMemberX = Request.QueryString["xval1"].ToString();
     totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
     if(Request.QueryString["xval2"] != ""){
 
          Series totals2 = Chart1.Series.Add("Totals2");
          totals2.ChartArea = "ChartArea2";
 
          totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
          totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
     }
}}
        public DataTable CallDataTable()
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
            connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
            connection.Open();
 
            SqlCommand sqlCmd = new SqlCommand("SELECT * FROM " + Request.QueryString["reportName"].ToString(), connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);
 
        }

Open in new window

The error I get with the code above is as follows:
 
Error 1 '_Default.CallDataTable()': not all code paths return a value
Ok I didnt have a return value. Stupid error. But the following error is something I get a lot when I was trying something. Its probably something basic too, but I havent been able to figure out whats wrong here!
query.JPG
Ok I got to fix that. I just need to do the where thing. I think thats the only thing I need help with so far. I'm attaching the code written thus far!
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["reportName"] != null)
        {
 
            if (Request.QueryString["xval1"] != "")
            {
 
                DataTable dt = CallDataTable();
 
 
                Chart1.DataSource = dt; //dt is the data you need to chart
                Chart1.DataBind();
 
                Series totals = Chart1.Series.Add("Totals");
                totals.ChartArea = "ChartArea1";
 
 
                totals.ValueMemberX = Request.QueryString["xval1"].ToString();
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
                if (Request.QueryString["xval2"] != "")
                {
 
                    Series totals2 = Chart1.Series.Add("Totals2");
                    totals2.ChartArea = "ChartArea2";
 
                    totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
                    totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
                }
 
            }
        }
    }
 
        public DataTable CallDataTable()
        {
            
                DataTable dt = new DataTable();
                SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
                connection.Open();
                
                SqlCommand sqlCmd = new SqlCommand("SELECT * FROM " + Request.QueryString["reportName"].ToString(), connection);
 
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
                sqlDa.Fill(dt);
            
                return dt;
            
        }

Open in new window

These emails are now getting blocked by my spam filter for some reason....
Anyway, it looks like you just about got it!  I think the where clause will come from the one dropdown you have for country.  I slightly modified the code above.  CountryDropDownList is what I called your dropdown for listing countries.

        public DataTable CallDataTable()
        {
            
                DataTable dt = new DataTable();
                SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
                connection.Open();
                
                SqlCommand sqlCmd = new SqlCommand("SELECT * FROM " + Request.QueryString["reportName"].ToString() + " WHERE Country = '" + CountryDropDownList.SelectedValue + "'", connection);
 
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
                sqlDa.Fill(dt);
            
                return dt;
            
        }

Open in new window

Ok I think I have almost got it, except one thing. It might be out of the scope of this question, but if you can answer it, it would be great! My deadline is today and I have just about got it!
The two drop downs, with country and city? When I click on the datalist link, their state changes to default types! Is there a way to keep their state intact?
Also is there a way so that the drop downs dont have any values selected (instead of the first value in the drop downs being selected)? I would prefer if the user explicitly selects the value. Sometimes the "Area" drop down only has one value, and it gets selected by default.
Thanks
Cause whenever one value in the datalist gets selected, the drop down change to their initial values, which sucks because when a user selects some other reportname in the datalist, it is actually showing the charts for a different set of data.
I dont have any default value set for the DropDowns.
You dont have a dropdown option like "Choose One" that is automatically selected when the page is first shown?  Is this what you want?
Yea. Basically when the dropdown is loaded, nothing should be selected. And once the value is being selected, the state of that dropdown remains intact, even after the DataList Item is clicked (which currently it changes to the first value available in the drop down).
Also, the code that you gave me gives an error. I thought I'll use the querystring to use the country from the table itself (which should work), but I do think selecting the dropdown value is better.
Here's the error:

Error	83	'System.Web.UI.WebControls.SqlDataSource' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'System.Web.UI.WebControls.SqlDataSource' could be found (are you missing a using directive or an assembly reference?)	

Open in new window

Thank you for your prompt responses!!!! :)
Dont worry about the error! Its the other problem that needs to be fixed. Otherwise we seem to be all set for now!
First, be sure you are loading the dropdownlist in the pageload using:
if(!IsPostBack){
      //load dropdowns
}
 
This way you can be sure that the selected values persist between pageloads.
To initially display "Choose One" in the dropdowns look at the code snippet.


ddlCountryList.DataTextField = "Name";
ddlCountryList.DataValueField = "ID";
ddlCountryList.DataSource = somedatatable;
ddlCountryList.DataBind();
 
ddlBuildings.Items.Add(new ListItem("Choose One", "Choose One"));

Open in new window

This is my final piece of code. Another error I get when I click around is as follows, which I have absolutely no clue how to handle it!
 "The chart series [MonsterIBSSeries1] is missing and cannot be deserialized. You can use Chart.CallbackStateContent="All" to preserve all chart content inlcuding series."
This happens when I try to change the country/Area AFTER I view the charts.
2 more things left and we are done! I am indebt to you for all your help!  

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["reportName"] != null)
        {
 
            if (Request.QueryString["xval2"] != "")
            {
 
                Chart2.Visible = false;
                DataTable dt = CallDataTable(Request.QueryString["xval1"].ToString(), Request.QueryString["yval1"].ToString(), Request.QueryString["yval2"].ToString(), CountryDropDownControl.SelectedValue.ToString());
 
 
                Chart1.DataSource = dt; //dt is the data you need to chart
                Chart1.DataBind();
 
                //   Chart1.Series["Series1"].ChartArea = "Default";
                Series totals = Chart1.Series.Add("MonsterIBSSeries1");
                totals.ChartArea = "Default";
 
 
                totals.ValueMemberX = Request.QueryString["xval1"].ToString();
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
                Series totals2 = Chart1.Series.Add("MonsterIBSSeries2");
                totals2.ChartArea = "ChartArea1";
 
 
                //Preserve state of callback
                // Chart1.CallbackStateContent = CallbackStateContent.All;
 
                totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
                totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
                Chart1.ChartAreas["Default"].CursorX.UserEnabled = true;
                Chart1.ChartAreas["ChartArea1"].CursorX.UserEnabled = true;
 
                Chart1.ChartAreas["Default"].AlignWithChartArea = "ChartArea1";
                Chart1.ChartAreas["Default"].AlignOrientation = AreaAlignOrientation.Vertical;
                Chart1.ChartAreas["Default"].AlignType = AreaAlignType.All;
            }
            else
            {
                Chart1.Visible = false;
 
 
 
                DataTable dt = CallDataTable1(Request.QueryString["xval1"].ToString(), Request.QueryString["yval1"].ToString(), CountryDropDownControl.SelectedValue.ToString());
 
                Chart1.DataSource = dt; //dt is the data you need to chart
                Chart1.DataBind();
 
 
                Series totals = Chart2.Series.Add("MonsterIBSSeries1");
                totals.ChartArea = "Default";
 
 
                totals.ValueMemberX = Request.QueryString["xval1"].ToString();
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
                Chart2.DataBind();
                //  totals.ValueMembersY = Request.QueryString["yval1"].ToString();
                Chart2.ChartAreas["Default"].CursorX.UserEnabled = true;
            }
 
        
        }
        else
        {
            Chart1.Visible = false;
            Chart2.Visible = false;
        }
    }
 
   
    public DataTable CallDataTable(string xval1, string yval1, string yval2, string country)
    {
 
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
        connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
        connection.Open();
 
        SqlCommand sqlCmd = new SqlCommand("SELECT [" + xval1 + "], [" + yval1 + "], [" + yval2 + "] FROM " + Request.QueryString["reportName"].ToString() + " WHERE Country = '" + 
           CountryDropDownControl.SelectedValue + "'", connection);
 
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
 
        return dt;
 
    }
 
    public DataTable CallDataTable1(string xval1, string yval1, string country)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
        connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
        connection.Open();
        // String dropdownvalue = CountryListDropDown.SelectedValue.ToString();
        // String dropdownval = Delete.Text;
        SqlCommand sqlCmd = new SqlCommand("SELECT [" + xval1 + "], [" + yval1 + "] FROM [" + Request.QueryString["reportName"].ToString() + "] WHERE Country= '" + CountryDropDownControl.SelectedValue + "'", connection);
        // "WHERE Country = " + dropdownvalue, connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
 
        return dt;
    }
}

Open in new window

So basically everything works unless they try to choose another set of options after viewing the charts.  Then you get this error?
I would put a break point on the pageload and see what values are getting passed in.  Step through the code until it breaks.  What line breaks?  Im assuming its Chart1.DataBind();
 
 
Yes. thats what breaks!!! Why do you think thats the case?
Also, Could you help me with the ISPostBack thing? There are a bunch of codes available online to help me out on that. Unfortunately none of them work!
The break error usually gets to something like this:
 
Cannot add series. A series with the name "MonsterIBSSeries1" already exists.  
This occurs on the line :

Series totals = Chart1.Series.Add("MonsterIBSSeries1")
 
:(
Here's the postback error I get. Its probably a syntax thing, except I have no idea to solve it!
postbackerror.JPG
Ive never dealt with the chart error before.  Try adding this Chart1.Series.Clear() right after:
        if (Request.QueryString["reportName"] != null)
        {
...
Do the same for Chart2.Series.Clear().  Im not sure if this will work and its a little hacky, but clearing out the series may help.
As far as postback error, why do you have this line - CountryDropDownControl.DataSourceID?  Cant you take that out?  Ive never used that before.  I also notice that you are getting the selectedvalue right after you bind....that is gonna give you the same value for that string every time.  Close your if(!ispostback) block after you DataBind();
I dont think I understand the following:
ountryList.DataTextField = "Name";
ddlCountryList.DataValueField = "ID";

I dont think I have an ID column in my table. Is that what this is mapping to?
I was trying to make it work, hence I was trying to do a few things.

CountryDropDownControl.DataTextField= "ReportName";
CountryDropDownControl.DataSource = CountryDropDownList;
CountryDropDownControl.DataBind();
CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
I get the error :
Both DataSource and DataSourceID are defined on 'CountryDropDownControl'.  Remove one definition."
 
CountryList.DataTextField = "Name"; //this is the text you see in the dropdown
ddlCountryList.DataValueField = "ID"; //this is the option value.
You can make both of these Name if you want.  Whatever you feel comfortable with.  
Make sure you have defined this:
CountryDropDownControl.DataTextField= "ReportName";
CountryDropDownControl.DataValueField= "ReportName";  
 Then you can do the rest.
CountryDropDownControl.DataSource = CountryDropDownList;
CountryDropDownControl.DataBind();
CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
 
That should work....
I did exactly as you mentioned earlier. Apparently it puts "Choose One" at the bottom of the list. Can it be at the top of the list so it gets selected?
  if (!IsPostBack)
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
        }

Open in new window

Choose One is at the bottom. Unfortunately the functionality doesnt remain! As soon as I click the datalist, the value goes back to Belgium for Country and National for Area! :(

 if (!IsPostBack)
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
          CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
        }

Open in new window

Ah...I think I remember why.  Your datalist links dont postback...they call the page again and pass the querystring.  Try the code snippet.
This makes sure that its not a postback and its also not the datalist links calling the page with a querystring.  

 if (!IsPostBack && Request.QueryString["xval1"] == null)
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
          CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
        }

Open in new window

I did the following and it has some errors but it should work eventually. The problem is that now the Area Drop Down Doesnt populate. I am assuming its taking the "Choose One" Value to populate the data and its not helping it!'
CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Insert(0, new ListItem("Choose One"));
          // CountryDropDownControl.Items.Add(new ListItem("Choose One"));
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Insert(0, new ListItem("Choose One"));
         //  AreaDropDownControl.Items.Add(new ListItem("Choose One"));
            

Open in new window

Yup. Now I am stuck and a little terrified :-P
The Area Control Doesnt populate anymore! It just says "Choose One". I didnt change anything, just the code (which happens to be pretty similar to what you mentioned). Aaah...any ideas?

   if (!IsPostBack && Request.QueryString["xval1"] == null)
 
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Insert(0, new ListItem("Choose One", "Choose One"));
        
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Insert(0, new ListItem("Choose One", "Choose One"));
         
        }

Open in new window

Items.Insert and Items.Add are different.  I think you are wiping the values out with Add.  
Use the add and try this to select Choose One.
 


CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));

Open in new window

Yea I did as you said. The Area Control doesnt populate anymore. I tried to bind it directly, and then it populates - just to make sure that the Connection is on and the database is fine.
!!BREATHER!!. ..So now what, pretty annoying problem huh!?! :)
Here's the code I tried. FYI I did try both Insert and Add. Same effect really!
     CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Add(new ListItem("Choose One"));
          //  CountryDropDownControl.Items.Insert(0, new ListItem("Choose One"));
            CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));
        
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One"));
           // AreaDropDownControl.Items.Insert(0, new ListItem("Choose One"));
           AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One")); 

Open in new window

So, what isnt working now?  If you do this, it should work fine.
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";  
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
 
Yea I tried that...didnt work :(
DropDown.bmp
Im not sure what the deal is here.  The above code should work without any issues.  Copy and paste your whole pageload content.  
Here you Go!
public partial class Economy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
     {
        
 
   
 
         if (!IsPostBack && Request.QueryString["xval1"] == null)
 
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
          
          
            CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));
 
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
 
            
        }
 
 
         
        
         
         
        if (Request.QueryString["reportName"] != null)
        {
 
            if (Request.QueryString["xval2"] != "")
            {
 
               // Chart1.CallbackStateContent= CallbackStateContent.All;
                Chart2.Visible = false;
                DataTable dt = CallDataTable(Request.QueryString["xval1"].ToString(), Request.QueryString["yval1"].ToString(), Request.QueryString["yval2"].ToString(), CountryDropDownControl.SelectedValue.ToString());
 
 
                Chart1.DataSource = dt; //dt is the data you need to chart
                Chart1.DataBind();
 
                //   Chart1.Series["Series1"].ChartArea = "Default";
                Series totals = Chart1.Series.Add("MonsterIBSSeries1");
                totals.ChartArea = "Default";
 
 
                totals.ValueMemberX = Request.QueryString["xval1"].ToString();
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
                totals.Type = SeriesChartType.Line;
                totals.Color = Color.Red;
                totals.BorderWidth = 3;
                
 
                Series totals2 = Chart1.Series.Add("MonsterIBSSeries2");
                totals2.ChartArea = "ChartArea1";
 
 
                //Preserve state of callback
                // Chart1.CallbackStateContent = CallbackStateContent.All;
 
                totals2.ValueMemberX = Request.QueryString["xval2"].ToString();
                totals2.ValueMembersY = Request.QueryString["yval2"].ToString();
              //  Chart1.ChartAreas["Default"].CursorX.UserEnabled = true;
               //Chart1.ChartAreas["ChartArea1"].CursorX.UserEnabled = true;
 
                Chart1.ChartAreas["Default"].AlignWithChartArea = "ChartArea1";
                Chart1.ChartAreas["Default"].AlignOrientation = AreaAlignOrientation.Vertical;
                Chart1.ChartAreas["Default"].AlignType = AreaAlignType.All;
            }
            else
            {
                Chart1.Visible = false;
 
 
 
                DataTable dt = CallDataTable1(Request.QueryString["xval1"].ToString(), Request.QueryString["yval1"].ToString(), CountryDropDownControl.SelectedValue.ToString());
 
                Chart2.DataSource = dt; //dt is the data you need to chart
                Chart2.DataBind();
 
 
                Series totals = Chart2.Series.Add("MonsterIBSSeries1");
                totals.ChartArea = "Default";
                
 
 
                totals.ValueMemberX = Request.QueryString["xval1"].ToString();
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
 
                
                totals.ValueMembersY = Request.QueryString["yval1"].ToString();
             }
 
        
        }
        else
        {
            Chart1.Visible = false;
            Chart2.Visible = false;
        }
    }
 
   
    public DataTable CallDataTable(string xval1, string yval1, string yval2, string country)
    {
 
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
        connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
        connection.Open();
 
        SqlCommand sqlCmd = new SqlCommand("SELECT [" + xval1 + "], [" + yval1 + "], [" + yval2 + "] FROM " + Request.QueryString["reportName"].ToString() + " WHERE Country = '" + 
           CountryDropDownControl.SelectedValue + "'", connection);
 
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
 
        return dt;
 
    }
 
    public DataTable CallDataTable1(string xval1, string yval1, string country)
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["Global KPIConnectionString"]);
        connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Global KPIConnectionString"].ConnectionString;
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT [" + xval1 + "], [" + yval1 + "] FROM [" + Request.QueryString["reportName"].ToString() + "] WHERE Country= '" + CountryDropDownControl.SelectedValue + "'", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
 
        return dt;
    }
  
}

Open in new window

This is the Postback code
   if (!IsPostBack && Request.QueryString["xval1"] == null)
 
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
                      CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));
 
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
 
            
        }

Open in new window

Does the control come up empty after the postback?  Its interesting how the countrydropdowncontrol works using the same code.  
Everything looks good.  Step through the code that populates the areadropdowncontrol and be sure that it returns data each time and the text and value fields are correctly set.
 
Sorry I meant this one!.
  if (!IsPostBack && Request.QueryString["xval1"] == null)
 
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
          
            CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));
 
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
 
            
        }

Open in new window

They look the same to me.  But Im assuming the postback code here is in the page_load event.
Step through the code and look for something fishy.  Your code looks good to me.  Im heading out for the day.  Good luck.
Yes it is in the page_load event!
Did you find anything after stepping through the code?  I dont see anything wrong with your code and Ive tested the method we are using and it works fine.  You should be able to bind, add an item, then select that index.  Also make sure you have Areas for the Country you choose.
Hmmm...one more thought.  If you select a country, the Area dropdown should change.  So the steps are like this:
1.  Load page with countries, preselecting "Choose One".
2.  Select a country.
3.  Area dropdown uses the country selection to fill the areas.
If this is correct, then you actually need to do the following.
1.  Double click (in design mode) on your Country dropdown.
2.  Take your AreaDropDownControl binding and put it in the method that is created from double clicking the Country dropdown.
3.  Be sure your AreaDropDownControl binding uses the country selected to get the correct data(areas.)
See the snippet for an example of this.
I think our problem is that since the areadropdownlist only populates during the initial loading of the page, and since it uses the preselected option of Choose One for country, it obviously has no areas for the country Choose One.
Make sense?  Use the snippet example to populate the area control every time a new country is selected.


private void CountryDropDownControl_SelectedIndexChanged(object sender, System.EventArgs e)
{
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
		
}

Open in new window

Almost there! Yea I did try that yesterday (and I'm glad you thought of that too!!). I did put the code you suggested in the  CountryDropDownControl_SelectedIndexChanged method.
It works fine until I click on an item in the DataList! Once I click on an item, dropdown values vanish showing two empty dropdowns! My guess this has something to do with the "&& Request.QueryString["xval1"] == null" section of the if (!IsPostback statement!
Yep, we talked about this in a previous comment.  Its not really a postback since the querystring changes.  So we just need to be sure that the country dropdown populates everytime the querystring changes.  
It appears that it works, its just the dropdowns are the issue now, right?
We need to do the following:
1.  Be sure to populate the country dropdown when the page is loaded (pre-selecting the value Choose One value on the initial page loading, and pre-selecting the selected country every other time.  (See #1snippet for this)
2.  Add an html hidden value on the page that stores the selected country dropdown.  Every time the country dropdown changes, this hidden saves the value. (See else statement in snippet #1 and selected change event in #2.)  I add an html hidden value on the page, the right click and run as server control.  Name it "hdnCountry".
If the area dropdown loses its value anywhere in the process, you may want to also do a selected index change event on this one that sets another hidden value as well.  But Im not sure you will need this.  Test what we have here and see if it works.
There is probably a cleaner way to do this, but it should work.  You can play around with the details and flow.  You may want to put the dropdown population stuff in their own method and then call it, etc.
By the way....I think we solved the original issue, dont you think?

#1:
//page load
  if (!IsPostBack && Request.QueryString["xval1"] == null)
 
        {
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
            CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
          
            CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));            
        }
else if(!IsPostBack && Request.QueryString["xval1"] != null){
            //this populates the country control and preselects the country they selected
 
            CountryDropDownControl.DataTextField= "ReportCountry";
            CountryDropDownControl.DataValueField = "ReportCountry";  
            CountryDropDownControl.DataSource = CountryDropDownList;
            CountryDropDownControl.DataBind();
 
CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue(Request.Form[hdnCountry.Value]));
 
           //now populate the area dropdown based on the country again.
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList; //be sure that this method is getting the latest country selected (if not, use the hidden value on your page to query the db.)
            AreaDropDownControl.DataBind();
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
}
 
 
private void CountryDropDownControl_SelectedIndexChanged(object sender, System.EventArgs e)
{
     hdnCountry.Value = CountryDropDownControl.SelectedValue;
 
            AreaDropDownControl.DataTextField = "Area";
            AreaDropDownControl.DataValueField = "Area";
            AreaDropDownControl.DataSource = AreaDropDownList;
            AreaDropDownControl.DataBind();
            AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
            AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
 
		
}

Open in new window

yes we are done with this question. But pls need an urgent answer to this since we are so close to finishing this. If I post another question, it might hard to get your attention. And believe me, I dont have any problems posting another question. Its just that this thing is due "yesterday" sorta thing and we are just about done here. Plus, you know this inside out :)
PLS PLS we are so close.... :)
I did exactly as you said. This is what happens:
1) I click on Country Drop Down (at this time the Area drop down is empty), and select the country.
2) The AreaDropDown populates, and then I select the Area
3) The DataList gets populated. I select the Report and the chart shows up! The values of the drop downs are set back to Belgium and National. Choose One options are gone. The labels that I had setup to show me the Country and Area Selected are empty
To clarify, the postback statements are in PageLoad. I debugged through code it seemed that the hdnCountry parameter isnt being set.

<asp:HiddenField ID="hdnCountry" runat="server" />

Open in new window

I guess instead of having a hidden value, I could have used the value set to the label as well. The problem is that, the state gets lost! Wow I had no idea setting the state could be this much of a pain!
It looks like you have an asp hidden field.  I prefer to use an html one.  It should look like this:   <INPUT id="hdnCountry" type="hidden" name="hdnCountry" runat="server">
Maybe the best way to set the hidden values is to do this in the page load:

CountryDropDownList.Attributes.Add("onchange","setVal(this.value)");
 
Then you need a javascript function called setVal(val).  Put this in your head tags.
<script type="text/javascript">
  function setVal(val)
  {
     
     aspnetForm.hdnCountry.value=val;
     
   
  }
</script>
 Ive tested this and it works.  You can do the same for area if you need to.  You can even have your labels use the hidden vals.
Another option instead of hidden vals is using session vars.  But lets try this first.  
Linq Error?
Error	1	'System.Xml.Linq.Extensions.Attributes(System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>, System.Xml.Linq.XName)' is a 'method', which is not valid in the given context
 
 
----------------------
 
protected void Page_Load(object sender, EventArgs e)
     {
         CountryDropDownList.Attributes.Add("onchange", "setVal(this.value)"); //THIS IS THE CODE YOU HAD GIVEN ME!
         
        if (!IsPostBack && Request.QueryString["xval1"] == null)
         {
             CountryDropDownControl.DataTextField = "ReportCountry";
             CountryDropDownControl.DataValueField = "ReportCountry";
             CountryDropDownControl.DataSource = CountryDropDownList;
             CountryDropDownControl.DataBind();
             CountryDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
             CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue("Choose One"));
 
         }
         else if (!IsPostBack && Request.QueryString["xval1"] != null)
         {
             //this populates the country control and preselects the country they selected
 
             CountryDropDownControl.DataTextField = "ReportCountry";
             CountryDropDownControl.DataValueField = "ReportCountry";
             CountryDropDownControl.DataSource = CountryDropDownList;
             CountryDropDownControl.DataBind();
 
             
            
             CountryDropDownControl.SelectedIndex = CountryDropDownControl.Items.IndexOf(CountryDropDownControl.Items.FindByValue(Request.Form[hdnCountry.Value]));
 
             //now populate the area dropdown based on the country again.
             AreaDropDownControl.DataTextField = "Area";
             AreaDropDownControl.DataValueField = "Area";
             AreaDropDownControl.DataSource = AreaDropDownList; //be sure that this method is getting the latest country selected (if not, use the hidden value on your page to query the db.)
             AreaDropDownControl.DataBind();
             AreaDropDownControl.Items.Add(new ListItem("Choose One", "Choose One"));
             AreaDropDownControl.SelectedIndex = AreaDropDownControl.Items.IndexOf(AreaDropDownControl.Items.FindByValue("Choose One"));
         }

Open in new window

Did you mean
CountryDropDownControl.Attributes.Add("onchange", "setVal(this.value)");
 
instead of CountryDropDownList?
Probably....  ;-)
Ok to make life much easier, cause I dont want to hog up anymore of your time. Here is the URL for the sample application.
http://docs.google.com/Doc?id=djk8gmz_55ccdfb8dj
Once you go there, you'll see the URL. Click on that and play around. You'll get the feel of how things are. Oh and btw, the credit for all this goes to you :)
CountryDropDownList is a datasource and CountryDropDownControl is the drop down menu itself. CountryDropDownList doesnt have an Attributes method but the CountryDropDownControl does.
Anyways long story short - that didnt help either. The hidden field isnt catching the Value clicked in the country I think!
Try this.  Put an alert in the javascript to see what is happening.
 function setVal(val)
  {
     
    alert(val);
    aspnetForm.hdnCountry.value=val;
     
 
  }  
 
Also, make sure the countrydown control is set to autopostback.
Aah Screw it! I'll use the Request.QueryString...always works! Just added an extra &country. Just need to make sure it works! And I think we're done!
Cant thank you enough. Learned a lot dude - hell Request.QueryString is definitely a useful thing to learn! I'll award you points. Maybe I'll have a few other question on dundas charts in the near future. I'll post a link for the question here. Hopefully it wont be too long as this one.
Thank you once again!
ASKER CERTIFIED SOLUTION
Avatar of clintfield
clintfield
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thankyou for all your help!!!! i'll come back for anything else. I'll probably post the link to anything else I might need help with over here.

Thank you :)
Here's the URl for your interest:
 http://docs.google.com/Doc?id=djk8gmz_56c2pxjccf
its basically data to be used internally for the company. Browse around and enjoy while its up there. :)
I have a quick question :). How do I add the Choose One item to either appear on top of the drop down, OR remove it completly whenever the user clicks on the arrow next to the drop down!
 
I think the way we did it, Choose One will always be the last item in the list, even if it appears pre selected.  I do have a class file that I created which allows you to compile it and use it as a dropdownlist in your apps.  It allows you to set a property for default selected value and text.  It also allows you to set colors in the dropdown, etc.  I can post it if you know how to create a project in which you can compile it and use it your apps.