代码之家  ›  专栏  ›  技术社区  ›  Adam

在SharePoint Web部件中为动态控件持久化用户输入

  •  1
  • Adam  · 技术社区  · 17 年前

    使用解决方案在底部编辑

    我将这些作为Web部件的类变量:

    Panel constraintPanel;
    HtmlInputText[] constraints;
    Label[] constraintLabels = null;
    

    在覆盖CreateChildControls中,我初始化面板:

    constraintPanel = new Panel();
    

    我在重写的OnPreRender中构建了动态输入框(注意:我听说有人说可以在OnInit、OnLoad或OnPreRender中实现,但OnPreRender是唯一一个不会导致整个Web部件出错的输入框):

    protected override void OnPreRender(EventArgs e)
    {
        buildConstraints();
        base.OnPreRender(e);
    }
    
    private void buildConstraints()
    {
        if (!viewSelect.SelectedValue.Equals(INConstants.NoFilterOption))
        {
            string[,] constraintList = docManager.GetFilterConstraints(viewFilterSelect.SelectedValue);
            if (constraintList != null)
            {
                this.constraints = new HtmlInputText[constraintList.Length / 2];
                this.constraintLabels = new Label[constraintList.Length / 2];
                for (int constraintCount = 0; constraintCount < constraintList.Length / 2; constraintCount++)
                {
                    Label constraintLabel = new Label();
                    constraintPanel.Controls.Add(constraintLabel);
                    constraintLabel.Text = constraintList[constraintCount, 0];
                    this.constraintLabels[constraintCount] = constraintLabel;
    
                    HtmlInputText constraint = new HtmlInputText();
                    constraintPanel.Controls.Add(constraint);
                    constraint.ID = "constraint_" + constraintCount;
                    constraint.MaxLength = 12;
                    constraint.Style.Add("FONT-FAMILY", "Verdana");
                    constraint.Style.Add("FONT-SIZE", "11");
                    this.constraints[constraintCount] = constraint;
                }
            }
        }
    }
    

    ...
    constraintPanel.RenderBeginTag(output); // not sure if this is needed
    if (constraints != null && constraints.Length > 0)
    {
        foreach (Control tempControl in constraintPanel.Controls)
        {
            if (tempControl is Label)
            {
                output.WriteLine("<tr>");
                output.WriteLine("<td width='2%' nowrap><font class='search-header'>");
                tempControl.RenderControl(output);
                output.WriteLine("&nbsp;");
            }
            else if (tempControl is HtmlInputText)
            {
                tempControl.RenderControl(output);
                output.WriteLine("</td>");
                output.WriteLine("<td width='*' nowrap></td>");
                output.WriteLine("</tr>");
            }
        }
    }
    constraintPanel.RenderEndTag(output); // not sure if this is needed
    ...
    

    使用解决方案编辑:

    以下是OnLoad中的代码,仅供参考:

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        try
        {
            viewsBuildConstraints();
        }
        catch (Exception)
        {
        }
    }
    
    1 回复  |  直到 17 年前
        1
  •  2
  •   Colin    17 年前

    尝试使用INamingContainer界面标记您的Web部件,并确保为所有控件提供ID。此外,我认为HtmlInput控件没有viewstate,这会导致它们在回发后“忘记”输入。你能试着使用实际的文本框控件吗?