hBifTs

山自高兮水自深!當塵霧消散,唯事實留傳.荣辱不惊, 看庭前花开花落; 去留随意, 望天上云展云舒.

导航

Infopath Cookies(2)

Posted on 2005-05-27 22:35  hbiftsaa  阅读(2723)  评论(6编辑  收藏  举报
昨天发了, Infopath Cookies(1)  .今天再发:)

这个文章主要写的是关于Dropdownlist的数据绑定...

1,最简单的方法,从控件上拖一个DropDownList到界面上面,在其属性选择"手动输入列表框项",然后自己把其中的数据一个个的填写到下面的表格中...

上面的方法应该是用的最多的了吧..方便,想写什么就写什么.爽..
不过缺点就是静态的,不能动态进行修改.使用上有限制:(

2.同上,在属性中选择"在数据库,Web服务或文件中进行查找".通过这个方式,我们可以直接把WebService的结果集绑定到DropDownList里面..只要设置好其数据源和相应的项就OK了...

3.Infopath中使用其开发界面,基本上就上述两种方式了.但是如果绑定的数据不是来自于WS,或是数据就是存在于此Infopath表单中的呢?这个时候怎么去动态绑定数据呢?
我们先来生成一个直接绑定WebService的DropDownList表单.
WebService定义的代码(示例):
    public class Data
    
{
        
private string key;

        
public string Key
        
{
            
get
            
{
                
return key;
            }

            
set
            
{
                key 
= value;
            }

        }


        
private string values;

        
public string Values
        
{
            
get
            
{
                
return values;
            }

            
set
            
{
                values 
= value;
            }

        }
        
    }

        
[WebMethod]
        
public Data[] TransD()
        
{
            Data[] xx 
= new Data[2];
            
for(int i=0;i<xx.Length;i++)
            
{
                xx[i] 
= new Data();
                xx[i].Key 
= "key"+i;
                xx[i].Values 
= "values"+ i;
            }

            
return xx;
        }
在Infopath中添加一个辅助数据源(TransD).把数据绑定到DropDownList中去.Key为DropDownList的显示名称,Values为值...

这时,我们提取表单文件.提取后的表单文件中,我们可以看到若干个*.xsl文件.在上个文章中,我提到过,Infopath就是XML+XSLT+JScript.使用xml进行数据的存储, xslt进行数据的显示,JScript进行数据的交换.

于是....注意到我们的视图只有一个,默认的名字是View1.于是,我们用打开View1.xsl文件...
找到这样的一段:
    <select class="xdComboBox xdBehavior_Select" title="" style="WIDTH: 130px" size="1" xd:CtrlId="CTRL10" xd:xctname="DropDown" value="" xd:boundProp="value" xd:binding="my:field5" tabIndex="0">
                        
<xsl:attribute name="value">
                            
<xsl:value-of select="my:field5"/>
                        
</xsl:attribute>
                        
<xsl:choose>
                            
<xsl:when test="function-available('xdXDocument:GetDOM')">
                                
<option/>
                                
<xsl:variable name="val" select="my:field5"/>
                                
<xsl:for-each select="xdXDocument:GetDOM(&quot;TransD&quot;)/dfs:myFields/dfs:dataFields/tns:TransDResponse/tns:TransDResult/tns:Data">
                                    
<option>
                                        
<xsl:attribute name="value">
                                            
<xsl:value-of select="tns:Values"/>
                                        
</xsl:attribute>
                                        
<xsl:if test="$val=tns:Values">
                                            
<xsl:attribute name="selected">selected</xsl:attribute>
                                        
</xsl:if>
                                        
<xsl:value-of select="tns:Key"/>
                                    
</option>
                                
</xsl:for-each>
                            
</xsl:when>
                            
<xsl:otherwise>
                                
<option>
                                    
<xsl:value-of select="my:field5"/>
                                
</option>
                            
</xsl:otherwise>
                        
</xsl:choose>
                    
</select>

熟悉XML和XSLT语法的人应该一眼就可以看出来这个代码的作用了.
通过使用<xsl:for-each>把节点 XDocument.GetDOM('TransD').selectNodes("//dfs:myFields/dfs:dataFields/tns:TransDResponse/tns:TransDResult/tns:Data")  中的所有子节点显示出来.
其中的tns:Key是显示的文本,tns:Values为其中的值.

找到这里,就知道方法显示Infopath表单中的数据的方法了.修改<xsl:for-each>中的数据源为Infopath表单中相应的节点...
定义Infopath的数据源如下:
<my:Root >
    
<my:Lists>
        
<my:Key/>
        
<my:Value/>
    
</my:Lists>
        
</my:Root>
其中<my:Lists>在Infopath中定义为重复节...
然后修改上面的XSLT代码如下:
    <select class="xdComboBox xdBehavior_Select" title="" style="WIDTH: 130px" size="1" xd:CtrlId="CTRL9" xd:xctname="DropDown" value="" xd:boundProp="value" xd:binding="my:field5" tabIndex="0">
                        
<xsl:attribute name="value">
                            
<xsl:value-of select="my:field4"/>
                        
</xsl:attribute>
                        
<xsl:choose>
                            
<xsl:when test="function-available('xdXDocument:GetDOM')">
                                
<option/>
                                
<xsl:variable name="val" select="my:field5"/>
                                
<xsl:for-each select="//my:Root/my:Lists">
                                    
<option>
                                        
<xsl:attribute name="value">
                                            
<xsl:value-of select="my:Value"/>
                                        
</xsl:attribute>
                                        
<xsl:if test="$val=my:Value">
                                            
<xsl:attribute name="selected">selected</xsl:attribute>
                                        
</xsl:if>
                                        
<xsl:value-of select="my:Key"/>
                                    
</option>
                                
</xsl:for-each>
                            
</xsl:when>
                            
<xsl:otherwise>
                                
<option>
                                    
<xsl:value-of select="my:field5"/>
                                
</option>
                            
</xsl:otherwise>
                        
</xsl:choose>
                    
</select>

OK了,到这里.代码基本上就都完成了.现在只要把相应的数据填充进去就可以了..
下面给一个测试的JScript代码,在此表单中,加入一个Button.把此Button的操作脚本函数定义如下:
    var node = XDocument.DOM.selectSingleNode("//my:Root/my:Lists");
    node.selectSingleNode(
"./my:Key").text = "key1";
    node.selectSingleNode(
"./my:Value").text = "Value1";
    var target 
= node.cloneNode(true);
    
    target.selectSingleNode(
"./my:Key").text = "key2";
    target.selectSingleNode(
"./my:Value").text = "Value2";
    
    node.parentNode.insertBefore(target,node);
    
    XDocument.View.ForceUpdate();

搞定...