【问题标题】:Gathering Data: Dynamic Text Boxes收集数据:动态文本框
【发布时间】:2010-10-12 06:44:11
【问题描述】:

编辑:如果有人也可以提出一种更明智的方法来实现我在下面尝试的事情,那也将非常感激

我正在构建一个多页表单,该表单从 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);

【问题讨论】:

  • “我怀疑如果失败是 FindControl() 不起作用,但我不知道为什么...”,请在调试器上运行它,您将能够分辨出哪个肯定行,如果还是不明白可以回来
  • FindControl() 正在寻找在 createUserForm() 中创建的控件,特别是从以下代码开始: ... Label lblTitle = new Label();并结束 tCell.Controls.Add(txt) ...(评论太长)我应该发布什么其他文件? (我也认为 FindControl() 失败了,但不明白为什么......)

标签: asp.net forms dynamic view


【解决方案1】:

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

【讨论】:

  • 虽然这是一个很好的观点并且我做出了改变,但它并没有解决问题。
【解决方案2】:

我想通了:

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

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

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

感谢 Gregory 的帮助,感谢所有 cmets 各位。

-马特

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2014-11-26
    • 2015-11-18
    • 2021-10-20
    • 2010-09-10
    • 1970-01-01
    相关资源
    最近更新 更多