1

编辑:如果有人还可以提出一种更明智的方法来使我在下面尝试的事情发生,那也将不胜感激

我正在构建一个多页表单,该表单从 POST 方法中获取(产品)数量,并显示依赖于该数字的表单序列。当用户转到下一页时,表单应该收集并显示此信息(用于确认),然后将此信息发送到将提供要显示的 URL 的服务。

不用说,我在完成这项工作时遇到了问题。这是我的(匿名)代码的相关部分:

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)

对不起(业余代码)的数量。我怀疑如果失败是 FindControl() 不起作用,但我不知道为什么......

如果可以提供任何帮助,我将非常感激。

编辑:显示错误可能会有所帮助:

错误(第 112 行)异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误:

第 111 行:哈希表 tempUser = new Hashtable();
第 112 行: tempUser.Add("name",name.Text);
第 113 行:tempUser.Add("email",email.Text);
第 114 行:this.Users.Add(tempUser);

4

2 回答 2

0

您的问题在于您每次都在 Page_Load 中重新加载表单。确保只加载一次动态文本框,并且在需要确认时能够找到它们。只要 Page_Load 重建,您将找不到答案,并且可能找不到任何东西。

于 2009-03-11T17:18:16.843 回答
0

我想到了:

FindControl() 直接搜索它所调用的控件的子项。

当我调用它时,它(自动)是 Page.FindControl() 我已经将表创建嵌套在一个字段和一个表控件中

当我调用 tableID.FindControl() 时,它找到了应有的控件。

感谢您的帮助,格雷戈里,以及大家的所有评论。

-马特

于 2009-03-13T19:28:32.103 回答