[C#.NET][Infopath 2007] 如何在 Form Service 使用多選 / How to use Multiple Optional in Form Service
預設情況下Form Service不提供多選功能,但我們仍可利用重複表格加程式碼達到功能
Step1.建立Sharepoint清單(Cities),如下圖
Step2.建立Infopath資料欄位,Data欄位資料型態為RTF,其餘為String
Step3.利用Infopath連線到Cities清單
Step4.建立Infopath畫面如下
Step5.讀取次要資料庫到DataTable(或是陣列)
按鈕事件中加入以下
//讀取次要資料庫
//=================================================================
//Connect Name
string ConnectName = "Cities";
//次要資料庫路徑
string ReTablePath = "/dfs:myFields/dfs:dataFields/dfs:Cities";
//DataTable的名稱
string DataTableName = "CitiesTable";
//次要資料庫的欄位
string[] ItemVariable = new string[] { "@City" };
//DataTable的欄位
string[] ColumnName = new string[] { "City" };
//取得資訊後存入DataTable,當然不一定要用DataTable,只是我習慣用DataTable
DataTable myTable = GetSubDatabase(ConnectName, ReTablePath, DataTableName, ItemVariable, ColumnName);
副程式如下
//取得次要資料庫的Repeat Table
private DataTable GetSubDatabase(string ConnectName, string ReTablePath, string DataTableName, string[] ItemVarible, string[] ColumnName)
{
int LoopCon = ItemVarible.Length;
//取得次要資料庫
XPathNavigator SubData = DataSources[ConnectName].CreateNavigator();
XPathNodeIterator SubDataReTable = SubData.Select(ReTablePath, NamespaceManager);
//建立DataTable
DataTable myTable = CreateDataTable(DataTableName, ColumnName);
DataRow Rows = null;
//取出資料庫的值
while (SubDataReTable.MoveNext())
{
//新增Row
Rows = myTable.NewRow();
//取出資料
for (int i = 0; i < LoopCon; i++)
{
XPathNavigator XPath = SubDataReTable.Current.SelectSingleNode(ItemVarible[i], NamespaceManager);
string XPathString = string.Empty;
if (XPath != null)
XPathString = SubDataReTable.Current.SelectSingleNode(ItemVarible[i], NamespaceManager).Value;
//寫入Row
Rows[ColumnName[i]] = XPathString;
}
//寫入DataTable
myTable.Rows.Add(Rows);
}
//Check Database
int j = 0;
foreach (DataRow row in myTable.Rows)
{
foreach (DataColumn col in myTable.Columns)
{
Debug.WriteLine(j.ToString() + " . " + col.Caption + ": " + row[col].ToString());
j++;
}
}
return myTable;
}
//建立DataTable
private DataTable CreateDataTable(string TableName, string[] ColumnName)
{
//建立DataTable
DataTable myTable = new DataTable(TableName);
//定義Columns
foreach (string column in ColumnName)
{
myTable.Columns.Add(column);
}
return myTable;
}
Step6.將DataTable寫入重複表格
按鈕事件下加入以下:
//寫入主要資料庫(重複表格)
//===================================================================================
//主要資料庫路徑
string MainSection = "/my:myFields/my:Group";
//主要資料庫Repeat Table的路徑
string MainReTable = MainSection + "/my:Retable";
//主要資料庫的欄位
ItemVariable = new string[] { "my:Title", "my:Option" };
//DataTable的欄位
ColumnName = new string[] { "City" };
SetRepeatTable(myTable, MainSection, MainReTable, ItemVariable, ColumnName);
副程式如下:
//設定重複表格
public void SetRepeatTable(DataTable myTable, string MainSection, string MainReTable, string[] ItemVariable, string[] ColumnNames)
{
//取得主要資料庫
XPathNavigator MainData = MainDataSource.CreateNavigator();
XPathNavigator MainDataSection = MainData.SelectSingleNode(MainSection, NamespaceManager);
XPathNavigator MainDataReTable = MainDataSection.SelectSingleNode(MainReTable, NamespaceManager);
//刪除舊的Repeat Table
XPathNodeIterator AllData = MainDataSection.Select(MainReTable, NamespaceManager);
XPathNavigator FirstRow = MainDataSection.SelectSingleNode(MainReTable + "[1]", NamespaceManager);
XPathNavigator EndRow = MainDataSection.SelectSingleNode(MainReTable + "[" + Convert.ToString(AllData.Count) + "]", NamespaceManager);
FirstRow.DeleteRange(EndRow);
//取出DataTable
foreach (DataRow Row in myTable.Rows)
{
foreach (string Column in ColumnNames)
{
//寫入Infopath主要資料庫
MainDataReTable.SelectSingleNode(ItemVariable[0], NamespaceManager).SetValue(Row[Column].ToString());
MainDataReTable.SelectSingleNode(ItemVariable[1], NamespaceManager).SetValue("no");
MainDataSection.AppendChild(MainDataReTable);
}
}
}
Step7.利用Option事件,將重複表格讀取出來
if (e.Site.Value == "")
return;
XPathNavigator MainPath = MainDataSource.CreateNavigator();
XPathNodeIterator ParentXPath = MainPath.Select("/my:myFields/my:Group/my:Retable", NamespaceManager);
XPathNavigator RTF = MainPath.SelectSingleNode("/my:myFields/my:Data", NamespaceManager);
//清空
RTF.SetValue("");
string RTFData = string.Empty;
while (ParentXPath.MoveNext())
{
string title = ParentXPath.Current.SelectSingleNode("my:Title",NamespaceManager).Value;
string option = ParentXPath.Current.SelectSingleNode("my:Option",NamespaceManager).Value;
if (option=="yes")
{
RTFData += "<div xmlns=\"http://www.w3.org/1999/xhtml\">" + title + "</div>";
}
}
if (RTFData!="")
RTF.InnerXml = RTFData;
Step8.上傳到Sharepoint看是否可執行
操縱畫面如下
範例下載:MultipleOption.rar
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET