Solved

How to build a dynamic form in a Sharepoint webpart?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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: …

729 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