Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

processing dynamic controls

Posted on 2004-04-26
16
Medium Priority
?
175 Views
Last Modified: 2012-05-04
Hi,

I am facing this problem, which is about processing dynamic controls.
I created a method that declare 2-D array of TextBox and insert them in ASPX page using C# (for Crossword Grid).

the problem is that when i want to process these TextBoxs it gives me an error saying:
"Object reference not set to an instance of an object."

My problem happens when i try to click on Button2 (Click_Button2)

I tried many ways to solve this problem but i could not. So, I need your help in it.

This is my source code:


      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.TextBox TextBox1;
            HtmlForm Form;
            
            TextBox[][] tb; // this is my TextBoxes
            
            
            public void create_grid(int size)
            {
                  tb = new TextBox[size+1][];
                  
                  for(int i = 0;i<tb.Length;i++)
                        tb[i] = new TextBox[size+1];
                        
                  for(int i=0;i<tb.Length;i++)
                        for(int j=0;j<tb.Length;j++)
                              tb[i][j] = new TextBox();

                  for (int i = 1; i<tb.Length; i++)
                        for(int j=1;j<tb.Length;j++)
                        {
                              int left = 25+((j-1)*25);
                              int top = 25+((i-1)*25);
                              tb[i][j].Style.Add("POSITION","absolute");
                              tb[i][j].Style.Add("HEIGHT","25");
                              tb[i][j].Style.Add("WIDTH","25");
                              tb[i][j].Style.Add("LEFT",left.ToString());
                              tb[i][j].Style.Add("TOP",top.ToString());
                              tb[i][j].ID = "textBoxddf"+i+j;
            

//////////////////////////////////////  Adding them to the Form ///////////////////////////                                              Form.Controls.Add(tb[i][j]);

                              
                              
                        }

                  tb[1][1].Text = "T";
                  tb[2][2].Style.Add("Background","Black");
      
                  tb[2][2].ReadOnly = true;
                  if(tb[1][1].Text == "T")
                        tb[3][3].Text = "F";
                  

            }
            private void Page_Load(object sender, System.EventArgs e)
            {
                  
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
                  Form = ( System.Web.UI.HtmlControls.HtmlForm )this.FindControl( "Form1" );
                  

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  create_grid(int.Parse(TextBox1.Text));
            }

            private void Button2_Click(object sender, System.EventArgs e)
            {
                  
                  if(tb[3][3].Text.Equals("3")) tb[3][1].Text = "T";
                  else tb[3][1].Text = "F";
            }
      }
}
0
Comment
Question by:shams_sa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
16 Comments
 
LVL 1

Expert Comment

by:warmach
ID: 10920439
Your TextBox[][] tb variable is not being saved each time the page loads.  This is the way ASP.Net is designed to operate.  Each time the page loads, or posts back, all the variable are initialized and then the UI controls(text box, check box, etc) are then populated with the information recieved from the post back.  The server variables are not saved at all.  

By pressing Button1, the server creates the text box matrix.  When you press Button2, the server doesn't know if you already created the text box matrix.  That is the nature of the internet, stateless.  

Your best bet would be to rebuild the matrix on postback.  I know that is going to suck to do but that is really the only way you can preserve the state of the page from call to call.

I hope some other people have a better idea...
0
 

Author Comment

by:shams_sa
ID: 10920556
Sorry but i'm new in ASP.Net and did not understand your solution. In other words, How to build a matrix on postback ?
0
 
LVL 1

Expert Comment

by:warmach
ID: 10920662
Basically, the server does not remember who you are (client browser).  It does not know what it sent you the last time you requested a page or clicked a button...  That is why you are getting the error.  You are trying to use the textbox grid, but the server doesn't remember if you created one.

If you wish, you could save the tb grid in a session variable.  Then when you press Button2, manipulate the grid in the Session variable.  At the end of the code to Button2, call create_grid again to show the new grid....

 public void create_grid(int size)
{
     //Look in session variable for an existing textbox grid
     tb = Session("TextBoxGrid");

     if (tb == null){
         //The grid was not in the Session variable...create a new one
          tb = new TextBox[size+1][];
     }


     ....all your previous code....

     Session("TextBoxGrid") = tb
}


 private void Button2_Click(object sender, System.EventArgs e)
          {
               
               if(tb[3][3].Text.Equals("3")) tb[3][1].Text = "T";
               else tb[3][1].Text = "F";

               create_grid(TextBox1.Text);
          }



Hope this helps....let me know if you need more clarification
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:shams_sa
ID: 10921022
I'm sorry to tell you that this is the first Time i heard of "Session". I tried your code and i faced this problem 'System.Web.UI.Page.Session' denotes a 'property' where a 'method' was expected

Also Can you give Sites where can i found informations about "Session"

I also want to Thank you for your help
0
 

Author Comment

by:shams_sa
ID: 10921188
I fix the problem, but it did not work...... :(
the problem was the session["   "] not ("   ")
Also when assign the session back to the TextBox tb I Down Cast the session to (TextBox[][])

if i did something wrong, pls tell me.
0
 
LVL 1

Expert Comment

by:warmach
ID: 10921276
Please post the code you tried and tell me exactly what is going wrong.
0
 

Author Comment

by:shams_sa
ID: 10921822
This is my code as you requested, but before that i want to ask is there a way that i can save the session thru a submit Button. I tried it but I could not do it. If i can store the session when i click on a button, my problem will be solved Hopefully.

My purpose is to store the Grid and Compare it with A Char[][] Grid with the same size When i click on the Button.

This is my Code:

public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.TextBox GSize;
      
            HtmlForm Form;
            protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.Button Button3;
         
            TextBox[][] tb; // this is my TextBoxes
         
         
            public void create_grid(int size)
            {
                  
                  tb = (TextBox[][]) Session["TextBoxGrid"];

                  if (tb == null)
                  {
                        //The grid was not in the Session variable...create a new one
                        
                        tb = new TextBox[size+1][];
               
                        for(int i = 0;i<tb.Length;i++)
                              tb[i] = new TextBox[size+1];
                   
                        for(int i=0;i<tb.Length;i++)
                              for(int j=0;j<tb.Length;j++)
                                    tb[i][j] = new TextBox();
                  }

                  for (int i = 1; i<tb.Length; i++)
                        for(int j=1;j<tb.Length;j++)
                        {
                              int left = 25+((j-1)*25);
                              int top = 25+((i-1)*25);
                              tb[i][j].Style.Add("POSITION","absolute");
                              tb[i][j].Style.Add("HEIGHT","25");
                              tb[i][j].Style.Add("WIDTH","25");
                              tb[i][j].Style.Add("LEFT",left.ToString());
                              tb[i][j].Style.Add("TOP",top.ToString());
                              tb[i][j].ID = "textBoxddf"+i+j;
         

                              //////////////////////////////////////  Adding them to the Form ///////////////////////////                                        
                              Form.Controls.Add(tb[i][j]);

                         
                         
                        }
                  //            

                  tb[1][1].Text = "T";
                  tb[2][2].Style.Add("Background","Black");
     
                  tb[2][2].ReadOnly = true;
                  if(tb[1][1].Text == "T")
                        tb[3][3].Text = "F";

                  Session["TextBoxGrid"] = tb;
               

            }
            
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.Button3.Click += new System.EventHandler(this.Button3_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
                  Form = (System.Web.UI.HtmlControls.HtmlForm)this.FindControl("Form1");

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  create_grid(int.Parse(GSize.Text));
            }

            private void Button2_Click(object sender, System.EventArgs e)
            {
                  if(tb[3][3].Text.Equals("3")) tb[3][1].Text = "T";
                  else tb[3][1].Text = "F";

                  create_grid(int.Parse(GSize.Text));

            }

            private void Button3_Click(object sender, System.EventArgs e)
            {
                  Session["TextBoxGrid"] = tb;
            }
      }
}
0
 
LVL 1

Expert Comment

by:warmach
ID: 10927665
The code dealing with the Session object is correct.  I suspect that there is an error somewhere else.  Did you use IIS Manager to create an IIS applicaiton for this page?
0
 

Author Comment

by:shams_sa
ID: 10927920
The problem here is I did not assign the session variable to the TextBox[][] tb in the Button2_click method. Instead of that, i assign the TextBox tb to the session when the page is Unloaded using the following code:

private void Page_UnLoad(object sender, System.EventArgs e)
            {
                  Session["TextBoxGrid"] = tb;
            }

It works fine, but the new problem is when i do changes on the TextBoxs in the aspx page No Effect is happen. The Real content of tb[3][3] for example not change.. is there a way to solve this new problem ?

0
 
LVL 1

Expert Comment

by:warmach
ID: 10927976
It looks like each time you call create_grid...this code gets called

              tb[1][1].Text = "T";
               tb[2][2].Style.Add("Background","Black");

               tb[2][2].ReadOnly = true;
               if(tb[1][1].Text == "T")
                    tb[3][3].Text = "F";

This code looks like something that you might only want to do when you create the grid.  If so, put it inside another if...

if (tb == null){
...above code...
}

This way it is only run when the grid is create...
0
 

Author Comment

by:shams_sa
ID: 10928504
No this code is for Testing only. The problem is only This code is retrieved from the session, i.e only the values which the program insert. My values are not retrieved from the session
0
 
LVL 1

Expert Comment

by:warmach
ID: 10928824
Try my above suggestion and see what happens...
0
 

Author Comment

by:shams_sa
ID: 10929298
I tried it, but it did not work also. It seems that you did not understand my new problem.

The values I enter in the Boxes are not stored in the TextBox[][], i dont know why!!!!, even if i do save the session in the page unload method.

Note that Button3 is for Checking the content of a specific cell... it allways give me false becuase the filled cell is not stored.

Also if you want me to send you all my work to take a look at it, tell me.
Thanks again for your help.

Here is my new Code:

public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.TextBox GSize;
            System.Threading.Thread thrd;
            HtmlForm Form;
            protected System.Web.UI.WebControls.Button Button2;
            protected System.Web.UI.WebControls.Button Button3;
         
            TextBox[][] tb; // this is my TextBoxes
         
         
            public void create_grid(int size)
            {
                  
                  tb = (TextBox[][]) Session["TextBoxGrid"];

                  if (tb == null)
                  {
                        //The grid was not in the Session variable...create a new one
                        
                        tb = new TextBox[size+1][];
               
                        for(int i = 0;i<tb.Length;i++)
                              tb[i] = new TextBox[size+1];
                   
                        for(int i=0;i<tb.Length;i++)
                              for(int j=0;j<tb.Length;j++)
                                    tb[i][j] = new TextBox();
                  }

                  for (int i = 1; i<tb.Length; i++)
                        for(int j=1;j<tb.Length;j++)
                        {
                              int left = 25+((j-1)*25);
                              int top = 25+((i-1)*25);
                              tb[i][j].Style.Add("POSITION","absolute");
                              tb[i][j].Style.Add("HEIGHT","25");
                              tb[i][j].Style.Add("WIDTH","25");
                              tb[i][j].Style.Add("LEFT",left.ToString());
                              tb[i][j].Style.Add("TOP",top.ToString());
                              tb[i][j].ID = "textBoxddf"+i+j;          

                              //////////////////////////////////////  Adding them to the Form ///////////////////////////                                        
                              Form.Controls.Add(tb[i][j]);

                         
                              
                         
                        }

//                  tb[1][1].Text = "T";
//                  tb[2][2].Style.Add("Background","Black");
//    
//                  tb[2][2].ReadOnly = true;
//                  //            if(tb[1][1].Text == "T")
//                  tb[3][3].Text = "F";


                  Session["TextBoxGrid"] = tb;
               

            }
            
            private void Page_Load(object sender, System.EventArgs e)
            {
                  // Put user code to initialize the page here
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Button2.Click += new System.EventHandler(this.Button2_Click);
                  this.Button3.Click += new System.EventHandler(this.Button3_Click);
                  this.Load += new System.EventHandler(this.Page_Load);
                  Form = (System.Web.UI.HtmlControls.HtmlForm)this.FindControl("Form1");
                  this.Unload += new System.EventHandler(this.Page_UnLoad);
            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  create_grid(int.Parse(GSize.Text));
            }

            private void Button2_Click(object sender, System.EventArgs e)
            {
//            //      tb = (TextBox[][]) Session["TextBoxGrid"];
//                  for(int i=1;i<tb.Length;i++)
//                        for(int j=1;j<tb.Length;j++)
//                              Form.Controls.Add(tb[i][j]);

                  create_grid(int.Parse(GSize.Text));

                  if(tb[3][3].Text.Equals("3")) tb[3][1].Text = "TEST";
                  else tb[3][1].Text = "FTEST";

                  //create_grid(int.Parse(GSize.Text));

            }

            private void Button3_Click(object sender, System.EventArgs e)
            {
                  create_grid(int.Parse(GSize.Text));

                  if(tb[4][4].Text !="") Response.Write("Correct "+tb[4][4].Text);
                  else Response.Write("False "+tb[4][4].Text);
            }
            private void Page_UnLoad(object sender, System.EventArgs e)
            {
                  Session["TextBoxGrid"] = tb;
            }
      }

}
0
 

Author Comment

by:shams_sa
ID: 10976090
Thanx for your help warmach, but i solved my problem. I used Denis Bauer's custom placeholder control to automatically do add controls for me-- see http://www.DenisBauer.com. this site was given to me by Paul Wilson.

this control save the state of all the controls.

Regards,


0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11300187
PAQed, with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question