Domanda

I'm making a game with 6 dynamic buttons as "btn" at the top row and other 6 "lamp buttons" on a buttom row. Player clicks on a top row button and text displayed in a bottom row button after clicking on it. As soon as all the lamp buttons are full with text, I want a submit buttom to appear.

I tried to make for and foreach for my lamp buttons and it didn't help. Plese help!

here is a code:

public partial class Game : System.Web.UI.Page
{

    protected void Page_Init(object sender, EventArgs e)
    {
        for (int i = 0; i < 6; i++)
        {
            Button btnLamp = new Button();
            btnLamp.ID = "btnLamp" + i.ToString();
            btnLamp.Click += btnLamp_Click;
            this.Panel1.Controls.Add(btnLamp);
        }

        LiteralControl ltBreak = new LiteralControl();
        ltBreak.Text = "<br/><br/>";
        Panel1.Controls.Add(ltBreak);
        Panel1.DataBind();

        for (int i = 0; i < 6; i++)
        {
            Button btn = new Button();
            btn.ID = "btn" + i.ToString();
            btn.Text = "btn" + i.ToString();
            btn.Click += btn_Click;
            this.Panel2.Controls.Add(btn);
        }
        Panel2.DataBind();
    }        

   void btn_Click(object sender, EventArgs e)
    {
        Button clickedbutton = (Button)sender;
        string btn_cliked = clickedbutton.ID;


        for (int i = 0; i <5 ; i++)
        {
            ((Button)FindControl(("btn" + i.ToString()))).BackColor = System.Drawing.Color.LightSteelBlue;
        }
        clickedbutton.BackColor = System.Drawing.Color.Beige;

        Session["clickedbutton"] = clickedbutton;
    }        

    void btnLamp_Click(object sender, EventArgs e)
    {
        Button clickedbutton = (Button)sender;
        string btnLamp_cliked = clickedbutton.ID;


        ((Button)FindControl(((Button)Session["clickedbutton"]).ID)).Enabled = false;


        for (int i = 0; i < 5; i++)
        {
            if (((Button)Session["clickedbutton"]).Text.ToString() ==   ((Button)FindControl("btnLamp" + i)).Text)
            {
                ((Button)FindControl("btnLamp" + i)).Text = "";
            }
        }


        clickedbutton.Text = ((Button)Session["clickedbutton"]).Text.ToString();

    }
È stato utile?

Soluzione

This sort of stuff is best handled client side in javascript. Attach a handler to the onchange event of your inputs and figure out inside it if all inputs have value. Then show your button on page (should be present but hidden - display:none).

If none of the above makes any sense i suggest to do some research on web programming. Figure out the purpose of server code (C# in your case) versus html and javascript. Then come back with questions if needed

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top