Hey everyone,

I'm building a website and I have this problem. Let me explain the idea.

I have a masterpage divided in a header, 2 sidebars and in the middle a contentpage.

On de left sidebar I have 10 images The idea is to give a sort of "state" to these images. Each of these exist in color and in black-and-white. By default it's the black-and-white images that are shown.

Beside the masterpage I created an administration page, to make some changes to the website without making changes in the source code.

The idea is to be able to change the state of those 10 images by checking or unchecking checkboxes. If a checkbox is checked, the url of the image that is associated with that checkbox, changes to the coulor one, if unchecked the black-and-white.

Now that you more or less understood the idea, I'll show you what I have so far.

if (Application["Chaman"] != null)
        {
            if (Application["Chaman"].ToString() == "1")
                Image15.ImageUrl = "~/img/recrutement/recrutement_chaman.jpg";
            else
                Image15.ImageUrl = "~/img/recrutement/recrutement_chaman_grayscale.jpg";
        }

        if (Application["Chasseur"] != null)
        {
            if (Application["Chasseur"].ToString() == "1")
                Image16.ImageUrl = "~/img/recrutement/recrutement_chasseur.jpg";
            else
                Image16.ImageUrl = "~/img/recrutement/recrutement_chasseur_grayscale.jpg";
        }

        if (Application["DeathKnight"] != null)
        {
            if (Application["DeathKnight"].ToString() == "1")
                Image17.ImageUrl = "~/img/recrutement/recrutement_deathKnight.jpg";
            else
                Image17.ImageUrl = "~/img/recrutement/recrutement_deathKnight_grayscale.jpg";
        }

        if (Application["Demoniste"] != null)
        {
            if (Application["Demoniste"].ToString() == "1")
                Image18.ImageUrl = "~/img/recrutement/recrutement_demoniste.jpg";
            else
                Image18.ImageUrl = "~/img/recrutement/recrutement_demoniste_grayscale.jpg";
        }

        if (Application["Druide"] != null)
        {
            if (Application["Druide"].ToString() == "1")
                Image19.ImageUrl = "~/img/recrutement/recrutement_druide.jpg";
            else
                Image19.ImageUrl = "~/img/recrutement/recrutement_druide_grayscale.jpg";
        }

        if (Application["Guerrier"] != null)
        {
            if (Application["Guerrier"].ToString() == "1")
                Image20.ImageUrl = "~/img/recrutement/recrutement_guerrier.jpg";
            else
                Image20.ImageUrl = "~/img/recrutement/recrutement_guerrier_grayscale.jpg";
        }

        if (Application["Mage"] != null)
        {
            if (Application["Mage"].ToString() == "1")
                Image21.ImageUrl = "~/img/recrutement/recrutement_mage.jpg";
            else
                Image21.ImageUrl = "~/img/recrutement/recrutement_mage_grayscale.jpg";
        }

        if (Application["Paladin"] != null)
        {
            if (Application["Paladin"].ToString() == "1")
                Image22.ImageUrl = "~/img/recrutement/recrutement_Paladin.jpg";
            else
                Image22.ImageUrl = "~/img/recrutement/recrutement_paladin_grayscale.jpg";
        }

        if (Application["Pretre"] != null)
        {
            if (Application["Pretre"].ToString() == "1")
                Image23.ImageUrl = "~/img/recrutement/recrutement_pretre.jpg";
            else
                Image23.ImageUrl = "~/img/recrutement/recrutement_pretre_grayscale.jpg";
        }

        if (Application["Voleur"] != null)
        {
            if (Application["Voleur"].ToString() == "1")
                Image24.ImageUrl = "~/img/recrutement/recrutement_voleur.jpg";
            else
                Image24.ImageUrl = "~/img/recrutement/recrutement_voleur_grayscale.jpg";
        }
    }

This is in the page_load of the masterpage.
So if the variables don't exist in the application state, the black-and-white images are shown. if they do exist, the colour ones are shown.

On my administration page:

If on a previous visit I allready checked some checkboxes, I'd like to see those checkboxes still checked on every visit, so that I can uncheck them to change the state of the images.

for that I wrote the following:

if (Application["Chaman"] != null)
        {
            if (Application["Chaman"] == "1")
                CheckBoxList1.Items[0].Selected = true;
            else
                CheckBoxList1.Items[0].Selected = false;
        }

        if (Application["Chasseur"] != null)
        {
            if (Application["Chasseur"] == "1")
                CheckBoxList1.Items[1].Selected = true;
            else
                CheckBoxList1.Items[1].Selected = false;
        }

        if (Application["DeathKnight"] != null)
        {
            if (Application["DeathKnight"] == "1")
                CheckBoxList1.Items[2].Selected = true;
            else
                CheckBoxList1.Items[2].Selected = false;
        }

        if (Application["Demoniste"] != null)
        {
            if (Application["Demoniste"] == "1")
                CheckBoxList1.Items[3].Selected = true;
            else
                CheckBoxList1.Items[3].Selected = false;
        }

        if (Application["Druide"] != null)
        {
            if (Application["Druide"] == "1")
                CheckBoxList1.Items[4].Selected = true;
            else
                CheckBoxList1.Items[4].Selected = false;
        }

        if (Application["Guerrier"] != null)
        {
            if (Application["Guerrier"] == "1")
                CheckBoxList1.Items[5].Selected = true;
            else
                CheckBoxList1.Items[5].Selected = false;
        }

        if (Application["Mage"] != null)
        {
            if (Application["Mage"] == "1")
                CheckBoxList1.Items[6].Selected = true;
            else
                CheckBoxList1.Items[6].Selected = false;
        }

        if (Application["Paladin"] != null)
        {
            if (Application["Paladin"] == "1")
                CheckBoxList1.Items[7].Selected = true;
            else
                CheckBoxList1.Items[7].Selected = false;
        }

        if (Application["Pretre"] != null)
        {
            if (Application["Pretre"] == "1")
                CheckBoxList1.Items[8].Selected = true;
            else
                CheckBoxList1.Items[8].Selected = false;
        }

        if (Application["Voleur"] != null)
        {
            if (Application["Voleur"] == "1")
                CheckBoxList1.Items[9].Selected = true;
            else
                CheckBoxList1.Items[9].Selected = false;
        }

This is written in the page_load of the administration page. same principle: if it appears in the application state and it has the value 1, then check the checkbox. if not, uncheck it.

I wrote the following method to validate my choice (on the administration page)

protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            string myclass = CheckBoxList1.Items[i].Value;

            if (CheckBoxList1.Items[i].Selected == true)
            {
                Application[myclass] = "1";
                ListBox1.Items.Add(myclass + " = " + Application[myclass].ToString());
            }

            else
                Application[myclass] = "0";
        }
    }

What is working?

The very first time I visit the index page, the images on the left are in black-and-white, so that's working. I click on the administration page, I check some checkboxes, I click the button. Nothing changes. That's normal, the masterpage has to reload (any way I can do this from the contentpage?). So I go to the indexpage, the masterpage reloads and the correct colour images show.

What is not working?

The reverse action. So when I close the website, I reopen it, I go to the administration page, I see the correct checkboxes checked. But when I uncheck them and I click the button, the checkboxes are again checked. If you look back at the page_load of the administration page, I check the checkboxes if the variables in the application state have 1 as value. I'm guessing one way or the other, the page ignores the action by the button and just executes what's written in the page_load.

If someone understood what I just wrote and knows a solution, I'd be very happy to have it :)

I can't believe I wrote all that to find the solution 3 hours after just by looking for something else.

I just added

    if (!this.IsPostBack)
    {

in the administration.aspx.cs file and it works. Now the problem is that when I click the button to send my choice of the checked checkboxes, it doesn't update my masterpage directly. I have to go to another page to update the images on the masterpage. Is there any way to refresh a masterpage from a contentpage?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.