C# 当从另一个文本框触发ContextChanged事件时,访问Gridview中的文本框值

C# 当从另一个文本框触发ContextChanged事件时,访问Gridview中的文本框值,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在向gridview添加过滤器。其中一列是日期字段,我有两个文本框,用户可以输入一个范围。用户输入第二个日期后,将触发事件 但是,我无法访问第一个文本框中的数据。我得到一个错误,对象尚未设置 这是标记: asp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen"> <HeaderTemplate> <asp:LinkButt

我正在向gridview添加过滤器。其中一列是日期字段,我有两个文本框,用户可以输入一个范围。用户输入第二个日期后,将触发事件

但是,我无法访问第一个文本框中的数据。我得到一个错误,对象尚未设置

这是标记:

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>
但是,当我使用此函数时,文本框为空。我输入了一个日期,但当我阅读它时,文本框是空的。 我做错了什么? 这是我对该标题列的标记:

 <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();
                }
            }
protectedvoid txtBoxFilter\u text已更改(对象发送方,事件参数e)
{
尝试
{
如果(发件人是文本框)
{
populateSectionGrid();
DataTable dtSectionGridData=SectionGridView.DataSource作为DataTable;
可为空的tdtStartDate;
可为空的日期;
如果(dtSectionGridData!=null)
{
TextBox txtBox=(TextBox)发送方;
else if(txtBox.ID.Equals(“txtBoxOpenDateFilterEnd”))
{
SectionGridViewFilterExpression=string.Empty;
//获取开始日期
字符串temp=(SectionGridView.HeaderRow.Cells[4].FindControl(“txtBoxOpenDateFilterStart”)作为文本框);
字符串tstrStartDate=(SectionGridView.HeaderRow.FindControl(“txtBoxOpenDateFilterStart”)作为文本框);
如果(!String.IsNullOrWhiteSpace(tstrStartDate))
{
tdtStartDate=设置开始时间(tstrStartDate);
tstrStartDate=“RegistrationOpen>=#“+tdtStartDate+#”;
}
//获取结束日期
string tstrEndDate=string.Empty;
如果(!String.IsNullOrWhiteSpace(txtBox.Text))
{
TDTENDATE=设置开始时间(txtBox.Text);

tstrEndDate=“RegistrationOpen我找到了事件无法从文本框中获取日期值的方法。 必须删除
模板字段中的
SortExpression
。 以前是这样的:

sp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen">
sp:TemplateField HeaderText="Open Date">
更正后的版本如下所示:

sp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen">
sp:TemplateField HeaderText="Open Date">
我不知道为什么
SortExpression
会影响筛选器表达式。但是删除此属性可以设置文本框