Solved

How to build a dynamic form in a Sharepoint webpart?

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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