InfoPath 2010

在InfoPath 2010里面可以创建2种不同类型的表单,他们分别是:List和Form Library。

一。List,不需要写代码,可以应用于一些简单的表单。可以处理一些比较简单的list类型控件,例如:dropdownlist, listbox, checkboxlist。加上一些规则还可以实现多个list控件直接的联动。由于比较简单,这里不做详细介绍。

二。Form Library,需要写代码。当表单中有一些比较复杂的控件,例如:Repeater, 多个view的转换,这个时候Form Library就用的上了。当然,完全可以用webpart来实现表单。

  1. 简单控件初始化赋值

  当要New一个Item时候,很多情况下都必须对某些控件进行初始化赋值,例如:Creator,当然可以用规则的Username()来设置,可是Username()只是显示登录名,如果我要显示Name怎么办呢?这时候就需要在FormEvents_Loading事件里面写代码了。

      public void FormEvents_Loading(object sender, LoadingEventArgs e)

        {

            if (this.New)

            {

                string userName = SPContext.Current.Web.CurrentUser.Name;

                CreateNavigator().SelectSingleNode("/my:myFields/my:Creator", this.NamespaceManager).SetValue(userName);

            }

        }

  2. Repeater初始化赋值

  Repeater通常是和一个Group类型的属性绑定的,通常group的结构都是这样的:"/my:myFields/my:SponsorGroup/my:Sponsors"。看以下代码:

        public void FormEvents_Loading(object sender, LoadingEventArgs e)

        {

            int count = 0;



            foreach (SPListItem sponsor in Sponsors)

            {



                try

                {

                    XPathNavigator group = CreateNavigator().SelectSingleNode("/my:myFields/my:SponsorGroup/my:Sponsors", NamespaceManager);

                    XPathNavigator NewRow;

                    if (count == 0)

                    {

                        count = count + 1;

                        NewRow = group;

                    }

                    else

                    {

                        XPathNavigator NewRow = group.Clone();

                        NewRow.InsertAfter(group);

                    }

                    NewRow.SelectSingleNode("my:Milestones", NamespaceManager).SetValue(string.Empty);

                    NewRow.SelectSingleNode("my:BaselineDate", NamespaceManager).SetValue(string.Empty);

                    NewRow.SelectSingleNode("my:ApprovedDate", NamespaceManager).SetValue(string.Empty);

                    NewRow.SelectSingleNode("my:ActualDate", NamespaceManager).SetValue(string.Empty);

                    NewRow.SelectSingleNode("my:EstimatedDate", NamespaceManager).SetValue(string.Empty);

                    NewRow.SelectSingleNode("my:Status", NamespaceManager).SetValue(string.Empty);



                }

                catch

                {

                }

            }

        }

 3. FormEvents_Submit事件。

  当提交的时候,可以把Form Library保存到具体的某个地方。(/_layouts/FormServer.aspx?XmlLocation=/Sponsors/filename.xml&Source=/&DefaultItemOpen=1)

        public void FormEvents_Submit(object sender, SubmitEventArgs e)

        {

            FileSubmitConnection fscMain = null;

            fscMain = (FileSubmitConnection)this.DataConnections["Submit"];

            fscMain.Filename.SetStringValue("filename.xml");

            fscMain.FolderUrl = SPContext.Current.Web.Url + "Sponsors";

            fscMain.Execute();

            e.CancelableArgs.Cancel = false;

        }

  4. 数据源的管理

  通常数据源都是从某个list那获取的,其实也可以通过代码去设置。首先添加一个数据源,数据源的XML结构和相关代码如下:

<?xml version="1.0" encoding="UTF-8" ?>

<options>

	<option><value/><displayname/></option>

	<option><value/><displayname/></option>

</options> 

   private void BindDropDown(string dsName, string listName)

        {

            try

            {

                DataSource ds = DataSources[dsName];



                if (ds != null)

                {

                    ds.QueryConnection.Execute();



                    SPWeb web = SPContext.Current.Site.RootWeb;

                    SPList list = web.Lists[listName];



                    foreach (SPListItem item in list.Items)

                    {

                        if (item["Item"] != null)

                        {

                            string value = item["Item"].ToString();

                            AddItem(ds, value, value);

                        }

                    }



                    for (int j = 0; j < 2; j++)

                    {

                        RemoveInitialItem(ds);

                    }

                }

            }

            catch (Exception ex)

            {

            }

        }



        private void AddItem(DataSource ds, string displayName, string value)

        {

            XPathNavigator nodes = ds.CreateNavigator().SelectSingleNode("//options", NamespaceManager);

            XPathNavigator node = nodes.CreateNavigator().SelectSingleNode("//option", NamespaceManager).Clone();



            node.SelectSingleNode("displayname").SetValue(displayName);

            node.SelectSingleNode("value").SetValue(value);

            nodes.AppendChild(node);

        }



        private void RemoveInitialItem(DataSource ds)

        {

            XPathNavigator nodes = ds.CreateNavigator().SelectSingleNode("//options", NamespaceManager);

            XPathNavigator node = nodes.CreateNavigator().SelectSingleNode("//option", NamespaceManager);

            node.DeleteSelf();

        }

你可能感兴趣的:(Path)