Question

I have a custom list that I am using as a Frequently asked questions list for a number of subsites. I have 3 columns, Question, Answer and category. I want to make the question field readonly as well as the category field because these fields will be pre-populated.

Using the inline edit view of the ListViewWbPart, how can I set these fields to "readonly"? I tried setting the ControlMode to "display", but this affects all of my columns for some reason.

Also, how can I set the width of the textbox for each column in edit mode? I tried using style="width:250px;" in the SharePoint:FormField, but again, it affected the textboxes for all of the columns instead of only one.

Am I missing something?

JM

Was it helpful?

Solution 3

These are good suggestions, however, I managed to get the results I was looking for by using a DataViewWebPart in the default view rather than a ListViewWebpart. Apparently a DataViewWebpart is a lot more flexible when it comes to formatting sharepoint:formFields. For anyone else struggling with a ListViewWebPart, I recommend switching to a Dataview WebPart instead.

Here's how I did it: Open your site in Sharepoint designer 2010, Click Lists and Libraries, double click the list that you want to add a view for. Then in the "Views" box to the right, click new, name your new view and click ok. Now open the newly created view. you will need to hide the ListViewWebPart that is currently on the form (not sure why you wouldn't just delete it, but that's what I've heard). To hide the webpart, find the "isvisible" attribute (ctrl F) and set it to false. Now in the WYSIWYG split view, click below the ListViewWebPart. Now in the ribbon click the insert tab, click DataView and choose empty DataView. Now back to the WYSIWYG editor. Click the link to choose a datasource, and click the list that you want to attach to. Now drag in the columns that you would like to include in your view.

If you need inline editing as I did, click the options tab in the ribbon, click inline editing and choose the links that you would like to appear in your view (insert, edit or delete).

Now you should be able to make changes like I did for example, to set a column to read-only when in the edit mode, click on the DataviewWebPart and then click the design tab in the ribbon. On the far right under "Data View Preview, choose "Edit Template". now in the WYSiWYG editor, double click the field that needs to be read-only. This should take you right to the code that needs to be modified. In the Sharepoint:formfield tag, change the ControlMode attribute from "Edit" to "Display". now your field is read only.

If you want to change the width of the textbox in edit mode, follow the same proceedure to get to the field and then play with the DisplaySize attribute or if you know CSS, you could add a cssclass to the field and style it with an external stylesheet (search the web for info on the sharepoint:CssRegistration control).

OTHER TIPS

Have you tried setting ReadOnlyField property of SPField object to true?

This can be done with a simple PowerShell script or using SharePoint Console Application project.

PowerShell script sample:

$w = Get-SPWeb http://localhost
$l = $w.GetList("/Lists/QA");
$f = $l.Fields["Category"];
$f.ReadOnlyField = $true
$f.Update()
$f = $l.Fields["Question"];
$f.ReadOnlyField = $true
$f.Update()

And when you will pre-populate these fields, you can turn ReadOnlyField flag off programmatically, set the values, and then turn it on back again.

You could create 2 calculated columns and set their values to be the columns you wish to be readonly. Then show these calculated columns in your view instead of Question and Category.

e.g. Calculated column: DisplayQuestion, Formula: =[Question]

As far as the width of the input fields, you could create a custom new and edit form for your list. Then you can modify specific fields on your form. However, if you do that and subsequently add new columns to your list they will not show up on your custom form. Instead, you could use jQuery on your page to find the textbox and set its width:

<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
  $(function() {
    $("input[Title='Question']").css("width", "250px");
  });
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top