简体   繁体   English

如何在工具部件(在Webpart中)中封装经常重复的代码?

[英]How can I do encapsulated often repetitive code in a toolpart (in webpart)?

my webpart should have a lot of questions with answertype dropdownlist. 我的Webpart应该有很多与answertype dropdownlist有关的问题。 In the settings the questions are configurable with a toolpart. 在设置中,可以使用工具部件配置问题。 Write and read the settings of one question works. 编写和阅读一个问题作品的设置。 I don´t know how the code works for x-more questions (in which method must I use the redundant code - where and how can I encapsulated the code? Have you a tip for me, or do you know an interessting website? I hope you can understand my problem; else I write more details... 我不知道该代码如何解决x个以上的问题(我必须使用哪种方法使用冗余代码-我在哪里以及如何封装该代码?给我一个提示,还是一个有趣的网站?希望您能理解我的问题;否则我会写更多详细信息...

thanks 谢谢

In Addition the (essential part of) code from the toolpart: 另外,来自工具部分的(重要部分)代码:

public class FeedbackToolpart : ToolPart
{
    Label ques1Lab, ans1Lab, typ1Lab;
    DropDownList ddList;
    List<Question> outputList;
    ...

    public FeedbackToolpart() : base() { this.Title = "Bewertungseinstellungen"; }

    protected override void CreateChildControls()
    {
        parentWebPart = (Feedbackwebpart)this.ParentToolPane.SelectedWebPart;
        ...
        ddList = new DropDownList();
        ddList.ID = "TheCheckBoxList";
        ddList.Items.Add("");
        ddList.Items.Add("Schieberegler");
        ddList.Items.Add("Checkboxen");
        ddList.Items.Add("Textbox");
        ddList.SelectedIndexChanged += new EventHandler(ddList_SelectedIndexChanged);
        ddList.AutoPostBack = true;

        ddList.SelectedValue = (parentWebPart.MyValue != null) ? parentWebPart.MyValue[0].answType : "Textbox";

        this.Controls.Add(pan);
        base.CreateChildControls();            
    }

    protected void ddList_SelectedIndexChanged(object sender, EventArgs e) { ... }

    public override void ApplyChanges()
    {
        parentWebPart = (Feedbackwebpart)this.ParentToolPane.SelectedWebPart;
        outputList = new List<Question>();
        outputList.Add(new Question(texQuestion.Text, texAnswers.Text, ddList.SelectedValue));
        parentWebPart.MyValue = outputList;           
    }
}

One design approach could be: 一种设计方法可能是:

Create an XML with an element for every question (encapsulating text, UI control type and/or other characteristics you need). 使用每个问题的元素创建XML(封装文本,UI控件类型和/或所需的其他特征)。 Create .NET classes to deserialize the content of your XML by the standard .NET XML serializer. 创建.NET类,以通过标准.NET XML序列化程序反序列化XML的内容 Deploy the XML file somewhere to SharePoint you can access it depending on your solution type (farm or sandbox). 将XML文件部署到SharePoint的某个位置,您可以根据解决方案类型(服务器场或沙箱)访问它。 You would read its contents either over HTTP or by SP OM. 您可以通过HTTP或SP OM读取其内容。 After deserializing it you'd enumerate the array of question-objects in a loop and create a table-row with specified input control for each question. 反序列化之后,您将在一个循环中枚举问题对象数组,并为每个问题创建一个具有指定输入控件的表行。

--- Ferda -费尔达

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM