开发自己的SQL2005报表查看SharePoint WebPart(四)

    技术2022-05-11  53

      这一节,我们给Web part增加报表查询参数。为了演示,这里只增加两个查询参数:开始时间和结束时间。这两个参数都是日期型的数据,为了方便用户操作,我使用了client端的日历脚本来实现日期输入。

    与上一节相比,我们需要增加两部分工作

    第一步:构造日期输入控件

    我的页面布局大概是这样的:

    From [_txtStartDate] [_btnStartDate]To [_txtStartDate] [_btnEndDate][_cmdOK]  参数输入区报表显示区状态来栏

     为了加入参数,我在构造函数里创建了一组table,将控件分别加入到各单元格中。

    下面为代码

     

    protected string _reportServerURL = "" ; // = "http://ctc-bar:81/ReportServer/ReportService.asmx";

     

    protected string _reportPath = "" ; // = "/BARreports/EBCdetailList";

     

    // UI controls

     

     

     

     

     

     

     

     

     

     

    protected HtmlInputText _txtStartDate = BuildTextBox( "txtStartDate" ); protected HtmlButton _btnStartDate = new HtmlButton (); protected HtmlInputText _txtEndDate = BuildTextBox( "txtEndDate" ); protected HtmlButton _btnEndDate = new HtmlButton (); protected Button _cmdOK = new Button (); protected HtmlGenericControl ReportView = new HtmlGenericControl (); protected Label _lblStatus = new Label (); protected Label _lblError = new Label (); protected HtmlInputHidden ctrlPrefix = new HtmlInputHidden (); protected string canlander_JSP = "" ;

    #region

     

    {

    canlander_JSP = BARreportWebPart.Properties.

     

    initailize the web controls public BARreportWebPartClass() Resources .Canlander_JSP; //<input type=hidden id="ctrlPrefix" name="ctrlPrefix" value='<asp:Literal Runat=server ID=ctrlPrefix></asp:Literal>'>

    ctrlPrefix.ID =

    ctrlPrefix.Name =

    ctrlPrefix.Value =

    WebPart_Load();

    }

     

    "ctrlPrefix" ; "ctrlPrefix" ; "<asp:Literal Runat=server ID=ctrlPrefix></asp:Literal>" ; /// <summary>

     

    /// load the web part UI controls

     

    /// </summary>

     

    /// <param name="sender"></param>

     

    /// <param name="e"></param>

     

    {

     

    protected void WebPart_Load() // build up the table that is our UI

     

     

     

     

    Label ParameterName; TableRow tr; TableCell cell; //build the main table

     

    t2.ID =

    t2.CssClass =

    t2.Width =

    t2.Height =

     

    Table t2 = new Table (); "TblMain" ; "mainTable" ; new Unit (100, UnitType .Percentage); new Unit (100, UnitType .Percentage); //add the top table

    tr =

    tr.ID =

    t2.Rows.Add(tr);

    cell =

    cell.ID =

    t2.Rows[0].Cells.Add(cell);

    t2.Rows[0].Cells[0].VerticalAlign =

    t2.Rows[0].Cells[0].HorizontalAlign =

     

     

    new TableRow (); "TblMainRow1" ; new TableCell (); "TblMainRow1Cell1" ; VerticalAlign .Middle; HorizontalAlign .Left; //add the content table

    t2.Rows.Add(

    t2.Rows[1].Cells.Add(

    t2.Rows[1].Cells[0].VerticalAlign =

    t2.Rows[1].Cells[0].HorizontalAlign =

     

     

    new TableRow ()); new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Left; //add the bottom table

    t2.Rows.Add(

    t2.Rows[2].Cells.Add(

    t2.Rows[2].Cells[0].VerticalAlign =

    t2.Rows[2].Cells[0].HorizontalAlign =

     

    Controls.Add(t2);

     

    new TableRow ()); new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Left; /// build top table

     

    tblTop.ID =

    tblTop.Width =

    tblTop.Height =

    tr =

    tr.ID =

    tblTop.Rows.Add(tr);

    cell =

    cell.ID =

    tblTop.Rows[0].Cells.Add(

    tblTop.Rows[0].Cells[0].VerticalAlign =

    tblTop.Rows[0].Cells[0].HorizontalAlign =

    ParameterName =

    ParameterName.Text =

    _txtStartDate.Attributes.Add(

    _btnStartDate.ID =

    _btnStartDate.InnerText =

     

    Table tblTop = new Table (); "TblTop" ; new Unit (100, UnitType .Percentage); new Unit (100, UnitType .Percentage); new TableRow (); "TblTopRow1" ; new TableCell (); "TblTopRow1Cell1" ; new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Center; new Label (); "From" ; "ondblclick" , "show_cele_date(this,'','',this)" ); "btnStartDate" ; "Calendar" ; //_btnStartDate.Attributes.Add("onclick", "show_cele_date(this,'','',document.getElementById('" + ClientID_txtStartDate + "'))");

    tblTop.Rows[0].Cells[0].Controls.Add(ParameterName);

    tblTop.Rows[0].Cells[0].Controls.Add(_txtStartDate);

    tblTop.Rows[0].Cells[0].Controls.Add(_btnStartDate);

    tblTop.Rows[0].Cells.Add(

    tblTop.Rows[0].Cells[1].VerticalAlign =

    tblTop.Rows[0].Cells[1].HorizontalAlign =

    ParameterName =

    ParameterName.Text =

    _txtEndDate.Attributes.Add(

    _btnEndDate.InnerText =

    _btnEndDate.Attributes.Add(

    tblTop.Rows[0].Cells[1].Controls.Add(ParameterName);

    tblTop.Rows[0].Cells[1].Controls.Add(_txtEndDate);

    tblTop.Rows[0].Cells[1].Controls.Add(_btnEndDate);

    tblTop.Rows.Add(

    tblTop.Rows[1].Cells.Add(

    tblTop.Rows[1].Cells[0].VerticalAlign =

    tblTop.Rows[1].Cells[0].HorizontalAlign =

    _cmdOK.Text =

    tblTop.Rows[1].Cells[0].Controls.Add(_cmdOK);

    _cmdOK.Click +=

     

    new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Center; new Label (); "To" ; "ondblclick" , "show_cele_date(this,'','',this)" ); "Calendar" ; "onclick" , "show_cele_date(this,'','',GetObject('" + _txtEndDate.ClientID + "'))" ); new TableRow ()); new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Center; "View Report" ; new EventHandler (OnOKClick); //add the top table into table main

    t2.Rows[0].Cells[0].Controls.Add(tblTop);

     

     

    //build bottom table

     

    tblBottom.Width =

    tblBottom.Height =

    tblBottom.Rows.Add(

    tblBottom.Rows[0].Cells.Add(

    tblBottom.Rows[0].Cells[0].VerticalAlign =

    tblBottom.Rows[0].Cells[0].HorizontalAlign =

    _lblStatus.Text =

    tblBottom.Rows[0].Cells[0].Controls.Add(_lblStatus);

     

    Table tblBottom = new Table (); new Unit (100, UnitType .Percentage); new Unit (100, UnitType .Percentage); new TableRow ()); new TableCell ()); VerticalAlign .Middle; HorizontalAlign .Center; "" ; //add the bottom table into main table

    t2.Rows[2].Cells[0].Controls.Add(tblBottom);

     

    //build the content table

     

     

    Table tblContent = new Table (); //Step 1:

    tblContent.Rows.Add(

    tblContent.Rows[0].Cells.Add(

    ReportView.InnerText =

    tblContent.Rows[0].Cells[0].Controls.Add(ReportView);

     

    new TableRow ()); new TableCell ()); "report view region" ; //add the content table into main table

    t2.Rows[1].Cells[0].Controls.Add(tblContent);

     

    Controls.Add(ctrlPrefix);

    }

     

     

    /// <summary>

     

    /// Build a new textbox that has standard props.

     

    /// </summary>

     

    /// <param name="id"> The id for the textbox. </param>

     

    /// <returns> A TextBox </returns>

     

    {

     

    t.ID = id;

     

    }

    private static HtmlInputText BuildTextBox( string id) HtmlInputText t = new HtmlInputText (); return t;

    #endregion

    第二步:注册javascript脚本来实现日历输入。

    为了更加友好的用户操作,需要利用javascript来实现日期输入。javascript脚本很长,所以我把脚本保存在资源文件中,这样,对脚本的修改大大方便了。具体原理参见我的另一篇文章http://blog.csdn.net/yanwei100/archive/2007/03/15/1530528.aspx

    由于脚本是运行在一个自定义web控件中,所以需要解决ClientID的问题,具体方法参考我的另外一篇文章。

    下面是代码

    #region

     

    {

    RegisterJSP();

     

    regist Java script protected override void OnLoad( EventArgs e) // set ctrlPrefix's value

     

     

    string name = this .ClientID; string [] elm = ctrlPrefix.ClientID.Split( '_' ); //get an array of values

    ctrlPrefix.Value = ctrlPrefix.ClientID.Replace(elm[elm.Length - 1],

    "" ); //get the last element and replace from the id

     

    }

     

    {

     

    base .OnLoad(e); void RegisterJSP() //read jsp file from resource

     

     

     

    String csname1 = "CalendarScript" ; Type cstype = this .GetType(); // Get a ClientScriptManager reference from the Page class.

     

     

    ClientScriptManager cs = Page.ClientScript; // Check to see if the client script is already registered.

     

    {

    cs.RegisterClientScriptBlock(cstype, csname1, canlander_JSP,

    }

     

    if (!cs.IsClientScriptBlockRegistered(cstype, csname1)) false ); //add the getprefix function

     

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

    strbld.Append(

     

     

    {

    cs.RegisterClientScriptBlock(cstype, csname2, strbld.ToString(),

    }

    }

     

     

    {

     

     

     

    {

     

    {

    ClientID_txtStartDate = ctrl.ClientID;

    }

     

    {

    _btnStartDate = (

    }

    }

     

    {

    _btnStartDate.Attributes.Add(

    }

     

    }

    StringBuilder strbld = new StringBuilder (); "<script language ='javascript' type='text/javascript' >" ); "function getCrtlPrefix() {" ); "var prefix; " ); "var objCrtlPrefix = document.getElementById('" + ctrlPrefix.ClientID + "');" ); "if (objCrtlPrefix)" ); "prefix = objCrtlPrefix.value;" ); "return prefix;}" ); "function GetObject(ctrlName) {" ); "var prefix = getCrtlPrefix();" ); "var objCrtl = document.getElementById(prefix + ctrlName);" ); "if (objCrtl)" ); "return objCrtl;" ); " else " ); " alert('not found!');} " ); "</script>" ); string csname2 = "getCrtlPrefixScript" ; if (!cs.IsClientScriptBlockRegistered(cstype, csname2)) false ); protected override void OnPreRender( EventArgs e) string ClientID_txtStartDate= "" ; HtmlButton _btnStartDate = null ; foreach ( Control ctrl in Controls[0].Controls[0].Controls[0].Controls[0].Controls[0].Controls[0].Controls) if ( "txtStartDate" == ctrl.ID) if ( "btnStartDate" == ctrl.ID) HtmlButton )ctrl; if ( null != _btnStartDate) "onclick" , "show_cele_date(this,'','',document.getElementById('" + ClientID_txtStartDate + "'))" ); base .OnPreRender(e);

    #endregion

    最后,将用户输入的日期加入到web service的查询参数中:

     

    {

     

     

    ||

    {

    writer.Write(

     

    }

     

     

     

    ReportingServer.

    ReportingServer.

     

    {

    parameterValues =

     

    {

    parameterValues[i] =

    parameterValues[i].Name = parameters[i].Name;

    parameterValues[i].Label = parameters[i].Prompt;

    }

     

     

     

    protected override void Render(System.Web.UI. HtmlTextWriter writer) base .Render(writer); if ( string .Empty == ReportServerURL.Trim() || null == ReportServerURL string .Empty == ReportPath.Trim() || null == ReportPath) "please set the ReportServerURL, ReportPath" ); return ; string reportServerURL = ReportServerURL; string reportPath = ReportPath; ReportAdapter rsAdapter = new ReportAdapter (reportServerURL, reportPath); ReportParameter [] parameters = rsAdapter.GetParameters(); ParameterValue [] parameterValues = null ; if (parameters.Length > 0) new ReportingServer. ParameterValue [parameters.Length]; for ( int i = 0; i < parameters.Length; i++) new ReportingServer. ParameterValue (); DateTime startDate = new DateTime ( DateTime .Now.Year, DateTime .Now.Month,1); DateTime endDate = startDate.AddMonths(1); try

    {

    startDate =

    }

     

     

    DateTime .Parse(_txtStartDate.Value); catch {} try

    {

    endDate =

    }

     

    parameterValues[0].Value = startDate.ToString(

    DateTime .Parse(_txtEndDate.Value); catch {} "yyyy-MM-dd" ); //"2007-1-1";

    parameterValues[1].Value = endDate.ToString(

    "yyyy-MM-dd" ) ; //"2007-3-31";

    parameterValues[2].Value =

    parameterValues[3].Value =

    parameterValues[4].Value =

    }

    System.Text.

     

     

     

    "0,1,2,3,4,5,6,7,8" ; "Government,Enterprise,Partner,International" ; "Requested,Confirmed,Cancelled,Closed" ; Encoding enc = System.Text. Encoding .UTF8; byte [] result = rsAdapter.RenderReport(parameterValues); string htmlResult = enc.GetString(result); //htmlResult = htmlResult.Replace(reportServerURL.Replace("/ReportService.asmx", "?"), "http://" & Request("SERVER_NAME") & Request("SCRIPT_NAME") & "?Report=");

    writer.Write(htmlResult);

    }


    最新回复(0)