I have a requirement that I need to preset the value of some columns of a new SPListItem when entering data.
The default NewForm.aspx cannot serve the purpose. So, I use the sharepoint built-in web controls to build a customized data input page. The following is some code snippets:
SPListItem newItem = this.DataList.Items.Add();
//Preset some values before populating the controls
newItem["Title"] = "Test Title";
newItem["Agency"] = "Education";
newItem["Description"] = "Prepopulate my data accordingly";
//Use the following code to get the proper control to populate entering contol on UI
BaseFieldControl control = field.FieldRenderingControl;
control.ListId = list.ID;
control.ItemId = item.ID;
control.FieldName = field.Title;
control.ID = GetControlID(field);
control.ControlMode = SPControlMode.New;
I was expecting the value preset would be the default value of the controls populated on the UI. What I actually got: the preset value like "Test Title", "Education" were not populated. Instead, they were just blank text box input controls.
I think it was caused by the ControlMode has been set as SPControlMode.New. The sharepoint control knows the setting and just ignores any value has been assigned to the SPListItem. However, if I set the ControlMode as SPControlMode.Edit, the sharepoint controls will still ignore the value set and prepopulate some value like Title field value, Agency field value, Description field value based on its built-in behaviour. To precreate the item in the list and then edit it is not an option because a user could cancel the creation.
Can someone please share some lights on this?
First, if you aren't calling newItem.Update(); then the values you are setting are never written back to the database. Second, if you create the new item programmatically then you are creating another if you use the SPControlMode.New setting. What I think you want is to create the new item programmatically and then point the edit control at it by setting the control.ItemId equal to the GUID of the new Item that you just created.
Paul Stork SharePoint Server MVP
Thank you for the reply, Paul!
What I've been trying to do is to create a customized UI to enter data into a list. The list has 16 columns with 6 of them as lookup, 3 of them as RichTextBox. As I stated above, I use Sharepoint built-in controls to build the input UI. The problem I encountered is that I cannot find a way to set the default value of the controls based on different scenarios. One workaround is to precreate a new item in the list first. And then edit the item in the SPControlMode.Edit mode.
However, the precreate option is not an option. Can you please provide some code snippets to set the value of the sharpoint built-in web controls?
I am not sure what you mean by saying - "What I think you want is to create the new item programmatically"
this setup doesn't look nice, I would play with the standard NewForm.aspx page - you can manipulate a lot there, since everything that appears on it is defined in control templates - it's all in the controltemplates/DefaultTemplates.ascx - so basically you can define a custom template for your custom content type and either use the standatd ListFieldIterator control or place your field controls directly in the template declaratively.
with your solution you can try to set the .Value property of your field controls but on some later page event - e.g. the OnPreRender one.
I believe ASP.NET AJAX's TextBoxWatermark is what you're looking for, take a look at it's demo:
Cogito, ergo sum.
I don't think TextBoxWatemark is what I need. I guess I need to make my question more straightforward.
There are bunch of built-in sharepoint web controls like LookupField, MultipleLookupField, RichTextField, TextField and etc. I need to use them to build a web part for customized data input UI. I need to set the default value of the controls based on business logic. The problem is I can't find any way to set the default value.
Can someone please provide some input for the issue?
You may want to try this:
I dont think you can preselect values sharepoint controls unless you have a field already created for them. The DefaultValue property of the field needs be changed.
However if you want full control on the how the controls should behave then I would recommend using asp.net controls instead of sharepoint control. For example use drop down list control instead of lookup field, textbox for single line of textbox etc.