在PreRender之后,可能无法注册扩展程序控件。控件动态添加

时间:2014-03-03 13:50:37

标签: c# asp.net

我正在动态地向表单添加控件,然后回读响应。为此,我必须在回发之前重新创建表单,然后再尝试获取响应。 目前,只有需要日期的字段才有AjaxControlToolkit MaskedEditExtender:

    case "DAT":
                            TextBox tb = new TextBox();
                            tb.ID = "tbDat" + intQuestionCount.ToString();
                            tb.CssClass = "PositionCol3 SVSTextBox1";
                            tb.Width = 250;
                            // check for date range restricdtions and display in tooltip
                            strTooltip = "";
                            if (question.DateMaxDaysInPast != 0)
                            {
                                DateTime dtPastDate = DateTime.Now.AddDays(-Convert.ToInt32(question.DateMaxDaysInPast));
                                strTooltip = "Date must be after " + dtPastDate.ToShortDateString();
                            }
                            if (question.DateMaxDaysInFuture != 0)
                            {
                                DateTime dtFutureDate = DateTime.Now.AddDays(Convert.ToInt32(question.DateMaxDaysInFuture));
                                if (strTooltip == "")
                                {
                                    strTooltip = "Date must be before " + dtFutureDate.ToShortDateString();
                                }
                                else
                                {
                                    strTooltip += " and before " + dtFutureDate.ToShortDateString();
                                }
                            }
                            tb.ToolTip = strTooltip; ;
                            // if this is a completed form look for saved response
                            if (intCompletedSurveyAnswerId != 0)
                            {
                                tb.Text = strAnswerText;
                            }
                            questionsPanel.Controls.Add(tb);

                            // add to list of controls
                            m_orderOfControls[intNumOfControls] = "TB";
                            intNumOfControls += 1;
                            // add to saved textboxes
                            m_dynamicTextBoxes[intTextBoxCount] = tb;
                            intTextBoxCount += 1;

                            // try add masked edit extender
                            AjaxControlToolkit.MaskedEditExtender maskedEdit = new AjaxControlToolkit.MaskedEditExtender();
                            maskedEdit.ID = "mk_" + tb.ID;
                            maskedEdit.TargetControlID = tb.ID;
                            maskedEdit.Mask = "99/99/9999";
                            maskedEdit.MaskType = AjaxControlToolkit.MaskedEditType.Date;
                            //maskedEdit.InputDirection = AjaxControlToolkit.MaskedEditInputDirection.RightToLeft;
                            questionsPanel.Controls.Add(maskedEdit);
                            // add to list of controls
                            m_orderOfControls[intNumOfControls] = "MK";
                            intNumOfControls += 1;
                            // add to saved textboxes
                            m_dynamicMasks [intMaskCount] = maskedEdit;
                            intMaskCount  += 1;


                            break;

m_orderOfControls,intNumOfControls,m_dynamicMasks,intMaskCount都保存为会话变量,以便可以在回发时重新创建表单:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        retrieveDynamicControlsFromSession();
        recreateForm();
    }

    protected void recreateForm()
    {
        //
        // Form needs to be recreated after postback exactly as want sent out
        //
        try
        {

            int intNumOfLabels = 0;
            int intNumOfTextBoxes = 0;
            int intNumDdls = 0;
            int intNumCbls = 0;
            int intNumRbls = 0;
            int intNumCbs = 0;
            int intNumLitControls = 0;
            int intNumMaskedEditExtenders = 0;
            questionsPanel.Controls.Clear();

            foreach (string controlType in m_orderOfControls)
            {
                if (controlType != null)
                {
                    LiteralControl htmlControl = new LiteralControl();

                    switch (controlType)
                    {
                        case "MK":
                            AjaxControlToolkit.MaskedEditExtender maskedEdit = new AjaxControlToolkit.MaskedEditExtender();
                            maskedEdit = m_dynamicMasks[intNumMaskedEditExtenders];
                            intNumMaskedEditExtenders += 1;
                            questionsPanel.Controls.Add(maskedEdit);
                            break;
                        case "LB":
                            Label lbl = new Label();
                            lbl = m_dynamicLabels[intNumOfLabels];
                            intNumOfLabels += 1;
                            questionsPanel.Controls.Add(lbl);
                            break;
                        case "TB":
                            TextBox textBox = new TextBox();
                            textBox = m_dynamicTextBoxes[intNumOfTextBoxes];
                            intNumOfTextBoxes += 1;
                            questionsPanel.Controls.Add(textBox);
                            break;
                        case "CB":
                            CheckBox checkBox = new CheckBox();
                            checkBox = m_dynamicCheckBoxes[intNumCbs];
                            intNumCbs += 1;
                            questionsPanel.Controls.Add(checkBox);
                            break;
                        case "CBL":
                            CheckBoxList checkBoxList = new CheckBoxList();
                            checkBoxList = m_dynamicCheckBoxLists[intNumCbls];
                            intNumCbls += 1;
                            questionsPanel.Controls.Add(checkBoxList);
                            break;
                        case "RBL":
                            RadioButtonList radioButtonList = new RadioButtonList();
                            radioButtonList = m_dynamicRadioButtonLists[intNumRbls];
                            intNumRbls += 1;
                            questionsPanel.Controls.Add(radioButtonList);
                            break;
                        case "HTML":
                            LiteralControl hmtlControl = new LiteralControl();
                            htmlControl = m_literalControls[intNumLitControls];
                            intNumLitControls += 1;
                            questionsPanel.Controls.Add(htmlControl);
                            break;
                        case "DDL":
                            DropDownList dropDownList = new DropDownList();
                            dropDownList = m_dynamicDropDownLists[intNumDdls];
                            intNumDdls += 1;
                            questionsPanel.Controls.Add(dropDownList);
                            break;
                        default:
                            break;
                    }
                    // end paragraph

                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

在测试时,在使用表单“保存”按钮一次或两次后,我收到此错误:在PreRender之后可能无法注册扩展程序控件。

有什么想法吗?

0 个答案:

没有答案