Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to build a dynamic form in a Sharepoint webpart?

Posted on 2010-08-16
4
Medium Priority
?
756 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 32

Accepted Solution

by:
Jamie McAllister MVP earned 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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. …
If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 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