SharePoint 2010 Web controls - LookupField value not being set on postback - sharepoint

I've created a custom edit form as a simple aspx page in VS2010 (inherits from LayoutsPageBase) which uses the SharePoint Web controls LookupField control to display a drop down list of values from a custom type
The form displays correctly with the drop down box containing the expected range of values
The ControlMode is set to the same as the FormContext (though I have tried explicitly setting this to Edit)
But on the postback the value of the dropdown list is not set - the selected item index is set to -1
How can I correctly use the LookupField control to capture a selected value from the user?
Could it be because I'm adding the controls declaritivly in the aspx and then setting the list id etc from the SPContext in the page load event? - see code snippet below (not the prettiest but just trying to get it to work at this point):
from aspx:
<SharePoint:FileField ID="FileNameText" InputFieldLabel="Name" runat="server" ControlMode="Display"/><br />
<SharePoint:LookupField ID="FeedType" runat="server" />
<SharePoint:TextField ID="FeedStatus" runat="server" />
....
in the code behind page load:
if (!IsPostBack)
{
SPItem feedFileItem = SPContext.Current.Item;
FileNameText.ControlMode = SPContext.Current.FormContext.FormMode;
FileNameText.ListId = SPContext.Current.ListId;
FileNameText.ItemId = SPContext.Current.ItemId;
FileNameText.FieldName = "Name";
FeedType.ControlMode = SPControlMode.Edit;
FeedType.ListId = SPContext.Current.ListId;
FeedType.ItemId = SPContext.Current.ItemId;
FeedType.FieldName = "FeedType";
FeedStatus.ItemContext = SPContext.Current;
FeedStatus.RenderContext = SPContext.Current;
FeedStatus.ControlMode = SPControlMode.Edit;
FeedStatus.ListId = SPContext.Current.ListId;
FeedStatus.ItemId = SPContext.Current.ItemId;
FeedStatus.FieldName = "FeedStatus";
}
UPDATE
Ok I managed to get my form working by adding the controls in the code behind in the override of CreateChildControls - this is in line with the majority of the samples I've seen on the net.
But can someone explain why my approach didn't work and whether I can do this all in a declarative way in the aspx?

During postbacks selected values from lists are simply ignored if the list control isn't populated. So if you choose item 2 and the list items are null it will simply ignore the response parameter and not set the Value property. This is because ProcessPostData occurs prior to LoadData. Even if you were to remove the !IsPostBack on the LoadData method it still wouldn't work because ProcessPostData still occurs before LoadData and you didn't load the list prior to processing the postback.
A simple way to fix this is move your initialization code into the EnsureChildControls method of your Application page.
protected override void EnsureChildControls()
{
base.EnsureChildControls();
...
FeedType.ControlMode = SPControlMode.Edit;
FeedType.ListId = SPContext.Current.ListId;
FeedType.ItemId = SPContext.Current.ItemId;
FeedType.FieldName = "FeedType";
...
}

Related

dynamically adding PXTabItem from codebehind

Trying to add a new tab items from the page cs file in the Page_Init method which contains a PXSmartPanel in Iframe mode.
Initially I just created the blank tab
for (int i=0;i<3;i++)
{
PXTabItem tabItem = new PXTabItem();
tabItem.Key = "myKey"
tabItem.Text = "myTab" + i;
tabItem.Visible = true;
this.tab.Items.Add(tabItem);
PXSmartPanel smartPanel = new PXSmartPanel();
smartPanel.RenderIFrame = true;
smartPanel.InnerPageUrl = "thispage" + i;
..... {other smartpanel properties}
tabitem.TemplateContainer.Controls.Add(smartPanel);
}
This does create the tabs on my page and works/looks correctly in Chrome however in Firefox, the style of the iframe rendered is always 150px regardless of the style set in the smartPanel above.
Tried adding the following
smartPanel.height = new Unit(100, UnitType.Percentage);
smartPanel.Style["height"] = "100%";
Tried even forcing a specific size of say 600px.
As a secondary test, I added directly to my page the tab items, smart panels and those render correctly in both Chrome and Firefox.
I also attempted to create a "dummy" / "template" and use the tabitem.CopyFrom() method to set it up. This also renders correctly in chrome but not firefox.
Has anyone added tab items directly from the code behind file and have any hints on what property I am missing here?

How can I detect uiview is an activated viewport

I need to detect whether a Uiview is a standard opened view or if it is an activated viewport on a sheet. Querying the uiview’s view Id returns the Id of the activated viewport's view. I have found no direct way to detect that a uiview is actually a sheet with an activated viewport.
I am already tracking opened views in the view activated event for another purpose. So I considered storing the view Id with the uiview hashcode for later checking that it was indeed a sheetview prior to becoming an activated view. Unfortunately, and I think in opposition to standard use, the uiview hashcode is not stable. Multiple hashcode requests from the uiview object return different values.
Does anyone have a way to detect this condition? I need to be able to use the the methods on the uiview still. So any help to find the actual child windows I would like to relate to the uiview object. The view still says "Sheet: ..." in the title when a view is activated.
TaskDialog mainDialog = new TaskDialog("Hello, viewport check!");
mainDialog.MainInstruction = "Hello, viewport check!";
mainDialog.MainContent =
"Sadly Revit API doesn't automatically know if the user is in an active viewport. "
+ "Please click 'Yes' if your are, or 'No' if your not.";
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
"Yes, I am in an active viewport on a sheet.");
mainDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
"No, I am just in an ordinary view.");
mainDialog.CommonButtons = TaskDialogCommonButtons.Close;
mainDialog.DefaultButton = TaskDialogResult.Close;
TaskDialogResult tResult = mainDialog.Show();
bool YesOrNo = true;
if (TaskDialogResult.CommandLink1 == tResult)
{
YesOrNo = true;
}
else if (TaskDialogResult.CommandLink2 == tResult)
{
YesOrNo = false;
}
else{
return;
}
You can use the ViewSheet GetAllViewports method to determine all the viewports on a given sheet. Using that, you could put together a bi-directional dictionary lookup system map any sheet to all the viewports it hosts and vice versa. That should help solve your task. Here is some example usage:
http://thebuildingcoder.typepad.com/blog/2014/04/determining-the-size-and-location-of-viewports-on-a-sheet.html
Im late to the party - but another way to sense if the user is in a viewport is to investigate the Process.MainWindow title. Something like this (in RevitPythonShell):
import threading, clr
from System.Diagnostics import Process
# need winform libraries for feedback form only
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form, Label
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
ui = __revit__.ActiveUIDocument
def lookAtWindow(activeView):
# Looking for one of three conditions:
# 1. User is on a sheet (ActiveView will be DrawingSheet)
# 2. User is in an active ViewPort on a sheet (ActiveView will NOT be be DrawingSheet, but MainWindowTitle will contain " - [Sheet: " string)
# 3. User is on a View (neither of the previous two conditions)
result = False
if str(activeView.ViewType) == 'DrawingSheet':
result = 'Youre on a sheet'
else:
processes = list(Process.GetProcesses())
for process in processes:
window = process.MainWindowTitle
if window and 'Autodesk Revit '+app.VersionName[-4:] in window and ' - [Sheet: ' in window and ' - '+doc.Title+']' in window:
result = 'I reckon youre in a Viewport'
if not result:
result = 'so you must be in a '+str(activeView.ViewType)
form = Form()
form.Width = 300
form.Height = 100
label = Label()
label.Width = 280
label.Height = 70
label.Text = result
label.Parent = form
form.ShowDialog()
# need to close RevitPythonShell console before checking MainWindowTitle, so run on timer
threading.Timer(1, lookAtWindow, [ui.ActiveView]).start()
__window__.Close()

SharePoint Hiding fields in newform, dispform, editform

I have a task here when I need some assistance. what I am trying to accomplish is the follow..
Hide some fields on the newform, editform and dispform with in SharePoint (2013)
The field I am trying to hide is ONLY the input/textbox field not the whole column/heading associated with it. Basically I have a form with a heading and an associated textbox(single line of text) next to it, what I would like to do is hide the textbox only.
I have used the F12 IE tools to select the text box to which displays the following souce code:
<input title="Travel" class="ms-long ms-spellcheck-true" id="Travel_f6801fb9-c4ff-4109-acb9-f7dd63c1d98a_$TextField" type="text" maxlength="255" value="">
(the textbox is associated with my "Travel" column)
Now when I use the F12 tools while selecting this, I added some css(from that I can tell) under the "inline Style" top heading which was "display=none" and bingo it works!.
Now what I cant do here is add this to the forms permanently. I have tried to google this by adding a Content web part to the form and try some CSS/Java script but I simply do not have the skills in this area.. does this make sense?
examples:
any help would be great
Cheers!
In SharePoint 2013 was introduced Client Side Rendering (aka CSR) which is used for rendering list views, list forms and search results. For a more details follow SharePoint 2013 Client Side Rendering: List Forms article.
The following JavaScript template demonstrates how to hide field controls in List Form pages:
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
OnPostRender: hideFieldControls
});
});
function getFieldControlId(field){
return field.Name + '_' + field.Id + '_$' + field.Type + 'Field';
}
function hideFieldControl(field){
var fieldControlId = getFieldControlId(field);
var fieldControl = document.getElementById(fieldControlId);
fieldControl.style.display = "none";
}
function hideFieldControls(ctx){
var fieldNamesToHide = ['JobTitle','WorkPhone']; //<- set field names to hide here
if(fieldNamesToHide.indexOf(ctx.ListSchema.Field[0].Name) > -1) {
hideFieldControl(ctx.ListSchema.Field[0]);
}
}
How to apply changes
Open List Form page in edit mode
Add Script Editor web part on the page
Insert the specified JavaScript template by enclosing it using
script tag Note: specify field names to hide via fieldNamesToHide variable
Save page
Results
Pic 1. Original New Form page
Pic. 2 Customized New Form (field controls for Job Title and Business Phone are hidden)

Sharepoint Toolpart Event Not Firing

I have created a Sharepoint WebPart, and I have given it a custom ToolPart that includes a Grid (a Telerik RadGrid, to be exact, though that is rather irrelevant). I have populated the grid, and created a GridButtonColumn object to add to the grid:
protected override void CreateChildControls()
{
GridButtonColumn c = new GridButtonColumn();
c.ConfirmText = "Really Delete?";
c.ConfirmDialogType = GridConfirmDialogType.RadWindow;
c.ConfirmTitle = "Delete";
c.ButtonType = GridButtonColumnType.LinkButton;
c.Text = "Delete";
c.UniqueName = "DeleteColumn";
grid.Columns.Add(c);
// ...
grid.DeleteCommand += new GridCommandEventHandler(Grid_DeleteCommand);
}
The grid renders correctly - populated with data and with the delete button present.
Now, when I click any of the delete button, the Grid_DeleteCommand() event does not get triggered. However, when I add a random button outside of the grid, it's click event gets triggered:
Button b = new Button();
b.Text = "Hello World";
b.Click += new EventHandler(Button_Click);
I'm not able to debug on this installation of Sharepoint (or maybe I can, but attaching to the process hasn't allowed me to do so yet), so the method of both of those events is simply a redirection to Google. That is how I check to see if the events fire:
string AbsoluteUri ="http://www.google.com";
Page.Response.Redirect(AbsoluteUri);
The only difference I can see between the two is that, with the 'Delete' button, it is nested inside of a Grid control, whereas with the 'Hello World' button, there is no nesting.
How would I be able to have the Grid_DeleteCommand fire when I click the button in the grid?
Using the Telerik Grid control, you should specify button's CommandName in your code.
Adding this line should solve the problem:
c.CommandName = "Delete";

C# TableLayoutPanel replace control?

I was wondering if it was possible to replace one control in a TableLayoutPanel with another at runtime. I have a combo box and a button which are dynamically added to the TableLayoutPanel at runtime, and when the user selects an item in the combo box and hits the button, I'd like to replace the combobox with a label containing the text of the selected combo box item.
Basically, if I could simply remove the control and insert another at it's index, that would work for me. However I don't see an option like "splice" or "insert" on the Controls collection of the TableLayoutPanel, and I was wondering if there was a simple way to insert a control at a specific index. Thanks in advance.
Fixed this by populating a panel with the two controls I wanted to swap and putting that into the TableLayoutPanel. Then I set their visibility according to which I wanted to see at what time.
This is what I've been able to come up with for what I needed. It gets the position of the ComboBox and makes a new label using the selected value.
// Replaces a drop down menu with a label of the same value
private void lockDropMenu(ComboBox dropControl)
{
TableLayoutPanelCellPosition pos = myTable.GetCellPosition(dropControl);
Label lblValue = new Label();
myTable.Controls.Remove(dropControl);
if (dropControl.SelectedItem != null)
{
lblValue.Text = dropControl.SelectedItem.ToString();
lblValue.Font = lblValue.Font = dropControl.Font;
// Just my preferred formatting
lblValue.AutoSize = true;
lblValue.Dock = System.Windows.Forms.DockStyle.Fill;
lblValue.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
myTable.Controls.Add(lblValue, pos.Column, pos.Row);
}
}

Resources