我正在向gridview添加过滤器。其中一列是日期字段,我有2个文本框,因此用户可以输入范围。用户输入第二个日期后,将触发事件。
但是,我无法访问第一个文本框中的数据。我收到一个错误,表示该对象尚未设置。
这是标记:
asp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen">
<HeaderTemplate>
<asp:LinkButton runat="server" ID="lbOpenDate" Text="Open Date" CommandName="Sort" CommandArgument="OpenDate"></asp:LinkButton>
<asp:TextBox runat="server" ID="txtBoxOpenDateFilterStart" CssClass="datepick"></asp:TextBox>
<asp:Label runat="server" ID="lblOpenDateFilter" Text="To"></asp:Label>
<asp:TextBox runat="server" ID="txtBoxOpenDateFilterEnd" AutoPostBack="true" OnTextChanged="txtBoxFilter_TextChanged" CssClass="datepick"></asp:TextBox>
</HeaderTemplate>
这是OnTextChanged事件:
protected void txtBoxFilter_TextChanged(object sender, EventArgs e)
{
try
{
if (sender is TextBox)
{
populateSectionGrid();
DataTable dtSectionGridData = SectionGridView.DataSource as DataTable;
Nullable<DateTime> tdtStartDate;
Nullable<DateTime> tdtEndDate;
if (dtSectionGridData != null)
{
TextBox txtBox = (TextBox)sender;
if (txtBox.ID.Equals("txtBoxOpenDateFilterEnd"))
{
SectionGridViewFilterExpression = string.Empty;
//Get the start date
string tstrStartDate = (SectionGridView.FindControl("txtBoxOpenDateFilterStart") as TextBox).Text; \\This is the problem. This Control is returned as not set even though there is a date in the text box.
if (!String.IsNullOrWhiteSpace(tstrStartDate))
{
tdtStartDate = setStartTime(tstrStartDate);
tstrStartDate = "RegistrationOpen >= #" + tdtStartDate + "#";
}
//Get the end date
string tstrEndDate = string.Empty;
if (!String.IsNullOrWhiteSpace(txtBox.Text))
{
tdtEndDate = setStartTime(txtBox.Text);
tstrEndDate = "RegistrationOpen <= #" + tdtEndDate + "#";
}
if (String.IsNullOrWhiteSpace(tstrEndDate))
{
SectionGridViewFilterExpression = tstrStartDate;
}
else
{
SectionGridViewFilterExpression = tstrStartDate + " AND " + tstrEndDate;
}
}
DataRow[] drFound = dtSectionGridData.Select(SectionGridViewFilterExpression);
dtSectionGridData = drFound.CopyToDataTable();
SectionGridView.DataSource = dtSectionGridData;
SectionGridView.DataBind();
}
}
}
catch (Exception ex)
{
logger.ErrorException(ex.Message, ex);
Response.Redirect("~/Error.aspx");
}
}
当文本框触发事件时,如何访问第一个文本框(&#34; txtBoxOpenDateFilterStart&#34;)?&#34; txtBoxOpenDateFilterEnd&#34;)?
更新 我发现一些代码具有使用2个文本框所需的相同功能。 我更改了标记,以便只有第二个文本框触发事件,并且用于从第一个文本框获取数据的函数使用:
(GridView.HeaderRow.FindControl("controlID") as TextBox).Text
但是,当我使用此功能时,文本框为空白。我输入了一个日期但是当我读到它时,文本框是空的。 我究竟做错了什么? 这是此标题列的标记:
<HeaderTemplate>
<asp:LinkButton runat="server" ID="lbOpenDate" Text="Reg. Open Date" CommandName="Sort" CommandArgument="OpenDate"></asp:LinkButton>
<table>
<tr>
<td><asp:TextBox runat="server" ID="txtBoxOpenDateFilterStart" AutoPostBack="true" CssClass="datepick"></asp:TextBox></td>
<td><asp:Label runat="server" ID="lblOpenDateFilter" Text="To"></asp:Label></td>
<td><asp:TextBox runat="server" ID="txtBoxOpenDateFilterEnd" AutoPostBack="true" OnTextChanged="txtBoxFilter_TextChanged" CssClass="datepick"></asp:TextBox></td>
</tr>
</table>
</HeaderTemplate>
这是与此列有关的方法调用:
protected void txtBoxFilter_TextChanged(object sender, EventArgs e)
{
try
{
if (sender is TextBox)
{
populateSectionGrid();
DataTable dtSectionGridData = SectionGridView.DataSource as DataTable;
Nullable<DateTime> tdtStartDate;
Nullable<DateTime> tdtEndDate;
if (dtSectionGridData != null)
{
TextBox txtBox = (TextBox)sender;
else if (txtBox.ID.Equals("txtBoxOpenDateFilterEnd"))
{
SectionGridViewFilterExpression = string.Empty;
//Get the start date
string temp = (SectionGridView.HeaderRow.Cells[4].FindControl("txtBoxOpenDateFilterStart") as TextBox).Text;
string tstrStartDate = (SectionGridView.HeaderRow.FindControl("txtBoxOpenDateFilterStart") as TextBox).Text;
if (!String.IsNullOrWhiteSpace(tstrStartDate))
{
tdtStartDate = setStartTime(tstrStartDate);
tstrStartDate = "RegistrationOpen >= #" + tdtStartDate + "#";
}
//Get the end date
string tstrEndDate = string.Empty;
if (!String.IsNullOrWhiteSpace(txtBox.Text))
{
tdtEndDate = setStartTime(txtBox.Text);
tstrEndDate = "RegistrationOpen <= #" + tdtEndDate + "#";
}
if (String.IsNullOrWhiteSpace(tstrEndDate))
{
SectionGridViewFilterExpression = tstrStartDate;
}
else if (String.IsNullOrWhiteSpace(tstrStartDate))
{
SectionGridViewFilterExpression = tstrEndDate;
}
else
{
SectionGridViewFilterExpression = tstrStartDate + " AND " + tstrEndDate;
}
}
DataRow[] drFound = dtSectionGridData.Select(SectionGridViewFilterExpression);
dtSectionGridData = drFound.CopyToDataTable();
SectionGridView.DataSource = dtSectionGridData;
SectionGridView.DataBind();
}
}
答案 0 :(得分:0)
我发现事件没有从TextBox获取日期值。
必须删除SortExpression
中的TemplateField
。
以前它看起来像这样:
sp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen">
更正后的版本如下所示:
sp:TemplateField HeaderText="Open Date">
我不知道为什么SortExpression
会影响过滤器表达式。但是删除此属性允许设置文本框。