0

my webpart should have a lot of questions with 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...

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;           
    }
}
Rotaney
  • 247
  • 1
  • 3
  • 17

1 Answers1

1

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). Create .NET classes to deserialize the content of your XML by the standard .NET XML serializer. Deploy the XML file somewhere to SharePoint you can access it depending on your solution type (farm or sandbox). You would read its contents either over HTTP or by 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

Community
  • 1
  • 1
Ferdinand Prantl
  • 5,281
  • 34
  • 35
  • Thank you for your answer. I´m not sure, whether I understand correctly.The XML-document is only to store the data, is this right? I to simplify my problem again: it isn´t the data storage, but the output with unlimited repeat. I have in my toolpart a textbox. If something is entered into, an other empty textbox are displayed and so on (1.problem). – Rotaney May 14 '12 at 06:29
  • The content of every textbox, shall after "OK" or "Apply"-button, show in the webpart over a label. I have it so defined: How can I say the ascx-file, that I want x more label (with different ID) (2.problem).Or did you have your answer already referred to this and I don´t understand how I can do the repeat with xml? – Rotaney May 14 '12 at 06:34
  • 1
    The XML file is to separate data definition from the code. You should not harcode the data; just the logic. Instead of creating controls in a loop (which alone is not redundant anymore) you can have a look at [`Repeater`](http://msdn.microsoft.com/en-us/library/x8f2zez5.aspx). You would create a template with control(s) to repeat and bind a collection of data to the repeater to populate it. – Ferdinand Prantl May 14 '12 at 08:24