[RESOLVED] Checkbox_Changed event

Hi All,

So if I have a Checkbox_Changed event with some code in it and a few variables etc, how can I then reference these variables in the main part of my code? I don't seem to be able to reference these atm.

Below is a example of my code

protected void Button1_Click(object sender, EventArgs e)
        {
            string newStartDay = SDay.Text;                                             //All variables matched up to controls on the form.
            string newStartMonth = SMonth.Text;
            string newStartYear = SYear.Text;
            string newCompleteStartDate = "";
            string newEndDay = EDay.Text;
            string newEndMonth = EMonth.Text;
            string newEndYear = EYear.Text;
            string newCompleteEndDate = "";
            DateTime startdt = DateTime.MinValue;
            DateTime enddt = DateTime.MinValue;
            

            if ((Convert.ToInt32(newStartDay)) != 0 | newStartMonth != "Select" | (Convert.ToInt32(newStartYear)) != 0)     //picks up the values in the start date options and makes them in a string
            {
                newCompleteStartDate = newStartDay + " " + newStartMonth + " " + newStartYear;
                startdt = Convert.ToDateTime(newCompleteStartDate);
            }


                 

            

         

                        

            //string queryValuesRedirect = "WebResponsePage.aspx?";
            //queryValuesRedirect += "QueryValues=" + startdt + " " + enddt + " " + TextBoxValue + " " + newCountryEF;
            //Response.Redirect(queryValuesRedirect);
            

        }




protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            int i = 0;
            string[] Array = new string[100000000];
            foreach (ListItem item in Country.Items)
            {
                item.Selected = true;
                Array[i] = item.ToString();
                i = i + 1;
            }


        }


So say I want to reference the Array in Checkbox1_CheckedChanged under the button click how do I do this? I tried changing it from "Protected to Public" and that didnt help.

many thanks

For arrays - You can use Session Arrays described here..

http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=63

for variables - to use across functions, you can use straight forward session variables.

Hello there!

I not sure I follow what you trying to do. But in order to access variables on different parts of code you need to declare them as Class variables.

You cannot access your Array because of the scope in which is declared. Declare it on the class level and intantiate it on the checkbox1_checkedChanged event then you will be able to access it outside. 

Hi, as you've defined your variables within the control events they stay local and are only accessable within those events. To be able to use the variables within all control events set up your variables at the page class level. Also consider using properties.

See: http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx#Y84


thoughts on "[RESOLVED] Checkbox_Changed event"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>