views:

298

answers:

2

Edit: if someone could also suggest a more sensible way to make what I'm trying below to happen, that would also be very appreciated

I'm building an multiPage form that takes a quantity (of product) from a POST method, and displays a form sequence relying on that number. when the user goes to the next page, the form is supposed to collect this information and display it (for confirmation), which will then send this info to a service that will supply URL's to display.

Needless to say, I'm having problems making this work. Here is the relevant parts of my (anonymised) code:

public partial class foo : System.Web.UI.Page
{
 Int quantityParam    = 3;
 ArrayList Users  = new ArrayList();
 //the information for each user is held in a hashtable the array list will be an array list of the user hashtables


protected void Page_Init(object sender, EventArgs e)
{
    if(null != Request["quantity1"])
       { 
             this.quantityParam = Request["quantity1"];
       }
}
protected void Page_Load(object sender, EventArgs e)
{
    int quantity = this.quantityParam;
    if(quantity < 1){ mviewThankYou.SetActiveView(View4Error);} 
    else 
    { //create a form for each user
        mviewThankYou.SetActiveView(View1EnterUsers);
        for(int user = 0;user < quantity; user++)
        {
            createUserForm(user);    
        }
    }   
}
protected void BtnNext1_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        for(int i = 0; i < quantity; i++)
        {
            String ctrlName = "txtUser" + i.ToString();
            String ctrlEmail = "txtEmail" + i.ToString();
            TextBox name = (TextBox)FindControl(ctrlName);
            TextBox email = (TextBox)FindControl(ctrlEmail);

            /*BONUS QUESTION: How can I add the Hashtables to the Users Array without them being destroyed when I leave the function scope?

            this is where the failure occurs: 
            System.NullReferenceException: Object reference not set to an instance of an object. on: "tempUser.Add("name",name.Text); 
            */

            Hashtable tempUser = new Hashtable();
            tempUser.Add("name",name.Text);
            tempUser.Add("email",email.Text);
            this.Users.Add(tempUser);
        }
        for(int i = 0; i < quantity; i++)
        {
            v2Content.Text +="<table><tr><td>Name: </td><td>"+
            ((Hashtable)Users[i])["name"]+
            "</td></tr><tr><td>Email:</td><td>"+
            ((Hashtable)Users[i])["email"]+
            "</td></tr></table>";
        }
        mviewThankYou.SetActiveView(View2Confirm);
    }
}
private void createUserForm(int userNum){
    DataTable objDT = new DataTable();
        int rows = 2;
        int cols = 2;

 //create the title row..
 TableRow title = new TableRow();
 TableCell titleCell = new TableCell();
 formTable.Rows.Add(title);
 Label lblUser = new Label();
 lblUser.Text = "<b>User "+ (userNum+1) + "</b>";
 lblUser.ID = "lblTitle"+ userNum;
 titleCell.Controls.Add(lblUser);
 title.Cells.Add(titleCell);

 for(int i = 0; i < rows; i++)
 {
  TableRow tRow = new TableRow();  
  formTable.Rows.Add(tRow);
  for(int j = 0; j < cols; j++)
  {
   TableCell tCell = new TableCell();
   if(j == 0){
    Label lblTitle = new Label();
    if(i == 0){
     lblTitle.Text = "User Name:";
     lblTitle.ID = "lblUser" + userNum;
    }
    else{
     lblTitle.Text = "User Email:";
     lblTitle.ID = "lblEmail" + userNum;
    }
    tCell.Controls.Add(lblTitle);
   } else {
    TextBox txt = new TextBox();
    if(i==0){
     txt.ID = "txtUser" + userNum;
    }
    else{
     txt.ID = "txtEmail" + userNum;
    }
    RequiredFieldValidator val = new RequiredFieldValidator();
    val.ID = txt.ID + "Validator";
    val.ControlToValidate = txt.UniqueID;
    val.ErrorMessage = "(required)";

    tCell.Controls.Add(txt);
    tCell.Controls.Add(val);
   }
   tRow.Cells.Add(tCell);
  }//for(j)
 }//for(i)

 //create a blank row...
 TableRow blank = new TableRow();
 TableCell blankCell = new TableCell();
 formTable.Rows.Add(blank);
 Label blankLabel = new Label();
 blankLabel.Text = " ";
 blankLabel.ID = "blank" + userNum;
 blankCell.Controls.Add(blankLabel);
 blank.Cells.Add(blankCell);   

}//CreateUserForm(int)

Sorry for the gnarly amount of (amateur code). What I suspect if failing is that FindControl() is not working, but I can't figure out why...

if any help can be given, I'd be very greatful.

Edit: showing the error might help:

Error (Line 112) Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 111: Hashtable tempUser = new Hashtable();
Line 112: tempUser.Add("name",name.Text);
Line 113: tempUser.Add("email",email.Text);
Line 114: this.Users.Add(tempUser);

A: 

You problem comes in the fact you are reloading the form every time in Page_Load. Make sure you only load the dynamic text boxes once and you will be able to find them when you need them for confirmation. As long as Page_Load rebuilds, you will not find the answer, and risk not finding anything.

Gregory A Beamer
while this is a good point and I made the change, it didn't fix the problem.
Matt Dunnam
A: 

I figured it out:

FindControl() works as a direct search of the children of the control it's called on.

when I was calling it, it was (automatically) Page.FindControl() I had nested the table creation inside a field and a Table control

when I called tableID.FindControl() it found the controls just as it should.

Thanks for the help, Gregory, and for all the comments everyone.

-Matt

Matt Dunnam