locked
Passing a parameter to SL app from a .aspx page RRS feed

  • Question

  • Hi

    When a user logs into my app, they can choose to go to the SL client or the HTML client.

    This all works well but I now need to know which of two buttons (I've just added the new button "btnReporter_ServerClick") a user clicks to go to the SL app.

    This is the code that navigates to either app:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Account_Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }
        protected void btnDesigner_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Designer");
        }
        protected void btnReporter_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Designer");
        }
        protected void btnAuditor_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Auditor");
        }
    }

    The new button is the "Reporter" button.

    I have code on opening the On "Created" event in the Start Screen in the SL app that needs to know which button the user clicked above.

    Does anyone know if/how this can be achieved.

    Thanks in advance, Mark.


    Mark


    • Edited by ITPSB Wednesday, June 18, 2014 2:57 AM typo
    Wednesday, June 18, 2014 2:26 AM

Answers

  • Why not just add a URL parameter to the redirect URL and then check that URL parameter from within the Silverlight app? See this article on how to check the URL parameter inside the Silverlight client: http://jewellambert.com/deep-linking-between-lightswitch-html-and-silverlight-clients/

    Regards, Xander. My Blog

    • Proposed as answer by Simon Jones [MSDL] Wednesday, June 25, 2014 7:54 AM
    • Marked as answer by Angie Xu Thursday, June 26, 2014 1:23 AM
    • Unmarked as answer by ITPSB Thursday, June 26, 2014 8:35 AM
    • Marked as answer by ITPSB Friday, June 27, 2014 3:34 AM
    Tuesday, June 24, 2014 1:42 AM

All replies

  • This all works well but I now need to know which of two buttons (I've just added the new button "btnReporter_ServerClick") a user clicks to go to the SL app.


    I think we can distinguish these two buttons with different button name.
    Monday, June 23, 2014 12:48 PM
  • Hi Peter

    Thanks for your feedback.

    I actually have given it a unique name "btnReporter_ServerClick" but how do I know which button caused the app to be launched?

    Thanks, Mark.


    Mark

    Monday, June 23, 2014 10:54 PM
  • Why not just add a URL parameter to the redirect URL and then check that URL parameter from within the Silverlight app? See this article on how to check the URL parameter inside the Silverlight client: http://jewellambert.com/deep-linking-between-lightswitch-html-and-silverlight-clients/

    Regards, Xander. My Blog

    • Proposed as answer by Simon Jones [MSDL] Wednesday, June 25, 2014 7:54 AM
    • Marked as answer by Angie Xu Thursday, June 26, 2014 1:23 AM
    • Unmarked as answer by ITPSB Thursday, June 26, 2014 8:35 AM
    • Marked as answer by ITPSB Friday, June 27, 2014 3:34 AM
    Tuesday, June 24, 2014 1:42 AM
  • Hi Xander

    Many thanks.

    That's what I was looking for and thanks for the information.

    I'll try that method out today and update the topic then.

    Best regards, Mark.


    Mark

    Tuesday, June 24, 2014 1:48 AM
  • Hi Simon

    I may be missing something here. Please assist (again ;-) thanks.

    Following Jewel Lambert's suggestion, in the InitializeDataWorkspace method of the Silverlight client Home screen, I try to add the suggested line:

    QueryStringHelper.TryGetValue("screen", out screenName);

    However, VS2013 gives an error and investigating I found this topic:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/a329c1e6-9f01-4d92-996e-f1aee412a4d9/sl-client-cant-resolve-querystringhelper-after-march-2014-upgrade?forum=lightswitch

    where Justin advises that since the March 2014 release "You can parse the query string from the uri yourself:"

    However, when I try to use the suggested code line:

    uri = HtmlPage.Document.DocumentUri;

    I get an error in debug mode "This operation can only occur on the UI Thread."

    Your assistance will be greatly appreciated.

    Thanks, Mark.


    Mark

    Thursday, June 26, 2014 8:44 AM
  • Essentially, you have to wrap the code into a Dispatchers.Main.Invoke(() => {}) block. Also, QuerystringHelper does not seem to be available anymore, so you need a way to parse the qyery parameters from the Uri, the following working example is the best way I could find to do it:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Windows.Browser;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation.Extensions;
    using Microsoft.LightSwitch.Threading;

    public partial class SearchOrders { private Dictionary<string, string> _queryParams = null; partial void SearchOrders_InitializeDataWorkspace(List<IDataService> saveChangesTo) { Dispatchers.Main.Invoke(() => { Uri uri = HtmlPage.Document.DocumentUri; string queryString = uri.Query; _queryParams = GetParams(queryString); }); } // static query string parameter parser static Dictionary<string, string> GetParams(string uri) { var matches = Regex.Matches(uri, @"[\?&](([^&=]+)=([^&=#]*))"); return matches.Cast<Match>().ToDictionary( m => Uri.UnescapeDataString(m.Groups[2].Value), m => Uri.UnescapeDataString(m.Groups[3].Value) ); } partial void ShowQueryParam_Execute() { this.ShowMessageBox("Parameter: " + _queryParams["source"]); } }


    Clicking the ShowQueryParam button will display the value for the "source" parameter, e.g.:

    http://localhost:5252/DesktopClient/default.htm?IsLaunchedByVS=True&AuthenticationType=None&source=ButtonOne

    Hope this helps.

    EDIT: btw, the GetParams() static method is from here: Get params from an URL C# code


    Regards, Xander. My Blog


    • Edited by novascape Thursday, June 26, 2014 9:44 AM
    Thursday, June 26, 2014 9:40 AM
  • Hi Xander

    Firstly, a big Thank You for assisting. There is no way I could have figured this out.

    Please check my implementation of your suggested code. I must be missing something.

    In the top screen-shot you'll notice that I get a compilation error.

    In the bottom screen-shot I attempted to move the line that displays the message but I then receive the run-time error that you can see.

    Please assist and many thanks, Mark.

    https://dl.dropboxusercontent.com/u/10788935/Picture1.png


    Mark


    • Edited by ITPSB Friday, June 27, 2014 2:06 AM Image
    Friday, June 27, 2014 2:04 AM
  • Hi Mark,

    No problem, glad to help out. You would get that exception when it cannot find the "source" URL query parameter and the "source" key does not exist in the dictionary. Also note that the query parameter might be case sensitive. Make sure that your URL has the following somewhere within the URL: "...&source=ButtonOne".

    If you want to make the code robust so it will handle cases where the "source" query parameter was not specified then you could do the following to default the value:

    partial void ShowQueryParam_Execute()
    {
        // set a default value for sourceParam
        var sourceParam = "SomeDefaultValue";
        // was the "source" query parameter specified - if so use the specified value?
        if (_queryParams.ContainsKey("source"))
             sourceParam = _queryParams["source"];
        // display the value
        this.ShowMessageBox("Parameter: " + sourceParam);
    }
    

    Hope that helps.


    Regards, Xander. My Blog

    Friday, June 27, 2014 2:14 AM
  • Just to be clear:

    1.  You get the compile error on ShowQueryParam_Execute() as that method  in my example is declared as "partial" since it is hooked up to a button on screen. You can add that code to another button on your screen or as you've done in your second screenshot.

    2.  The runtime error is explained in my previous post above.



    Regards, Xander. My Blog

    Friday, June 27, 2014 2:18 AM
  • Woo Hooo! Thanks Xander. I've got it to work.

    Many thanks indeed.

    All the best, Mark.


    Mark

    Friday, June 27, 2014 3:35 AM
  • Hi

    Thanks for the additional information.

    Best regards.


    Mark

    Friday, June 27, 2014 5:21 AM