ObjectDataSource 选择方法无法“看到”任何其他控件的值
我什至不知道如何清楚地表达这一点,而且代码太多,无法全部粘贴到这里。
让我从一般性描述开始,也许它会敲响警钟。我有一个使用 ObjectDataSource 的 DataGrid。由于需要两个日期选择器来过滤结果,ObjectDataSource 使用调用另一个方法的 SelectMethod。
但是,当 SelectMethod 触发时,日期选择器始终为空。
一个相关的问题是,需要一个按钮来使 ObjectDataSource 选择并使用两个日期选择器值,但这根本不起作用。就好像 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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您希望
ObjectDataSource
中的代码能够访问您的 Page 对象及其子对象。一般来说,它不是这样工作的。 ObjectDataSource 中的代码需要独立,这意味着如果您需要使用几个日期过滤 SQL SELECT,则这些日期应作为参数发送。如果您可以发布您的 ObjectDataSource 标记和 DAL 对象的类代码,我可以提供一个具体的示例。
但是,您的问题的一般答案可以在本文中找到:
MSDN:使用参数ObjectDataSource 控件
使用当前
Page
类作为ObjectDataSource.TypeName
的问题是,每次ObjectDataSource
绑定时,它都会创建一个的新实例要使用的类(有警告,但这在这里并不重要)。这就是为什么您无法读取控制值但可以读取会话的原因。这就是为什么您不能依赖从SelectMethod
中直接访问页面上的项目。您需要将查询参数作为SelectMethod
的参数传递。看起来您已经有一个名为
Sql
的类,其中包含设置为进行数据访问的(共享?)方法。这是您的creditRateObjectDataSource.TypeName
应该引用的类。然后将 CreditRateObjectDataSource.SelectMethod 设为 sql.GetCreditRateHistoryPagedRecords,类似于此示例: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 forObjectDataSource.TypeName
is that every timeObjectDataSource
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 yourSelectMethod
. You need to pass the query parameters in as parameters of theSelectMethod
.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 yourcreditRateObjectDataSource.TypeName
should reference. Then havecreditRateObjectDataSource.SelectMethod
besql.GetCreditRateHistoryPagedRecords
, similar to this example: