Passing a selected Item to an aspx page

Category: visual studio lightswitch

Question

Joseph Serrano on Thu, 09 Oct 2014 22:46:32


Hello all,

First of all thanks to the entire LS community and all those articles publish showing us the capability of LS. I am coming across an issue where I try to pass a selected item to ASPX page. The ASPX page is having a RDLC report.Everything does work properly the first time. Soon as I selected another item, the report is re-generated and keep showing the data from the previous selected item.

LS Code:

myapp.BrowseRecordTypes.RecordTypeSPC_selectedItem_RecordTypeSPCID_postRender = function (element, contentItem) {
    // Write code here.


    var RecordTypeSPCID = contentItem.screen.RecordTypeSPC.selectedItem.RecordTypeSPCID.toString();
    
    var HtmlContent = $("<div/>").html("<object width='1600px' height='800px' data='../ReportAspx/SPCReport.aspx?RecordTypeSPCID=" + RecordTypeSPCID + "'/>");
    HtmlContent.appendTo($(element));


};

ASPX Code:

      

  protected void Page_Load(object sender, EventArgs e)
        {
            int RecordTypeSPCID = 0;
            if (String.IsNullOrEmpty(Request.QueryString["RecordTypeSPCID"]) == false )
            {

               RecordTypeSPCID = Int32.Parse(Request.QueryString["RecordTypeSPCID"]);
            }
            if (!IsPostBack)
            {
                ShowReport(RecordTypeSPCID);
            }
        }
Thank you all for all your help.



Replies

technetcolmar on Sat, 11 Oct 2014 09:51:41


Hi Joseph, remember that _postRender event is fired only once per element.

Try to rearrange your code to work inside a dataBind event.

contentItem.dataBind('value', function (newValue) {
      //do something
});

Joseph Serrano on Sat, 11 Oct 2014 13:51:30


Hi Marco, Thanks for your reply. I did it using a custom control as well without success ( render event), but I was still using the content item instead of databind. I will give it a try and let you know. Thanks,

technetcolmar on Sat, 11 Oct 2014 14:57:48


Joseph, both _render and _postRender events fire only once per element so if your problem arise after screen render is finished but when selectedItem changes, then dataBind can be the solution.

Let us know if you solve or if you need help.

Joseph Serrano on Wed, 15 Oct 2014 22:14:06


Hi Marco,

Thanks for all your help. So I got where my mistake is, I was reading online about data-binding, i found a really useful tuto, http://blogs.msdn.com/b/lightswitch/archive/2012/12/06/custom-controls-and-data-binding-in-the-lightswitch-html-client-joe-binder.aspx

But I still need some help, so when I click on the tab where the report is located, I need to bind the new value, correct? So the data-bind will allows me to reload the element when a value change?

I found this website too, http://www.c-sharpcorner.com/UploadFile/dbd951/how-data-binding-works-in-visual-studio-lightswitch-html-cli/ . it is a lot of information there as well.

Sorry for my beginners questions, I am new with the light switch HTML, I am still at the bottom of the learning curve.

technetcolmar on Thu, 16 Oct 2014 19:23:59


I usually put my report viewer inside a separate LS screen and I'm not very good with aspx and js...

Your aspx page should have a ScriptManager, so maybe you can call a static WebMethod on your page passing the selectedItem Id with an ajax call inside the dataBind event.

I think this should do the trick (but...)

contentItem.dataBind('value', function (newValue) {
   $.ajax({
            url: "../reports/ReportViewer.aspx/GetNeededParameter", type: "POST",
   data: JSON.stringify({ theId: newValue }),
   contentType: 'application/json; charset=utf-8',
   dataType: 'json',
   success: function (result) {
       //everything should be ok
   },
   error: function (xhr, status, p3, p4) {
       var err = "Error " + " " + status + " " + p3;
       if (xhr.responseText && xhr.responseText[0] == "{")
       err = JSON.parse(xhr.responseText).Message;
           alert(err);
       }
   });
});

you WebMethod and ScriptManager properties:

<WebMethod> _
Public Shared Function GetNeededParameter(theId As Integer) As Boolean
    Console.WriteLine("doing my report stuff")
    Return True
End Function


<asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="True"> 
        </asp:ScriptManager>

(...but) I'm not sure 100% this is the best way to go, never tested with a fully working test...

Joseph Serrano on Mon, 27 Oct 2014 22:06:37


Hello,

Marco, Never got a chance to get back to you on this, I just tried to implemented your solution and I have some errors. I guess I will just create a new LS page and pass the value as a parameter to the other page, instead of the tab. It will work better and the impact on my app is really small.

Thanks gain for all your time and help.

-Jo