I have been struggling with this for too long and the result is still not quite satisfying but it'll have to do for now.
I have a Web Control that renders an InfoPath form using the Microsoft.Office.InfoPath.Server.Controls.XmlFormView control and want the form to open with a specific View rather than the default one.
Browsing the web I found the following candidates:
- xmlformView.XmlForm.ViewInfos.SwitchView(...)
- xmlformView.XmlForm.ViewInfos.Initial = ...
Call one of these too early in the life cycle of your Control and the XmlForm instance is not yet available. Call them too late and the ViewInfos will not be changeable. I haven't really figured this one out yet...
I thought I had it when I was doing this in the Initialize event of the xmlformView instance, but no luck. Perhaps I'm doing something wrong ?
public class MyControl : WebControl
{
Microsoft.Office.InfoPath.Server.Controls.XmlFormView xmlFormView;
protected override void OnInit(EventArgs e)
{
xmlFormView = new XmlFormView();
xmlFormView.Initialize += new EventHandler(xmlFormView_Initialize);
}
protected void xmlFormView_Initialize(object sender, InitializeEventArgs e)
{
if (null != xmlFormView.XmlForm)
xmlFormView.XmlForm.ViewInfos.Initial = xmlFormView.XmlForm.ViewInfos["View 2"];
}
}
Anyway, I kinda gave up on this and tricked my way out using javascript:
"Write some javascript that runs on page load and triggers the 'switch view' javascript method."
I have implemented this in a custom control inheriting the XmlFormView. Although I don't like doing it this way it's here to download: XmlFormView.cs
And I'm still hoping for a better solution... ;)
References: