开发者

ObjectDataSource Selecting method can't 'see' any other control's values

I don't even know how to state this clearly, and there's too much code to paste it all in here.

Let me start off with a general description and maybe it'll ring a bell. I have a DataGrid which uses an ObjectDataSource. The ObjectDataSource uses a SelectMethod that calls another method due to the fact that two datepickers are required to filter the results.

When the SelectMethod fires, though, the datepickers are always null.

A related issue is that a button is required to cause the ObjectDataSource to Select and use the two datepicker values which doesn't work at all. It's as if the ObjectDataSource doesn't consult the GridView for the starting index, etc.

If anyone knows of an example of something like this setup (GridView, Date controls, button, ObjectDataSource) that would be great.

Edit: Here's the code.

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

   <asp:Label ID="Label39" CssClass="txtSelect" runat="server" Text="Start Date" />
   <cc1:datepicker ID="startDatePicker" runat="server" Width="70px" PaneWidth="150px" SelectedDate="1/1/2000">
      <panetablestyle bordercolor="#707070" borderwidth="1px" borderstyle="Solid" />
      <paneheaderstyle backcolor="#0099FF" />
      <titlestyle forecolor="White" font-bold="true" />
      <nextprevmonthstyle forecolor="White" font-bold="true" />
      <nextprevyearstyle forecolor="#E0E0E0" font-bold="true" />
      <dayheaderstyle backcolor="#E8E8E8" />
      <todaystyle backcolor="#FFFFCC" forecolor="#000000" font-underline="false" bordercolor="#FFCC99" />
      <alternatemonthstyle backcolor="#F0F0F0" forecolor="#707070" font-underline="false" />
      <monthstyle backcolor="" forecolor="#000000" font-underline="false" />
   </cc1:datepicker>

   <asp:Label ID="Label5" CssClass="txtSelect" runat="server" Text="End Date" />
   <cc1:datepicker ID="endDatePicker" runat="server" Width="70px" PaneWidth="150px" SelectedDate="1/1/2020">
      <panetablestyle bordercolor="#707070" borderwidth="1px" borderstyle="Solid" />
      <paneheaderstyle backcolor="#0099FF" />
      <titlestyle forecolor="White" font-bold="true" />
      <nextprevmonthstyle forecolor="White" font-bold="true" />
      <nextprevyearstyle forecolor="#E0E0E0" font-bold="true" />
      <dayheaderstyle backcolor="#E8E8E8" />
      <todaystyle backcolor="#FFFFCC" forecolor="#000000" font-underline="false" bordercolor="#FFCC99" />
      <alternatemonthstyle backcolor="#F0F0F0" forecolor="#707070" font-underline="false" />
      <monthstyle backcolor="" forecolor="#000000" font-underline="false" />
   </cc1:datepicker>

   <asp:Button ID="RetrieveButton" runat="server" Text="Retrieve" OnClick="RetrieveButton_Click" />

   <asp:GridView ID="creditRateGridView" runat="server"
                 DataSourceID="creditRateObjectDataSource"
                 AllowPaging="true"
                 AllowSorting="true"
                 PageSize="10" 
                 Width="900"
                 AutoGenerateColumns="False" 
                 DataKeyNames="EFFECTIVE_DATE"
                 GridLines="Both"
                 EnableSortingAndPagingCallbacks="true">
      <Columns>
         <asp:BoundField   DataField="EFFECTIVE_DATE" HeaderText="Effective Date" 
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True" 
                           SortExpression="EFFECTIVE_DATE" DataFormatString="{0:d}" />
         <asp:BoundField   DataField="REFERENCE_DATE" HeaderText="Reference Date" 
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True" 
                           SortExpression="REFERENCE_DATE" DataFormatString="{0:d}" />
         <asp:BoundField   DataField="GROSS_CREDIT_USED_RATE" HeaderText="Credit Rate" 
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True" 
                           SortExpression="GROSS_CREDIT_USED_RATE" DataFormatString="{0:p}" />
         <asp:BoundField   DataField="ANNUALIZED_YIELD_ACTUAL_AVERAGE_RATE" HeaderText="Yield" 
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True"
                           SortExpression="ANNUALIZED_YIELD_ACTUAL_AVERAGE_RATE" DataFormatString="{0:p}" />
         <asp:BoundField   DataField="DURATION_USED_YEAR" HeaderText="Duration"
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True"
                           SortExpression="DURATION_USED_YEAR" DataFormatString="{0:n2}" />
         <asp:BoundField   DataField="BOOK_VALUE_USED_AMOUNT" HeaderText="Book Value"
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True"
                           SortExpression="BOOK_VALUE_USED_AMOUNT" DataFormatString="{0:c}" />
         <asp:BoundField   DataField="MARKET_VALUE_USED_AMOUNT" HeaderText="Market Value" 
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True"
                           SortExpression="MARKET_VALUE_USED_AMOUNT" DataFormatString="{0:c}" />
         <asp:BoundField   DataField="MV_BV_RATE" HeaderText="MV/BV Ratio"
                           ItemStyle-HorizontalAlign="Right" ReadOnly="True"
                           SortExpression="MV_BV_RATE" DataFormatString="{0:p}" />
      </开发者_运维百科Columns>
      <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" />
      <PagerStyle HorizontalAlign="Center" />
   </asp:GridView>

   <asp:ObjectDataSource ID="creditRateObjectDataSource" runat="server"
                         EnablePaging="true"
                         TypeName="AegonSVS.Restricted.CreditRateHistory"
                         SelectMethod="CreditRateSelectMethod" 
                         StartRowIndexParameterName="startRowIndex"
                         MaximumRowsParameterName="maximumRows"
                         SortParameterName="sortExpression"
                         SelectCountMethod="CreditRateSelectCountMethod" />

</asp:Content>


public partial class CreditRateHistory : System.Web.UI.Page
   {
      private const int contractId = 1;

      protected void Page_Load(object sender, EventArgs e)
      {
         Session.Add("startDateTime", startDatePicker.SelectedDate);
         Session.Add("endDateTime", endDatePicker.SelectedDate);
      }

      public DataTable CreditRateSelectMethod(int startRowIndex, int maximumRows, string sortExpression)
      {
         var startDateTime = (DateTime)Session["startDateTime"];
         var endDateTime = (DateTime)Session["endDateTime"];

         DataTable dataTable =  Sql.GetCreditRateHistoryPagedRecords(startDateTime,
                                                     endDateTime,
                                                     contractId,
                                                     startRowIndex,
                                                     maximumRows,
                                                     sortExpression,
                                                     null);
         return dataTable;
      }

      public int CreditRateSelectCountMethod()
      {
         var startDateTime = (DateTime)Session["startDateTime"];
         var endDateTime = (DateTime)Session["endDateTime"];

         return Sql.GetCreditRateRowCount(startDateTime,
                                          endDateTime,
                                          contractId,
                                          null);
      }

      protected void RetrieveButton_Click(object sender, EventArgs e)
      {
         // So something with this return? How?
         IEnumerable dataSource = creditRateObjectDataSource.Select();
         creditRateGridView.DataBind();
      }

   }


It sounds like you are expecting code in your ObjectDataSource to have access to your Page object and its children. That is not how it works generally speaking. The code in your ObjectDataSource needs to stand on its own, which means if you need to filter your SQL SELECT using a couple of dates, those dates should be sent as parameters.

If you can post your ObjectDataSource markup and the Class code for your DAL object, I can provide a specific example.

However the general answer to your question can be found in this article:

MSDN: Using Parameters with the ObjectDataSource Control


The problem with using the current Page Class for ObjectDataSource.TypeName is that every time ObjectDataSource binds, it creates a new instance of the Class to work with (with caveats, but that's not important here). This is why you cannot read control values but you can read session. This is why you can't rely on directly accessing items on your page from within your SelectMethod. You need to pass the query parameters in as parameters of the SelectMethod.

It looks like you already have an Class called Sql with (shared?) methods that are set up to do the data access. That is the class your creditRateObjectDataSource.TypeName should reference. Then have creditRateObjectDataSource.SelectMethod be sql.GetCreditRateHistoryPagedRecords, similar to this example:

<asp:ObjectDataSource ID="creditRateObjectDataSource" runat="server" EnablePaging="true"
    TypeName="AegonSVS.Restricted.sql" SelectMethod="GetCreditRateHistoryPagedRecords"
    StartRowIndexParameterName="startRowIndex" MaximumRowsParameterName="maximumRows"
    SortParameterName="sortExpression" SelectCountMethod="CreditRateSelectCountMethod">
    <SelectParameters>
        <asp:ControlParameter Name="startDateTime" ControlID="startDatePicker" PropertyName="SelectedDate"
            DbType="DateTime" />
        <asp:ControlParameter Name="endDateTime" ControlID="endDatePicker" PropertyName="SelectedDate"
            DbType="DateTime" />
        <asp:Parameter Name="contractId" DefaultValue="1" DbType="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜