Solved

How to build a dynamic form in a Sharepoint webpart?

Posted on 2010-08-16
4
694 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Last week I faced a strange issue recently, i have deployed SharePoint 2003 servers for one project and one of the requirements was to open SharePoint site from same server. when i was trying to open site from the same server i was getting authentic…
Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now