Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 22nd, 2004, 05:11 PM
Authorized User
 
Join Date: Nov 2003
Location: Mataro, Barcelona, Spain.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default force Page_Load

Hello,

I have a webform with a button. In Page_Load I create an HtmlTable, but I have to press two times the button to execute the code writed in Page_Load.

if (Page.IsPostBack)
   <create table>

Anybody can tell me the way to ensure the execution of Page_load when I press the button?

Thank you

-------------------------------
World only exists into your mind.
  #2 (permalink)  
Old January 22nd, 2004, 08:55 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Page_load is executing every time, but you are drawing the table only when the page is posted back. Remove the if test.
  #3 (permalink)  
Old January 23rd, 2004, 05:06 AM
Authorized User
 
Join Date: Nov 2003
Location: Mataro, Barcelona, Spain.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did it. The problem persist the same. In the code bellow I only see "page_load" on the label lblMensajeLargo the first time I load the page (I see all the other times "button"), and the table only is repainted when I press the button two times:

namespace p2.privada
{
    /// <summary>
    /// Descripción breve de Buscaminas.
    /// </summary>
    public class Buscaminas : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.LinkButton lnkInicial;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.Button btnPartida;
        protected System.Web.UI.WebControls.Label lblMensaje;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.HtmlControls.HtmlInputText txtFilas;
        protected System.Web.UI.HtmlControls.HtmlInputText txtColumnas;
        protected System.Web.UI.HtmlControls.HtmlInputText txtMinas;
        private static int nFilas, nColumnas, nMinas, nArea;
        protected System.Web.UI.WebControls.Label lblMensajeLargo;
        protected System.Web.UI.WebControls.Panel Panel1;
        protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
        protected System.Web.UI.HtmlControls.HtmlTable Table2;

        private void Page_Load(object sender, System.EventArgs e)
        {
             nFilas = nColumnas = nMinas = 0;
            //EnableViewState = true;
            lblMensajeLargo.Text = "page_load";
/* if (!Page.IsPostBack)
            {
                lblMensaje.Text = "Partida no iniciada";
                lblMensajeLargo.Text = "!ispostback";
            }
            else
            {*/
                lblMensaje.Text = null;
                if (ViewState["_filas"] != null)
                {
                    nFilas = (int)ViewState["_filas"];
                    nColumnas = (int)ViewState["_columnas"];
                    nMinas = (int)ViewState["_minas"];
                    Panel1.Height = nColumnas * 15 + 2;
                    int row = 0;
                    for (int j=0; j<nFilas; j++)
                    {
                        HtmlTableRow r = new HtmlTableRow();
                        r.BgColor="Gainsboro";
                        r.Height = "15";
                        row++;
                        for (int i=0; i<nColumnas; i++)
                        {
                            HtmlTableCell c = new HtmlTableCell();
                            /*c.Controls.Add(new LiteralControl("row " + j.ToString() +
                                ", cell " + i.ToString()));*/
                            c.Height = "15";
                            r.Cells.Add(c);
                        }
                        Table2.Rows.Add(r);
                        Table2.Align = "Center";
                    }
                    Table2.Width = Convert.ToString(nColumnas*15);
                }
            //}
        }

        #region Código generado por el Diseñador de Web Forms
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: llamada requerida por el Diseñador de Web Forms ASP.NET.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Método necesario para admitir el Diseñador. No se puede modificar
        /// el contenido del método con el editor de código.
        /// </summary>
        private void InitializeComponent()
        {
            this.lnkInicial.Click += new System.EventHandler(this.lnkInicial_Click);
            this.Submit1.ServerClick += new System.EventHandler(this.Submit1_ServerClick);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void lnkInicial_Click(object sender, System.EventArgs e)
        {
            // TODO: Comprobar la autentificación en todos los RedirectFromLoginPage
            FormsAuthentication.RedirectFromLoginPage(User.Ide ntity.Name, false);
        }

        private void Submit1_ServerClick(object sender, System.EventArgs e)
        {
            lblMensajeLargo.Text = "button";
            try
            {
                nFilas = System.Convert.ToInt32(txtFilas.Value);
                nColumnas = System.Convert.ToInt32(txtColumnas.Value);
                nMinas = System.Convert.ToInt32(txtMinas.Value);
            }
            catch (System.FormatException)
            {
                lblMensaje.Text = "¡Error, debe introducir valores numéricos!";
                return;
            }
            nArea = nFilas * nColumnas;
            if ( (nArea < 16) || (nArea > 100))
            {
                lblMensajeLargo.Text = "¡Error, el área del tablero debe estar comprendida entre 16 y 100 casillas!";
                return;
            }
            if (nMinas > nArea * .90)
            {
                lblMensajeLargo.Text = "¡Error, el número de minas no puede ser superior al 90% de casillas!";
                return;
            }
            ViewState["_filas"] = nFilas;
            ViewState["_columnas"] = nColumnas;
            ViewState["_minas"] = nMinas;
        }
    }
}

-------------------------------
World only exists into your mind.
  #4 (permalink)  
Old January 23rd, 2004, 05:17 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's because you're setting the Text of lblMensajeLargo to Button in the Submit1_ServerClick event.

Here's what happens:
1. Page_Load will fire first, setting the button to page_load
2. You click the button and the page posts back
3. Page_Load fires, setting the button to page_load
4. Immediately after that (on the same PostBack), Submit1_ServerClick fires setting the Text of the lblMensajeLargo to Button.

That's why you don't see page_load on the button anymore.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #5 (permalink)  
Old January 23rd, 2004, 05:19 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

P.S. That's also the reason why you don't see the Table the first time.

Page_Load fires before the button click, so the first time, ViewState["_filas"] is still null.


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #6 (permalink)  
Old January 23rd, 2004, 05:31 AM
Authorized User
 
Join Date: Nov 2003
Location: Mataro, Barcelona, Spain.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, you're right. But what I need is to repaint the table every time I press the button. I don't want to paint the table the first time the page is executed. I need to repaint it every time the user press the button, and now the user has to press two times to repaint the table.

-------------------------------
World only exists into your mind.
  #7 (permalink)  
Old January 23rd, 2004, 05:36 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, that's possible. Simply move the code that draws the table to its own method, and call it from the Submit1_ServerClick method:
Code:
private void DrawTable()
// Check ViewState stuff here
// The draw the table
In the Submit1_ServerClick method, you just need to call this method:
Code:
private void Submit1_ServerClick(object sender, System.EventArgs e)
{
  // Do what you need to do here.
  // Then call the DrawTable method:
  // DrawTable();
}
Does this help?

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #8 (permalink)  
Old January 23rd, 2004, 06:04 AM
Authorized User
 
Join Date: Nov 2003
Location: Mataro, Barcelona, Spain.
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Humm... it works better, but, when I press <enter> after entering the values, page_load is executed, and the table is removed... brrr

Thank you... Possibly, this is the way to do it

-------------------------------
World only exists into your mind.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page_Load question wayneamurphy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 4 November 6th, 2006 03:54 AM
page_load() hassel vijayapteET ASP.NET 1.0 and 1.1 Basics 2 October 6th, 2005 01:30 PM
Page_Load not working! New2ASPnet General .NET 6 July 28th, 2004 01:53 PM
forcing a page_load ? Rashe ASP.NET 1.0 and 1.1 Basics 2 February 16th, 2004 10:53 AM



All times are GMT -4. The time now is 04:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.