熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ADO.NET 2.0 動態指定 ObjectDataSource 的 SelectMethod

2022-06-13   來源: .NET編程 

  效果如下圖指定要查詢的資料後再設定 ObjectDataSource 的 SelectMethod使用 ObjectDataSource 的好處是不用再自行撰寫分頁排序的code

  

  aspx的程式如下
    <body>
    <form id=form runat=server>
    <script type=text/javascript>
    //<![CDATA[
    function WebForm_OnSubmit() {
    $blockUI({ css: {
    border: none padding: px backgroundColor: # opacity: color: #fff
    webkitborderradius: px
    mozborderradius: px
    }
    })
    return true;
    }
    //]]>
    </script>
    <fieldset >
    <legend><font color=darkblue size=><b></b></font></legend>
    <table id=QryTable border= >
    <tr>
    <td class=TdTitle >查詢資料 </td>
    <td class=TdContent >
    <asp:DropDownList ID=QDDLReport AutoPostBack=true runat=server></asp:DropDownList>
    </td>
    <td class=TdTitle  id = QT runat=server>資料區間 </td>
    <td class=TdContent id = Q runat=server>
    <asp:TextBox ID=QtxtsDate runat=server Width=pt class=clscustxt></asp:TextBox> ~
    <asp:TextBox ID=QtxteDate runat=server Width=pt class=clscustxt></asp:TextBox>
    </td>
    <td>
    <asp:Button ID=btnQuery runat=server class=clsbtn Text=查 詢
    OnClientClick=WebForm_OnSubmit() onclick=btnQuery_Click/>
    </td>
    </tr>
    </table>
    </fieldset>
    <div id=div_count runat=server></div>
    <div >
    <asp:GridView ID=gvList runat=server AllowPaging=True AllowSorting=True AutoGenerateColumns=False BackColor=White
    CellPadding= DataSourceID=ObjectDataSource EnableModelValidation=True ForeColor=Black GridLines=None
    CellSpacing= CssClass=clsTable
    FontSize=pt Width=%
    PagerStyleCssClass=pgr ShowFooter=True
    onpageindexchanging=gvList_PageIndexChanging>
    <AlternatingRowStyle BackColor=#DDDDFF />
    <FooterStyle BackColor=#BBBBBB ForeColor=White HorizontalAlign=Center />
    <HeaderStyle BackColor=#BB FontBold=True ForeColor=White />
    <PagerSettings FirstPageImageUrl=~/Images/Arrow_Firstpng
    LastPageImageUrl=~/Images/Arrow_Endpng Mode=NumericFirstLast
    NextPageImageUrl=~/Images/Arrow_Nextpng PageButtonCount=
    Position=TopAndBottom PreviousPageImageUrl=~/Images/Arrow_Backpng />
    <PagerStyle BackColor=#FFDE ForeColor=Black HorizontalAlign=Right />
    <RowStyle BackColor=#FFDE />
    <SelectedRowStyle BackColor=#CEDA FontBold=True />
    </asp:GridView>
    </div>
    <asp:ObjectDataSource ID=ObjectDataSource runat=server OldValuesParameterFormatString=original_{}
    onselecting=ObjectDataSource_Selecting CacheDuration= EnableCaching=True>
    </asp:ObjectDataSource>
    <div >
    <asp:Button ID=btnToExcel runat=server class=clsbtn Text=匯出Excel
    onclick=btnToExcel_Click />
    </div>
    </div>
    <script type=text/javascript>
    $(document)ready(function () {
    $unblockUI()
    })
    </script>
    </form>
    </body>

後端程式如下
    protected void Page_Load(object sender EventArgs e)
    {
    if (!IsPostBack)
    {
    QDDLReportItemsAdd(A QueryData
    QDDLReportItemsAdd(B QueryData
    QDDLReportItemsAdd(C QueryData
    QtxtsDateText = DateTimeNowYearToString() + //;
    QtxteDateText = DateTimeNowToShortDateString()
    }
    else
    {
    //不同的報表有不同的SelectMethod
    if (QDDLReportSelectedValue == A Data
    {
    ObjectDataSourceSelectMethod = GetAQueryData;
    ObjectDataSourceTypeName = QueryDataBLL;
    }
    else if (QDDLReportSelectedValue == B Data
    {
    ObjectDataSourceSelectMethod = GetBQueryData;
    ObjectDataSourceTypeName = QueryDataBLL;
    }
    else if (QDDLReportSelectedValue == C Data
    {
    ObjectDataSourceSelectMethod = GetCQueryData;
    ObjectDataSourceTypeName = QueryDataBLL;
    }
    }
    }

  protected void ObjectDataSource_Selected(object sender ObjectDataSourceStatusEventArgs e)
    {
    SystemDataDataTable dt = eReturnValue as SystemDataDataTable;
    if (dt == null || dtRowsCount ==
    div_countInnerHtml = stringFormat(   查無資料!
    else
    div_countInnerHtml = stringFormat(   共 <span style=color:red;fontsize:pt;><b>{}</b></span> 筆資料 dtRowsCount)
    }
    protected void btnToExcel_Click(object sender EventArgs e)
    {
    if (gvListRowsCount ==
    return;
    ResponseClear()
    ResponseAddHeader(contentdisposition attachment;filename=ExportDataxls
    ResponseContentType = application/vndxls;
    SystemIOStringWriter sw = new SystemIOStringWriter()
    SystemWebUIHtmlTextWriter htw = new HtmlTextWriter(sw)
    //關閉換頁跟排序
    gvListAllowSorting = false;
    gvListAllowPaging = false;
    gvListDataBind()
    HtmlForm hf = new HtmlForm()
    ControlsAdd(hf)
    hfControlsAdd(gvList)
    hfRenderControl(htw)
    ResponseWrite(swToString())
    ResponseEnd()
    }
    protected void ObjectDataSource_Selecting(object sender ObjectDataSourceSelectingEventArgs e)
    {
    //首次進入頁面不Query
    if (!IsPostBack)
    eCancel = true;
    }
    protected void btnQuery_Click(object sender EventArgs e)
    {
    QueryDataBLL bll = new QueryDataBLL()
    ObjectDataSourceSelectParametersClear()
    if (QDDLReportSelectedValue == A Data
    {
    thiscreateGVColumns(bllGetAQueryDataSchema())
    //依Query的需求設定Parameter
    Parameter para = new Parameter(sdate
    paraDefaultValue = (QtxtsDateTextTrim() == ? // : QtxtsDateTextTrim())
    ObjectDataSourceSelectParametersAdd(para
    Parameter para = new Parameter(edate
    paraDefaultValue = (QtxteDateTextTrim() == ? // : QtxteDateTextTrim())
    ObjectDataSourceSelectParametersAdd(para
    }
    else if (QDDLReportSelectedValue == B Data
    {
    thiscreateGVColumns(bllGetBQueryDataSchema())
    //依Query的需求設定Parameter
    // 略
    }
    else if (QDDLReportSelectedValue == C Data
    {
    thiscreateGVColumns(bllGetCQueryDataSchema())
    //依Query的需求設定Parameter
    // 略
    }
    thisDataBind()
    }
    protected void gvList_PageIndexChanging(object sender GridViewPageEventArgs e)
    {
    ObjectDataSourceCacheDuration = ;
    }
    private void createGVColumns(SystemDataDataTable dt)
    {
    //依不同的查詢結果設定gridview欄位
    gvListColumnsClear()
    for (int i = ; i < dtColumnsCount; i++)
    {
    BoundField bf = new BoundField()
    bfDataField = dtColumns[i]ColumnNameToString()
    bfHeaderText = dtColumns[i]ColumnNameToString()
    gvListColumnsAdd(bf)
    }
    }
    QueryDataBLL 的 GetAQueryData 與 GetAQueryDataSchema 都是回傳 Datatable且 sql command 一樣只差在 GetAQueryDataSchema 的 sql command where 條件只下 where = 用來取回欄位資訊…


From:http://tw.wingwit.com/Article/program/net/201311/13280.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.