【发布时间】:2011-05-03 19:25:23
【问题描述】:
我什至不知道如何清楚地说明这一点,并且代码太多,无法将其全部粘贴在这里。
让我从一般性描述开始,也许它会响起警钟。我有一个使用 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();
}
}
【问题讨论】:
-
这已经完成了一半。我将日期选择器的值放在会话中(这看起来不正确,但现在还可以),如果单击两次按钮,则以下方法“有效”:protected void RetrieveButton_Click(object sender, EventArgs e) { / / 那么这个回报呢?如何? IEnumerable dataSource = creditRateObjectDataSource.Select(); creditRateGridView.DataBind(); }
-
要解决您需要点 ObjectDataSource 使用当前页面对象而不是创建新的问题。在这里查看我的答案:stackoverflow.com/a/25846301/451495
标签: asp.net gridview objectdatasource