Hi,
I am working on a mvc web application and learn how to use crystal report in mvc. I found a article http://hasibulhaque.com/index.php/2011/crystal-report-asp-net-mvc/
Following the instruction in the article, I created a InvoiceController, InvoiceIndex view. When I click on Show Invoice in New Window button, It calls GenericInNewWin function.
The function contains ajax that requests ShowInvoice action in the InvoiceController. The action gets data and saves in Sessions, then ajax open a web form(ReportPage.aspx).
In the page_load of ReportPage.aspx, It renders the crystal report and display it. The followings are the codes. The problem is the ReportPage.aspx only displays the crystal report viewer and there is no report content. If I do not use window.open(), use Response.Redirect("~/Report/ReportPage.aspx"); then it works. So if it is pop up window, it doesn't work. But I need it poped up. How to solve the problem?
In InvoiceIndex view:
<script>
function GenericInNewWin() {
var oParam = { "FromDate": "", "ToDate": "" };
oParam.FromDate = $("#txtFromDate").val();
oParam.ToDate = $("#txtToDate").val();
$.ajax({
url: '../../Invoice/ShowInvoice',
data: JSON.stringify(oParam),
type: 'POST',
contentType: 'application/json;',
dataType: 'json',
success: function () {
window.open("../Report/ReportPage.aspx", 'mywindow', 'fullscreen=no, scrollbars=auto');
}
});
}
</script>
@using (Html.BeginForm("ShowInvoice", "Invoice"))
{
<input type="text" id="txtFromDate" name="txtFromDate" value="@DateTime.Now.ToShortDateString()" class="dtp" /> <label>To</label>
<input type="text" id="txtToDate" name="txtToDate" value="@DateTime.Now.ToShortDateString()" class="dtp"/>
<br />
<input type="button" value="Show Invoice in New Window" onclick="GenericInNewWin()"/>
}
In the InvoiceController:
[HttpPost]
public void ShowInvoice(string FromDate, string ToDate)
{
this.HttpContext.Session["ReportName"] = "invoice.rpt";
this.HttpContext.Session["rptFromDate"] = FromDate;
this.HttpContext.Session["rptToDate"] = ToDate;
this.HttpContext.Session["rptSource"] = "test";
}
In the ReportPage.aspx :
protected void Page_Load(object sender, EventArgs e)
{
string strReportName = System.Web.HttpContext.Current.Session["ReportName"].ToString();
string strFromDate = System.Web.HttpContext.Current.Session["rptFromDate"].ToString();
string strToDate = System.Web.HttpContext.Current.Session["rptToDate"].ToString();
var rptSource = System.Web.HttpContext.Current.Session["rptSource"];
ReportDocument rd = new ReportDocument();
string strRptPath = Server.MapPath("~/") + "Rpts//" + strReportName;
rd.Load(strRptPath);
rd.SetDataSource(rptSource);
rd.SetParameterValue("fromDate", strFromDate);
rd.SetParameterValue("toDate", strFromDate);
CrystalReportViewer1.ReportSource = rd;
Session["ReportName"] = "";
Session["rptFromDate"] = "";
Session["rptToDate"] = "";
Session["rptSource"] = "";
}