Programatically Set Default Value of Sharepoint Controls


  • Please help!

    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?

    Thank you!

    Freitag, 5. März 2010 17:24

Alle Antworten

  • 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
    Freitag, 5. März 2010 17:49
  • 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"

    Montag, 8. März 2010 20:44
  • 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.
    Montag, 8. März 2010 23:44

  • I believe ASP.NET AJAX's TextBoxWatermark is what you're looking for, take a look at it's demo:
    TextBoxWatermark Demonstration(
    Cogito, ergo sum.
    Dienstag, 9. März 2010 02:01
  • Hi Chengli,

    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?

    Mittwoch, 10. März 2010 00:42

  • How about using JavaScript?
    You can set the default value to the control using the following syntax:

    var e = document.getElementById('id');
    e.value = 'preset value';

    Cogito, ergo sum.
    Mittwoch, 10. März 2010 01:21
  • Thanks for the reply.

    There are around 20 fields to prefill data. Using JS will make it infeasible. I look for a way to set the default value from server side.

    Mittwoch, 10. März 2010 16:19
  • It seems not possible with server side if you cannot use precreate option.

    Why JS will make it infeasible? You also need to handle the 20 fields in the server side, the same amount of work using JS.

    Cogito, ergo sum.
    Donnerstag, 11. März 2010 03:12
  • You may want to try this: 

    Set default value for lookup field


    Just make sure you have the correct field that you are looking at. (This is for code-behind, JavaScript is probably a way for you to do as well)



    Freitag, 19. November 2010 20:13
  • 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 controls instead of sharepoint control. For example use drop down list control instead of lookup field, textbox for single line of textbox etc.

    Freitag, 19. November 2010 22:03