On click first enter the method and then go to another page
<p>I have an image and a button. I have a method which will generate an image randomly from a set of images. For some reason as I click on the button apart from doing the event, it is also entering again in the method 'chooseImage' of the Image and generate another id.</p>

<p>Below here you can find the asp of the image and the image Button;</p>

<pre><code>&lt;asp:Image ID="Image1" runat="server" Height="300px" Width="200px" OnDataBinding="chooseImage" /&gt;


&lt;asp:ImageButton ID="aboutBtn" src="Resources/Icons/HomeAboutMeBtn.png" OnClick="AboutBtn_Click" runat="server" /&gt;
</code></pre>

<p>Below here I also posted the event handler of the button, where the childId is being generated from the 'chooseImge' method and stored in a global variable.</p>

<pre><code>protected void AboutBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ChildDescription.aspx?id=" + childId);
        }
</code></pre>

<p>Any suggestions why or how can I do it? Basically I would like to pass as a session the id of the image being displayed on load page. </p>

<p>Below here you can find the method. Basically first I am reading from database a set of records. Then I am storing the id in array, and then by calling a random generator method it will randomly choose an id from the array.</p>

<pre><code>protected void chooseImage(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(conString))
            {
                try
                {
                    connection.Open();

                    using (SqlCommand cmd = new SqlCommand("GetAllChildren", connection))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                array.Add(reader["Child_ID"].ToString());
                                counter++;
                            }

                      connection.Close();
                        }
                    }
                }
                catch (Exception) { }

                if (array.Count != 0)
                {
                    try
                    {
                        using (SqlCommand command = new SqlCommand("GetChildDetails", connection))
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            childId = Convert.ToInt32(array[RandomNumber(1, counter - 1)]);
                            command.Parameters.Add("@ChildID", SqlDbType.Int).Value = childId;
                            connection.Open();

                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    Image1.ImageUrl = "Resources/Children Images/" + reader["Primary_Image"].ToString();
                                    ChildName.Text = reader["Child_Name"].ToString();
                                    ChildName.Font.Size = 20;
                                    surname.Text = reader["Child_Surname"].ToString();
                                    surname.Font.Size = 20;
                                    place.Text = reader["Location"].ToString();
                                    place.Font.Size = 14;
                                    genderLabel.Text = reader["Gender"].ToString();
                                    genderLabel.Font.Size = 14;
                                    DateTime birthday = DateTime.Parse(reader["Birthday"].ToString());
                                    DateTime now = DateTime.Today;
                                    int age = now.Year - birthday.Year;

                                    if (birthday &gt; now.AddYears(-age))
                                    {
                                        age--;
                                    }

                                    Years.Text = age.ToString() + "-years-old";
                                    Years.Font.Size = 14;
                                }

                             connection.Close();
                            }
                        }
                    }
                    catch (Exception) { }

                }
            }
        }

        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
</code></pre>
