【问题标题】:Access TextBox value in Gridview when OnTextChanged event triggered from another textbox当从另一个文本框触发 OnTextChanged 事件时访问 Gridview 中的文本框值
【发布时间】:2014-05-22 18:44:14
【问题描述】:

我正在向网格视图添加过滤器。其中一列是日期字段,我有 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");
        }
    }

当事件被文本框(“txtBoxOpenDateFilterEnd”)触发时,如何访问第一个文本框(“txtBoxOpenDateFilterStart”)?

更新 我发现一些代码具有与使用 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();
                }
            }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    我发现事件没有从 TextBox 获取日期值。 TemplateField 中的 SortExpression 必须被删除。 以前是这样的:

    sp:TemplateField HeaderText="Open Date" SortExpression="RegistrationOpen">
    

    修正后的版本如下:

    sp:TemplateField HeaderText="Open Date">
    

    我不知道为什么SortExpression 会影响过滤器表达式。但是删除此属性允许设置文本框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      相关资源
      最近更新 更多