0

I am using visual studio 12, coding in asp.net using c#.
I have 3 dropdownlists in my code, all of which are bound by lists that I created.
I need some advise as to which method is better to call the postbackvalues of ddl's to perform a task.

Option 1
When a user selects an item from drop down list 3, the postbackvalue is sent from Dropdownlist3_SelectedIndexChanged to the dropdownlist2_selectedindexchanged by calling the method. Only after I have both the postbackvalues I would like to produce a chart. Regardless of what the chart holds and regardless of what the data is in the drop down list.

So something like

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
 // I would like to have the postbackvalue of drop down list 3 here so i can use its value and dropdownlist2's postbackvalue to produce a chart.
}

and in the dropdownlist3

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to call DropDownlist2_SelectedIndexChanged(...) method so I can send the postbackvalue of DDL3 for use in DDL2.
}

Option 2:
Define a global variable that stores the postbackvalue of Dropdownlist3 and use that value in Dropdownlist2_SelectedIndexChanged method for further use such as produces a chart.

I have read a lot about global variables but do not understand the con's about them.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Timetraveller
  • 304
  • 5
  • 18

1 Answers1

1

I am not sure if this is what you are after, but perhaps having a third method which is called that handles the updating of the chart...

for example

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    BuildChart();
}

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
    BuildChart();
}

private BuildChart()
{
    var ddl3Value = DropDownList3.SelectedValue;
    var ddl2Value = DropDownList2.SelectedValue;

    if(ddl3Value != null && ddl2Value != null)
    {
        //build chart.
    }
}
Squirrel5853
  • 2,376
  • 1
  • 18
  • 34
  • Please give me some time, i will get back to you as soon as I test it on my solution. By the looks off it, it would work! Do you have any knowledge about global variables? – Timetraveller Dec 18 '13 at 12:35
  • in `c#` there are no `global variables` read [this](http://stackoverflow.com/questions/14368129/c-sharp-global-variables) – Squirrel5853 Dec 18 '13 at 12:50
  • [here](http://stackoverflow.com/questions/2445436/global-variables-in-c-net) is some more help regarding how to use a static class which could be used like a "global variable" – Squirrel5853 Dec 18 '13 at 12:53