Welcome to weblogs.com.pk Sign in | Join | Help

Passing Logged On User Name to Silverlight App!

This is one of the basic requirement of any LoB Silverlight application, and is bit tricky for new comers…

Silverlight controls can be embedded with either well established techniques; like the one used for Flash or bit more easily using ASP.NET controls, here is how you will be doing it…

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RealTimeBandwidthMonitor.ascx.cs"
    Inherits="Resellers.RealTimeBandwidthMonitor" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
    TagPrefix="asp" %>
    
<asp:Silverlight ID="Xaml1" runat="server" Source="/ClientBin/BandwidthMonitor.xap"
    Height="500" Width="640"
    MinimumVersion="2.0.31005.0" />

 

//Code behind of ASCX
public partial class RealTimeBandwidthMonitor : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
       this.Xaml1.InitParameters = "Key1=Val,LoggedOnUserName=" + this.Context.User.Identity.Name;
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
       //..
    }
}

 
//Code behind of App.xaml of your Silverlight app
public partial class App : Application
{
    public IDictionary<string, string> InitValues = null;
 
    public App()
    {
        this.Startup += this.Application_Startup;
    }
 
    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //.
        if (null != e.InitParams)
            this.InitValues = e.InitParams;
    }
}

Now that you have your init parameters in the Silverlight app; you can you it where you need; e-g

//Code behind of Page.xaml
public partial class Page : UserControl
{
    string loggedOnUserName = string.Empty;
 
    public Page()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
    }
 
    void Page_Loaded(object sender, RoutedEventArgs e)
    {
        this.TextBlockForLoggedOnUserName.Text = "Default Value";
 
        App ourApp = Application.Current as App;
        if (null != ourApp)
        {
            if (ourApp.InitValues.Keys.Contains("LoggedOnUserName"))
            {
                this.loggedOnUserName = ourApp.InitValues["LoggedOnUserName"];
                this.TextBlockForLoggedOnUserName.Text = this.loggedOnUserName;
            }
        }
    }
}

 

Happy Lighting up the web!

Published Thursday, March 5, 2009 10:46 AM by khurram
Filed under:

Comments

No Comments

New Comments to this post are disabled