收集数据:动态文本框

时间:2009-03-09 19:49:10

标签: asp.net forms dynamic view

编辑:如果有人也可以提出一种更明智的方式来制作我正在尝试下面发生的事情,那也将非常感激

我正在构建一个多页面表单,它从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行:Hashtable tempUser = new Hashtable();
第112行:tempUser.Add(“name”,name.Text);
第113行:tempUser.Add(“email”,email.Text);
第114行:this.Users.Add(tempUser);

2 个答案:

答案 0 :(得分:0)

您遇到的问题是每次在Page_Load中重新加载表单。确保只加载动态文本框一次,当您需要它们进行确认时,您将能够找到它们。只要Page_Load重建,您将找不到答案,并且无法找到任何内容。

答案 1 :(得分:0)

我明白了:

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

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

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

感谢Gregory的帮助以及所有人的评论。

-Matt