Asp.net页面传参数给siliverlight

    技术2022-05-19  26

    Asp.net页面传参数给siliverlight Posted on 2010-08-15 16:03 eric(宽田) 阅读(1) 评论(0) 编辑 收藏 所属分类: Asp.Net

    在Asp.net页面上增加siliverlight,现在需要传参数给siliverlight。下边列举一些从页面传参数给siliverLight和siliverLight中取参数的方法。

     Asp.net页面传参数给siliverlight方法有:  一、通过param传参数及siliverlight中得到参数方法  二、通过Url传参数及siliverlight中得到参数方法

     

     一、通过param传参数及siliverlight中得到参数方法

     1.1、传参数

      在siliverlight调用页面的object中增加<param name="initParams" value="<%=Parameter%>" />。在value中设置参数,多参数用逗号分隔,如varid=001,emcstype=1。这样参数就传到了siliverlight中。代码如下,此处设Parameter值为varid=001,emcstype=1。即我们传入两个参数varid和emcstype。

                 < object  data ="data:application/x-silverlight-2,"  type ="application/x-silverlight-2"                 width ="100%"  height ="100%" >                  < param  name ="source"  value ="../ClientBin/Iaddtech.Environmental.Web.Silverlight.xap"   />                  < param  name ="onError"  value ="onSilverlightError"   />                  < param  name ="background"  value ="white"   />                  < param  name ="minRuntimeVersion"  value ="3.0.40818.0"   />                  < param  name ="onLoad"  value ="siliverLoaded"   />                  < param  name ="autoUpgrade"  value ="true"   />                  < param  name ="Windowless"  value ="true"   />                 < param  name ="initParams"  value ="<%=Parameter%>"   />                 < href ="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0"  style ="text-decoration: none" >                      < img  src ="http://go.microsoft.com/fwlink/?LinkId=108181"  alt ="Get Microsoft Silverlight"                         style ="border-style: none"   />                  </ a >              </ object >

     

    1.2、在Siliverlight中得到传入的参数:

      可以在Siliverlight项目中的App.xaml中的Application_Startup事件中得到参数。如下边代码:

    private   void  Application_Startup( object  sender, StartupEventArgs e){             if  (e.InitParams.Count  >   1 )            {                 // 参数1                  string  variableID  =  e.InitParams[ " varid " ].ToString();                 // 参数2                  string  emcstype  =  e.InitParams[ " emcstype " ].ToString();            }}

    1.3、将得到参数传入到页面(userControl)方法

       1.3.1利资源

        在Siliverlight页面中将得到的参数放入资源中。在页面中调用资源得到参数。方法为首先在App的Application_Startup中将参数保存到资源中。 

    private   void  Application_Startup( object  sender, StartupEventArgs e){                if  (e.InitParams.Count  >   1 )                {                     // 参数1                      string  variableID  =  e.InitParams[ " varid " ].ToString();                     // 参数放入资源中                     App.Current.Resources.Add( " varid " , variableID);                     // 参数2                      string  emcstype  =  e.InitParams[ " emcstype " ].ToString();                     // 参数放入资源中                     App.Current.Resources.Add( " emcstype " , emcstype);                }                 // 设置启动页面,此处调用了Trend.xaml                  this .RootVisual  =   new  Trend();}

        在Trend(siliverlight userControl)页面中得到参数。

         public   partial   class  Trend : UserControl     {         public  Trend()         {             InitializeComponent();             if  (App.Current.Resources.Count > 0 )             {                 // 得到参数                  string  varidValue  =  App.Current.Resources[ " varid " ].ToString();                 string  emcstypeValue  =  App.Current.Resources[ " emcstype " ].ToString();             }         }     }

     

      1.3.2利用构造函数

         在Application_Startup通过页面的构造函数将参数传过去。

    // 得到参数并传参数到页面 private   void  Application_Startup( object  sender, StartupEventArgs e){                if  (e.InitParams.Count  >   1 )                {                     // 参数1                      string  variableID  =  e.InitParams[ " varid " ].ToString();                     // 参数放入资源中                     App.Current.Resources.Add( " varid " , variableID);                     // 参数2                      string  emcstype  =  e.InitParams[ " emcstype " ].ToString();                     // 参数放入资源中                     App.Current.Resources.Add( " emcstype " , emcstype);                }                 // 设置启动页面,调用Trend.xaml页面中的带参数的构造函数                 this .RootVisual  =   new Trend(variableID, emcstype);}

        在Trend(siliverlight userControl)页面中得到参数。

         // 得到参数      public   partial   class  Trend : UserControl    {         public  Trend( string  varid,  string  emcstype)        {            InitializeComponent();             // 得到参数              string  varidValue  =  varid;             string  emcstypeValue  =  emcstype;        }    }

     

      1.3.3利用公共属性或变量

         在App中建立公共变量或属性,此处为变量,其它页面调用App的属性或变量得到。如下边建立了ParameterList变量。

    public   partial   class  App : Application{         ///   <summary>          ///  参数列表         ///   </summary>          public   static  IDictionary < string string >  ParameterList  =   new  Dictionary < string string > ();         private   void  Application_Startup( object  sender, StartupEventArgs e)        {             if  (e.InitParams.Count  >   1 )            {                 // 参数1                 ParameterList  =  e.InitParams;            }        }}

      在Trend(siliverlight userControl)页面中得到参数。

         // 得到参数      public   partial   class  Trend : UserControl    {         public  Trend()        {            InitializeComponent();            App myApp =  App.Current  as App;             // 得到参数              string  varidValue  =  myApp.ParameterList[ " varid " ];             string  emcstypeValue  =  myApp.ParameterList[ " emcstype " ];        }    }

     

     二、通过Url传参数及siliverlight中得到参数方法

       除了以上传参数外,还可以通过url传参数方法。此方法和我们平常得到url参数差不多。

      当Url为:http://www.xxx.com?varid=001&emcstype=1时,在Trend(siliverlight userControl)页面中得到参数方法 如下:

    public   partial   class  Trend : UserControl{         public  Trend()        {            IDictionary< String, String >  paraList  = HtmlPage.Document.QueryString;             if  (paraList  ==   null   ||  paraList.Count  <   1   ||   ! paraList.ContainsKey( " varid " ||   ! paraList.ContainsKey( " emcstype " ))            {                ShowInfo( " 没有传入参数 " );                 return ;            }          // 得到参数              string  varidValue  =  paraList[ " varid " ];             string  emcstypeValue  =  paraList[ " emcstype " ];    }}

    最新回复(0)