.Net 向导用户输入被覆盖



我一直在寻找很多,但找不到解决我所看到的问题的东西。我确信我错过了一些简单的东西,但我已经战斗了太久,真的需要弄清楚发生了什么。我有一个正在重建的现有(工作)用户控件。它是一个多步骤向导,每个步骤都是从表创建的一种"表单"。我已经成功地将 4 个步骤中的 3 个转换为div 以使它们动态化(使用 Bootstrap 3),但这一步,即第 2 步,与其他步骤不同。用户的输入正在丢失。原始代码(基于表)工作正常。它是在 .ascx 端声明的简单表:

<asp:WizardStep ID="childInformationStep" runat="server" Title="">
    <%-- Some more stuff...--%>
    <asp:Table cellpadding="2" class="annualSurveyTable" cellspacing="0" border="0" ID="tblChildInfo" runat="server">
    </asp:Table>
<asp:WizardStep>

在 c# 端,在Page_Load期间,将调用一种方法来循环浏览家庭的所有子项,并动态生成行,其中包含每个孩子的名字/姓氏、B 日、性别和年级的预填充输入单元格。它看起来像这样:

private void AddChildEdit(Person child, int index)
    {
        TableRow row = new TableRow();
        TableCell cell = new TableCell();
        row.ID = "trChildFirstName_" + index;
        cell.ID = "tcChildFirstName_" + index;
        cell.VerticalAlign = VerticalAlign.Middle;
        cell.HorizontalAlign = HorizontalAlign.Right;
        cell.Wrap = false;
        cell.CssClass = "registrationLabel";
        cell.Text = "Child's First Name";
        row.Cells.Add(cell);
        cell = new TableCell();
        TextBox tb = new TextBox();
        tb.ID = "tbChildFirstName_" + index;
        tb.Text = child.FirstName;
        tb.Enabled = false;
        cell.Controls.Add(tb);
        row.Cells.Add(cell);
        tblChildInfo.Rows.AddAt(tblChildInfo.Rows.Count, row);
        // snip (more of same for last name)
        row = new TableRow();
        cell = new TableCell();
        row.ID = "trChildBirthday_" + index;
        cell.ID = "tcChildBirthday_" + index;
        cell.VerticalAlign = VerticalAlign.Middle;
        cell.HorizontalAlign = HorizontalAlign.Right;
        cell.Wrap = false;
        cell.CssClass = "registrationLabel Birthday";
        cell.Text = "Child's Birth Date";
        row.Cells.Add(cell);
        cell = new TableCell();
        DateTextBox dtb = new DateTextBox();
        dtb.ID = "tbChildBirthday_" + index;
        dtb.CssClass = "registrationItem Birthday";
        if (child.BirthDate != DateTime.MinValue && child.BirthDate != DateTime.Parse("1/1/1900"))
            dtb.Text = child.BirthDate.ToShortDateString();
        cell.Controls.Add(dtb);
        row.Cells.Add(cell);
        tblChildInfo.Rows.AddAt(tblChildInfo.Rows.Count, row);
        row = new TableRow();
        cell = new TableCell();
        row.ID = "trChildGender_" + index;
        cell.ID = "tcChildGender_" + index;
        cell.VerticalAlign = VerticalAlign.Middle;
        cell.HorizontalAlign = HorizontalAlign.Right;
        cell.Wrap = false;
        cell.CssClass = "registrationLabel";
        cell.Text = "Child's Gender";
        row.Cells.Add(cell);
        cell = new TableCell();
        DropDownList ddlGender = new DropDownList();
        ListItem l = new ListItem("", "", true);
        l.Selected = true;
        ddlGender.Items.Add(l);
        l = new ListItem("Male", "0", true);
        ddlGender.Items.Add(l);
        l = new ListItem("Female", "1", true);
        ddlGender.Items.Add(l);
        ddlGender.ID = "ddlChildGender_" + index;
        // snip (there is one more row added for grade
    }

save 方法看起来像是在表中循环查找与它正在循环访问的子项相关的输入,并拉入文本值,其中应包括用户所做的任何更改。它按预期工作,看起来像这样(顺便说一句,我没有写它,看起来可以清理很多:D)

private void SaveChildValues()
    {
        string userID = CurrentUser.Identity.Name + " - Annual Survey";
        if (userID == " - Annual Survey")
            userID = "Annual Survey";
        int i = 0;
        foreach (Person child in childrenList)
        {
            TableCell selectedCell = null;
            foreach (TableRow row in tblChildInfo.Rows)
            {
                if (row.ID == "trChildBirthday_" + i)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        if (cell.ID == "tcChildBirthday_" + i)
                        {
                            selectedCell = cell;
                            DateTextBox box = (DateTextBox)selectedCell.FindControl("tbChildBirthday_" + i);
                            if (box.Text.Trim() != string.Empty)
                                try { child.BirthDate = DateTime.Parse(box.Text); }
                                catch { }
                            i++;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        i = 0;
        foreach (Person child in childrenList)
        {
            TableCell selectedCell = null;
            foreach (TableRow row in tblChildInfo.Rows)
            {
                if (row.ID == "trChildGender_" + i)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        if (cell.ID == "tcChildGender_" + i)
                        {
                            selectedCell = cell;
                            DropDownList ddl = (DropDownList)selectedCell.FindControl("ddlChildGender_" + i);
                            if (ddl.SelectedValue != string.Empty)
                                try { child.Gender = (Gender)Enum.Parse(typeof(Gender), ddl.SelectedValue); }
                                catch { }
                            i++;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        i = 0;
        foreach (Person child in childrenList)
        {
            TableCell selectedCell = null;
            foreach (TableRow row in tblChildInfo.Rows)
            {
                if (row.ID == "trChildGrade_" + i)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        if (cell.ID == "tcChildGrade_" + i)
                        {
                            selectedCell = cell;
                            DropDownList ddl = (DropDownList)selectedCell.FindControl("ddlChildGrade_" + i);
                            if (ddl.SelectedValue != string.Empty)
                                try { child.GraduationDate = Person.CalculateGraduationYear(Int32.Parse(ddl.SelectedValue), CurrentOrganization.GradePromotionDate); }
                                catch { }
                            i++;
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }

现在,这是我对该部分所做的更改。页面加载并运行所有动作,但当保存发生时,它会再次从子记录中提取原始数据库值,而不是用户的输入。我只是将表更改为 .ascx 文件中的 ASP 面板:

<asp:WizardStep ID="childInformationStep" runat="server" Title="">
    <%-- Some more stuff...--%>
    <asp:Panel ID="tblChildInfo" runat="server" ClientIDMode="Static">
    </asp:Panel>
<asp:WizardStep>

我已将动态行创建更改为动态div,为 bootstrap 3 布局:

private void AddChildEdit(Person child, int index)
    {
        Panel childRow = new Panel();
        childRow.ID = "ChildRow_" + index;
        childRow.CssClass = "form-horizontal";
        LiteralControl childTitle = new LiteralControl();
        childTitle.Text = string.Format("<h4>Child {0}:</h4>", (index + 1).ToString());
        childRow.Controls.Add(childTitle);
        Panel formGroup = new Panel();
        formGroup.ID = "trChildFirstName_" + index;
        formGroup.CssClass = "form-group";
        childRow.Controls.Add(formGroup);
        Panel inputContainer = new Panel();
        inputContainer.CssClass = "col-sm-8";
        formGroup.Controls.Add(inputContainer);
        TextBox tb = new TextBox();
        tb.ID = "tbChildFirstName_" + index;
        tb.Text = child.FirstName;
        tb.Enabled = false;
        inputContainer.Controls.Add(tb);
        Label inputLabel = new Label();
        inputLabel.ID = "tcChildFirstName_" + index;
        inputLabel.CssClass = "col-sm-3 control-label registrationLabel";
        inputLabel.Text = "First Name";
        inputLabel.AssociatedControlID = tb.ID;
        formGroup.Controls.AddAt(0, inputLabel);
        tblChildInfo.Controls.Add(childRow);
        // snip (more code for adding Last Name row
        formGroup = new Panel();
        formGroup.ID = "trChildBirthday_" + index;
        formGroup.CssClass = "form-group";
        inputContainer = new Panel();
        inputContainer.ID = "tcChildBirthday_" + index;
        inputContainer.CssClass = "col-sm-8";
        formGroup.Controls.Add(inputContainer);
        TextBox dtb = new TextBox();
        dtb.ID = "tbChildBirthday_" + index;
        dtb.CssClass = "form-control survey-control date-mask registrationItem";
        dtb.Attributes.Add("placeholder", "MM/DD/YYYY");
        if (child.BirthDate != DateTime.MinValue && child.BirthDate != DateTime.Parse("1/1/1900"))
            dtb.Text = child.BirthDate.ToString("MM/dd/yyyy");
        inputContainer.Controls.Add(dtb);
        inputLabel = new Label();
        inputLabel.CssClass = "col-sm-3 control-label";
        inputLabel.Text = "BirthDate";
        inputLabel.AssociatedControlID = dtb.ID;
        formGroup.Controls.AddAt(0, inputLabel);
        childRow.Controls.Add(formGroup);
        // snip (more of the same, adding two more rows for gender and grade)
    }

我将保存方法简化为:

private void SaveChildValues()
    {
        string userID = CurrentUser.Identity.Name + " - Annual Survey";
        if (userID == " - Annual Survey")
            userID = "Annual Survey";
        int i = 0;
        foreach (Person child in childrenList)
        {
            try
            {
                TextBox box = (TextBox)tblChildInfo.FindControl("tbChildBirthday_" + i);
                if (box.Text.Trim() != string.Empty)
                    child.BirthDate = DateTime.Parse(box.Text);
            }
            catch { }
            try
            {
                DropDownList ddl = (DropDownList)tblChildInfo.FindControl("ddlChildGender_" + i);  
                if (ddl.SelectedValue != string.Empty)
                    child.Gender = (Gender)Enum.Parse(typeof(Gender), ddl.SelectedValue);
            }
            catch {}
            try
            {
                DropDownList ddl = (DropDownList)tblChildInfo.FindControl("ddlChildGrade_" + i);
                if (ddl.SelectedValue != string.Empty)
                    child.GraduationDate = Person.CalculateGraduationYear(Int32.Parse(ddl.SelectedValue), CurrentOrganization.GradePromotionDate);
            }
            catch { }
            i++;
        }

据我了解,我的代码不会改变任何基本行为,除了使用div 元素来构建动态内容而不是向表中添加行。我缺少什么导致我的更新代码丢失用户的输入?

注意:这是第二步,其中呈现信息,为子信息捕获。save 方法直到步骤 4 才会执行,因此输入数据应通过另外两个步骤持久化,并保持原样。我尝试使用调试器,但永远看不到用户输入。我不知道我是否在错误的断点处寻找它,但我似乎找不到用户输入随帖子返回的位置,以及何时应该将其写入输入。任何帮助将不胜感激。

您可以

尝试将字段的动态创建移动到Page_Init部分而不是Page_Load。

最新更新