简体   繁体   中英

disable postback from button in asp.net

Hallo i have a school task to build a game called "The Hang Men" , i started to build this game but there is one big issue that i cant Solve it

every time that i click on the button. all the elements disappear from the page. i know the reason for that, its becase of the postback.

but if i disable from the button the postback , no thing happens.

any ideas how can i go it? how can i click a button and do some event without creating a postback?

this is the code..

abc ltr;

Words word = null;

static Label [] lbl = null;

static Button[] btn = null;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SetBord();
        SetWords();
    }

}

private void SetWords()
{
    word = new Words();
    Random ids = new Random();
    string temp_word;
    int id = ids.Next(0, word.GetListSize());
    temp_word = word.GetWords(id);

    lbl = new Label[word.GetWordLenght(id)];

    for (int i = 0; i < word.GetWordLenght(id); i++)
    {
        lbl[i] = new Label();
        lbl[i].ID ="lbl"+i;
        lbl[i].CssClass = "LetterStyle";
        lbl[i].Text = Convert.ToString(word.get_chars(id,i));
        this.Words_Panel.Controls.Add(lbl[i]);
    }


}
private void SetBord()
{
    ltr = new abc();

    btn = new Button[27];
    for (int i = 0; i<27; i++)
    {
      btn[i] = new Button();  
      btn[i].ID = "btn" + i;
      btn[i].Attributes.Add("runat", "server");
      btn[i].Click += new EventHandler(LetterClicked);
      btn[i].Width = new Unit("100px");
      btn[i].Height = new Unit("49px");
      btn[i].Text = "" + ltr.getLetters(i); 
      this.Letters.Controls.Add(btn[i]);
    }


}

protected void LetterClicked(object sender, EventArgs e)
{
    Button letter = (Button)sender;
    letter.Enabled = false;
    lbl[0].Text = letter.Text;
}   

What you have encountered is a phenomenon when you do dynamic control creation. Since you are creating the buttons & labels dynamically in the code, these controls are not "persisted" (remembered) when you your page reloads (they are lost each time the page is reloaded/post-back).

If there is no other way to go about the problem other than dynamically generating them; then you will have to do it like how everyone handles dynamic control creation.

Here's some pointers:

  • When you create a control, you need to "remember" the controls that you create (viewstate? session?).
  • When a page reload/refresh, at the Page_Load event, you will need to recreate all those controls before you can process them. This is especially true if you want to subsequently process events/values from those controls. If they are not recreated, whatever values you set on them will not be "persisted" by IIS (because IIS cannot find the controls, so the submitted values are discarded).

You will need to structure your design/program for the above. Good luck!

add Page_Init event

protected void Page_Init(object sender, EventArgs e)
{

        SetBord();
        SetWords();
}

Well you can't make your game work if there is no postback, or you'll have to do everything in javascript (but I don't think it's the purpose of making this game).

Since you create your controls dynamically, you need to recreate them on every postback (just remove the condition in your Page_Load function)

Also you should persist your game data from one postback to another

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM