Switch InfoPath form view based on field value

时间:2018-03-25 20:35:45

标签: c# forms sharepoint-2013 infopath

I have an InfoPath 2013 form I'm working on, and we have the need to switch views based on job category. We can easily determine this based on the job number at the top of the form, but a rule placed in "Form Load" isn't working because the job number field gets populated after the form loads in InfoPath Filler. Essentially I want to show View A if the job number starts with a "1", View B if it starts with a "2", and so on.

This form already has C# code behind it, but I'm banging my head on the desk trying to get this working. In theory, I should be able to use the Change event of the field to switch views, but so far it ignores any code I put in for the "After Change" context of the field.

I'm using the following to switch views:

this.ViewInfos.SwitchView("MyView");

Does anyone have an example of how this should work?

1 个答案:

答案 0 :(得分:0)

以下是有关InfoPath表单数据更改时切换视图的示例,如下拉列表更改事件,供您参考:

public void InternalStartup()
{          
    EventManager.FormEvents.ContextChanged += new ContextChangedEventHandler(FormEvents_ContextChanged);
    EventManager.XmlEvents["/my:DoanhNghieps/my:loaiHinhHoatDong"].Changed += new XmlChangedEventHandler(loaiHinhHoatDong_Changed);
}  

public void FormEvents_ContextChanged(object sender, ContextChangedEventArgs e)
{           
    if (canRedirect) 
    { 
        canRedirect = false; 
        ViewInfos.SwitchView("CN"); 
    }
}
private bool canRedirect = false;
public void loaiHinhHoatDong_Changed(object sender, XmlEventArgs e)
{
    // Write your code here to change the main data source.
    canRedirect = true;            
} 

更多信息:InfoPath: Drop-Down List Changed, Switch View