ObjectDataSource选择方法不能“看到”任何其他控件的值

时间:2011-05-03 19:25:23

标签: asp.net gridview objectdatasource

我甚至不知道如何清楚地说明这一点,并且有太多的代码可以将它全部粘贴在这里。

让我从一般描述开始,也许它会响铃。我有一个使用ObjectDataSource的DataGrid。 ObjectDataSource使用一个调用另一个方法的SelectMethod,因为需要两个日期选择器来过滤结果。

但是,当SelectMethod触发时,日期选择器始终为空。

相关问题是需要一个按钮才能使ObjectDataSource选择并使用两个完全不起作用的datepicker值。就好像ObjectDataSource没有向GridView查询起始索引等

如果有人知道这样的设置(GridView,日期控件,按钮,ObjectDataSource)之类的例子会很棒。

编辑:这是代码。

<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();
      }

   }

1 个答案:

答案 0 :(得分:4)

听起来您希望ObjectDataSource中的代码可以访问您的Page对象及其子对象。这不是一般的说法。 ObjectDataSource中的代码需要独立存在,这意味着如果您需要使用几个日期过滤SQL SELECT,那么这些日期应该作为参数发送。

如果你可以发布你的ObjectDataSource标记和DAL对象的类代码,我可以提供一个具体的例子。

但是,您可以在本文中找到对您的问题的一般答案:

MSDN: Using Parameters with the ObjectDataSource Control


Page使用当前ObjectDataSource.TypeName类的问题是,每次ObjectDataSource绑定时,它都会创建要使用的类的新实例(有警告,但不是这里很重要)。这就是您无法读取控制值但可以读取会话的原因。这就是为什么您不能依赖于SelectMethod内直接访问网页上的项目的原因。您需要将查询参数作为SelectMethod

的参数传递

看起来您已经有一个名为Sql的类,其中包含(共享?)方法,用于执行数据访问。这是您的creditRateObjectDataSource.TypeName应该引用的课程。然后creditRateObjectDataSource.SelectMethodsql.GetCreditRateHistoryPagedRecords,类似于此示例:

<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>