我正在使用Telerik的RadGrid和Calendar创建另一个用户控件。
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<table class="style1">
<tr>
<td>From</td>
<td>To</td>
</tr>
<tr>
<td><asp:Calendar ID="Calendar1" runat="server" SelectionMode="Day"></asp:Calendar></td>
<td><asp:Calendar ID="Calendar2" runat="server" SelectionMode="Day"></asp:Calendar></td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
<td><asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" /></td>
</tr>
</table>
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView CommandItemDisplay="Top"></MasterTableView>
</telerik:RadGrid>
我在代码隐藏中使用Linq:
Entities1 entities = new Entities1();
public static object DataSource = null;
protected void Page_Load(object sender, EventArgs e) {
if (DataSource == null) {
DataSource = (from entity in entities.nsc_moneytransaction
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
}).OrderByDescending(a => a.date);
}
BindData();
}
public void BindData() {
RadGrid1.DataSource = DataSource;
}
protected void btnSubmit_Click(object sender, EventArgs e) {
DateTime startdate = new DateTime();
DateTime enddatedate = new DateTime();
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
enddatedate = Calendar2.SelectedDate;
var queryDateRange = from entity in entities.nsc_moneytransaction
where DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
>= DateTime.Parse(startdate.ToShortDateString())
&& DateTime.Parse(entity.transaction_date.Value.ToShortDateString())
<= DateTime.Parse(enddatedate.ToShortDateString())
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = queryDateRange.OrderByDescending(a => a.date);
} else if (Calendar1.SelectedDate != null) {
startdate = Calendar1.SelectedDate;
var querySetDate = from entity in entities.nsc_moneytransaction
where entity.transaction_date.Value == startdate
select new {
date = entity.transaction_date.Value,
username = entity.username,
cashbalance = entity.cash_balance
};
DataSource = querySetDate.OrderByDescending(a => a.date); ;
}
BindData();
}
protected void btnClear_Click(object sender, EventArgs e) {
Calendar1.SelectedDates.Clear();
Calendar2.SelectedDates.Clear();
}
问题是,(1)当我点击提交按钮时。 RadGrid中的数据不会更改。 (2)我们如何检查Calendar控件中是否没有选择,因为即使我们没有从该日历中选择任何内容,也会设置日期(01/01/0001),因此 Calendar1.SelectedDate!= null 是不够的。 =(
感谢。
答案 0 :(得分:3)
将新数据源绑定到网格控件时,需要调用Rebind
让网格显示新数据。或者,你可以使用NeedDataSource
事件(在我看来更优雅的解决方案)
对于您的第二个问题,请尝试此操作(假设您使用的是ASP.NET日历,而不是Telerik DatePicker:
if(Calendar1.SelectedDate.Date == DateTime.MinValue) {
//no date selected
} else {
//date is selected
}