ObjectDataSource控件选择方法不能“看到”其他控件的值 [英] ObjectDataSource Selecting method can't 'see' any other control's values

查看:142
本文介绍了ObjectDataSource控件选择方法不能“看到”其他控件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我甚至不知道如何清楚地说明这一点,有太多code给它的所有粘贴在这里。

让我开始了与一般性描述,也许它会按门铃。我有一个使用一个ObjectDataSource一个DataGrid。 ObjectDataSource控件使用该调用的另一种方法,由于两个datepickers需要过滤结果的事实SelectMethod。

当SelectMethod火灾,虽然,datepickers总是空。

一个相关的问题是,一个按钮需要引起ObjectDataSource控件选择并使用根本不工作,这两个日期选择器值。这是因为如果ObjectDataSource控件不征询GridView控件的起始索引,等等。

如果任何人是这样的设置(GridView控件,日期控件按钮,ObjectDataSource控件),这将是伟大的例子都知道。

编辑:这里是code

 < ASP:内容ID =内容1ContentPlaceHolderID =ContentPlaceHolder1=服务器>   < ASP:标签ID =Label39的CssClass =txtSelect=服务器文本=开始日期/>
   < CC1:日期选择器ID =startDatePicker=服务器WIDTH =70像素PaneWidth =150像素SelectedDate =1/1/2000>
      < panetablestyle BORDERCOLOR =#707070边框宽度=1px的边框=固体/>
      < paneheaderstyle背景色=#0099FF/>
      < titlestyle前景色=白字体加粗=真/>
      <其次prevmonthstyle前景色=白字体加粗=真/>
      <其次prevyearstyle前景色=#E0E0E0字体加粗=真/>
      < dayheaderstyle背景色=#E8E8E8/>
      < todaystyle背景色=#FFFFCC前景色=#000000字体下划线=假BORDERCOLOR =#FFCC99/>
      < alternatemonthstyle背景色=#F0F0F0前景色=#707070字体下划线=FALSE/>
      < monthstyle背景色=前景色=#000000字体下划线=FALSE/>
   < / CC1:日期选择器>   < ASP:标签ID =Label5的CssClass =txtSelect=服务器文本=结束日期/>
   < CC1:日期选择器ID =endDatePicker=服务器WIDTH =70像素PaneWidth =150像素SelectedDate =2020年1月1日>
      < panetablestyle BORDERCOLOR =#707070边框宽度=1px的边框=固体/>
      < paneheaderstyle背景色=#0099FF/>
      < titlestyle前景色=白字体加粗=真/>
      <其次prevmonthstyle前景色=白字体加粗=真/>
      <其次prevyearstyle前景色=#E0E0E0字体加粗=真/>
      < dayheaderstyle背景色=#E8E8E8/>
      < todaystyle背景色=#FFFFCC前景色=#000000字体下划线=假BORDERCOLOR =#FFCC99/>
      < alternatemonthstyle背景色=#F0F0F0前景色=#707070字体下划线=FALSE/>
      < monthstyle背景色=前景色=#000000字体下划线=FALSE/>
   < / CC1:日期选择器>   < ASP:按钮的ID =RetrieveButton=服务器文本=检索的OnClick =RetrieveButton_Click/>   < ASP:GridView控件ID =creditRateGridView=服务器
                 的DataSourceID =creditRateObjectDataSource
                 AllowPaging =真
                 AllowSorting =真
                 每页=10
                 WIDTH =900
                 的AutoGenerateColumns =FALSE
                 的DataKeyNames =EFFECTIVE_DATE
                 网格线=两者
                 EnableSortingAndPagingCallbacks =真正的>
      <柱体和GT;
         < ASP:BoundField的数据字段=EFFECTIVE_DATE的HeaderText =生效日
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =EFFECTIVE_DATEDataFormatString ={0:D}/>
         < ASP:BoundField的数据字段=REFERENCE_DATE的HeaderText =基准日
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =REFERENCE_DATEDataFormatString ={0:D}/>
         < ASP:BoundField的数据字段=GROSS_CREDIT_USED_RATE的HeaderText =信用等级
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =GROSS_CREDIT_USED_RATEDataFormatString ={0:P}/>
         < ASP:BoundField的数据字段=ANNUALIZED_YIELD_ACTUAL_AVERAGE_RATE的HeaderText =产量
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =ANNUALIZED_YIELD_ACTUAL_AVERAGE_RATEDataFormatString ={0:P}/>
         < ASP:BoundField的数据字段=DURATION_USED_YEAR的HeaderText =持续时间
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =DURATION_USED_YEARDataFormatString ={0:N2}/>
         < ASP:BoundField的数据字段=BOOK_VALUE_USED_AMOUNT的HeaderText =账面价值
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =BOOK_VALUE_USED_AMOUNTDataFormatString ={0:C}/>
         < ASP:BoundField的数据字段=MARKET_VALUE_USED_AMOUNT的HeaderText =市场价值
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =MARKET_VALUE_USED_AMOUNTDataFormatString ={0:C}/>
         < ASP:BoundField的数据字段=MV_BV_RATE的HeaderText =MV / BV比率
                           ItemStyle-Horizo​​ntalAlign =右只读=真
                           SORTEX pression =MV_BV_RATEDataFormatString ={0:P}/>
      < /专栏>
      < PagerSettings模式=NumericFirstLastFirstPageText =第一LastPageText =最后/>
      < PagerStyle Horizo​​ntalAlign =中心/>
   < / ASP:GridView的>   < ASP:ObjectDataSource控件ID =creditRateObjectDataSource=服务器
                         EnablePaging =真
                         类型名=AegonSVS.Restricted.CreditRateHistory
                         SelectMethod =CreditRateSelectMethod
                         StartRowIndexParameterName =startRowIndex
                         MaximumRowsParameterName =maximumRows
                         SortParameterName =SORTEX pression
                         SelectCountMethod =CreditRateSelectCountMethod/>< / ASP:内容>
公共部分类CreditRateHistory:System.Web.UI.Page
   {
      私人const int的contractId = 1;      保护无效的Page_Load(对象发件人,EventArgs的发送)
      {
         Session.Add(的startDateTime,startDatePicker.SelectedDate);
         Session.Add(endDateTime,endDatePicker.SelectedDate);
      }      公开数据表CreditRateSelectMethod(INT startRowIndex,诠释maximumRows,串SORTEX pression)
      {
         VAR的startDateTime =(DateTime的)会议[的startDateTime];
         VAR endDateTime =(DateTime的)会议[endDateTime];         DataTable中的dataTable = Sql.GetCreditRateHistoryPagedRecords(的startDateTime,
                                                     endDateTime,
                                                     contractId,
                                                     startRowIndex,
                                                     maximumRows,
                                                     SORTEX pression,
                                                     空值);
         返回的dataTable;
      }      公众诠释CreditRateSelectCountMethod()
      {
         VAR的startDateTime =(DateTime的)会议[的startDateTime];
         VAR endDateTime =(DateTime的)会议[endDateTime];         返回Sql.GetCreditRateRowCount(的startDateTime,
                                          endDateTime,
                                          contractId,
                                          空值);
      }      保护无效RetrieveButton_Click(对象发件人,EventArgs的发送)
      {
         //因此,一些与此回报?怎么样?
         IEnumerable的数据源= creditRateObjectDataSource.Select();
         creditRateGridView.DataBind();
      }   }


解决方案

这听起来像你期待在code你的 ObjectDataSource控件来访问你的页面对象和它的孩子们。这不是它是如何一般来说。在ObjectDataSource控件的code需要站在自己的,这意味着如果你需要使用几个日期,这些日期应作为发送参数来筛选你的SQL SELECT。

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

但是你的问题最普遍的回答可以在本文中找到:

MSDN:使用参数与ObjectDataSource控件


使用当前 ObjectDataSource.TypeName 的问题是,每次 ObjectDataSource控件结合,它创建类的新实例一起工作(有警告,但在这里,这并不重要)。这就是为什么你不能读取控制值,但你可以阅读会话。这就是为什么你不能依靠直接访问你的页面上的项目从内部的 SelectMethod 。你需要传递查询参数作为参数的 SelectMethod

看起来你已经有一个名为 SQL中类与(共享?)被设置为进行数据访问方法。那是你的 creditRateObjectDataSource.TypeName 应引用类。然后让 creditRateObjectDataSource.SelectMethod sql.GetCreditRateHistoryPagedRecords ,类似这样的例子:

 < ASP:ObjectDataSource控件ID =creditRateObjectDataSource=服务器EnablePaging =真
    类型名=AegonSVS.Restricted.sqlSelectMethod =GetCreditRateHistoryPagedRecords
    StartRowIndexParameterName =startRowIndexMaximumRowsParameterName =maximumRows
    SortParameterName =SORTEX pressionSelectCountMethod =CreditRateSelectCountMethod>
    < SelectParameters>
        < ASP:ControlParameter名称=的startDateTime控件ID =startDatePicker属性名=SelectedDate
            的DbType =日期时间/>
        < ASP:ControlParameter NAME =endDateTime控件ID =endDatePicker属性名=SelectedDate
            的DbType =日期时间/>
        &所述; asp的:参数名称=contractId默认值=1的DbType =的Int32/>
    < / SelectParameters>
< / ASP:ObjectDataSource控件>

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>

这篇关于ObjectDataSource控件选择方法不能“看到”其他控件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆