Solved

How to build a dynamic form in a Sharepoint webpart?

Posted on 2010-08-16
4
734 Views
Last Modified: 2012-05-10
Hi experts,

I'm developing a Sharepoint webpart which must show a form that the user will fill in.
The fields in the form are generated in the runtime dynamically. That means, that this webpart can be used by user1 and he configures it to show 3 fields, while another user2 can uses it to show a form with 5 fields, etc..

Well, I could render the fields of the form using C# code in the "Render" function of my Webaprt class which inherits from Microsoft.SharePoint.WebPartPages.WebPart class, and here is the code:

protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.Write("First name: <input type=\"text\" name=\"fname\" />" +
                         "<br />Last name: <input type=\"text\" name=\"lname\" /><br />" +
                         "<input type=\"submit\" value=\"Submit\" />");
            writer.Write("<br/>");
        }

So, the actual webpart shows a form with two fields "Firstname" and "Lastname", and one button to submit.
First, is there a better method to reach my goal? If yes, how? If no, my problem,with this method, is : how can I associate the Click event handler to the button? So, when the user clicks on the button, How can I get the entered values in the the two fields?

Thanks.
0
Comment
Question by:pgirardier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 33447256
Your life would be much easier if you did this a slightly different way.

Declare controls for your textboxes, and instantiate them and set their properties in 'CreateChildControls'. Add them to the controls collection.

Then in Render, instead of throwing out markup as you have in your example, use e.g. txtMyBox.RenderControl(writer) type code to render the control.

Doing it this way makes the controls fully fledged ASP.NET controls and you can get the properties on postback easily.

Example as attached.
protected override void CreateChildControls()
        {
this._tbxFirstName = new TextBox();
            this._tbxFirstName.ID = "tbxFirstName";
            this._tbxFirstName.MaxLength = 25;
            this._tbxFirstName.Text = string.Empty;
            this._tbxFirstName.ToolTip = "Your First Name";
            this.Controls.Add(_tbxFirstName);
}

protected override void Render(HtmlTextWriter writer)
        {
this._tbxFirstName.RenderControl(writer);
}

Open in new window

0
 
LVL 9

Expert Comment

by:SharePointGirl
ID: 33450083
How are you deciding how many fields your user has?

If you use the

 Button saveButton = new Button();
    saveButton.ID = "SaveButton";
    saveButton.Text = "Save";
 saveButton.Click += new EventHandler(saveButton_Click);
this.Controls.Add(saveButton);

in the CreateChildControls method


void saveButton_Click(object sender, EventArgs e)
{
      string name   = _tbxFirstName.Text
}

 
0
 
LVL 2

Expert Comment

by:LeicaLouie
ID: 33459989
From what I understand, you're trying to replicate what SharePoint already does out of the box.
When you create a list with three columns, the DataFormWebPart will render a form with the three field for the user to fill.

Unless there is a strong reason to code a new WebPart I don't see why you can't acomplish this with SharePoint's own WebParts.
0
 

Author Closing Comment

by:pgirardier
ID: 33473682
This answer has guided me to reach my goal.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question