【问题标题】:SqlDataSource requery on button clickSqlDataSource 在按钮单击时重新查询
【发布时间】:2015-10-18 05:34:12
【问题描述】:

我遇到了关于 asp:SqlDataSource 的问题。我正在尝试根据开始和结束日期提取一些数据。我希望这些数据能够在过去 24 小时内加载,但现在我只是想把它全部拉回来进行测试。我有两个 asp:TextBox 控件,一个开始日期和一个结束日期。

没什么特别的,应该很简单……

我的问题是将存储过程参数绑定到 asp:TextBox。 TextBox 将我在文本框中键入的文本放入“文本”属性中。似乎有道理,但问题是asp将这个控件转换为输入标签,然后将输入的文本放入值属性中。

现在,在配置 SqlDataSource 时,它​​想使用实际页面上不存在的 controlName.Text。它是 controlName.value。

如果我尝试将 asp:ControlParameter 绑定到 controlName.value,则会收到一条错误消息,指出 value 不是 TextBox 的属性。没错...这是输入的属性...所以它不会让我这样做。

如果我只有一个直接的 sql 查询,我会取回数据。当我使用所有默认值测试存储过程时,我会取回数据。一旦我绑定控制,我什么也得不到。我确实处理传入日期字段的空值和空字符串,所以我认为不是这样。

简而言之,我迷路了。帮助!

初始 HTML:

<asp:TextBox ID="startDate" Width="10em" runat="server" AutoPostBack="true"></asp:TextBox>

生成的 HTML:

<input type="text" style="width:10em;" id="startDate" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" onchange="javascript:setTimeout('__doPostBack(\'startDate\',\'\')', 0)" value="2000/07/13 00:00" name="startDate">

失败:

        <asp:SqlDataSource ID="employeesSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DbConnectionString %>" SelectCommand="SPRT_GetRecords" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="startDate" Name="StartDate" PropertyName="Text" Type="DateTime" />
                <asp:ControlParameter ControlID="endDate" Name="EndDate" PropertyName="Text" Type="DateTime" />
                <asp:Parameter Name="GetLogs" Type="Boolean" />
                <asp:Parameter Name="LogType" Type="Decimal" />
            </SelectParameters>
        </asp:SqlDataSource>

失败:

       <asp:SqlDataSource ID="employeesSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DbConnectionString %>" SelectCommand="SPRT_GetRecords" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter Name="StartDate" Type="DateTime" />
                <asp:Parameter Name="EndDate" Type="DateTime" />
                <asp:Parameter Name="GetLogs" Type="Boolean" />
                <asp:Parameter Name="LogType" Type="Decimal" />
            </SelectParameters>
        </asp:SqlDataSource>

工作但不拉入控制输入:

        <asp:SqlDataSource ID="employeesSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DbConnectionString %>" SelectCommand="declare @SDate datetime
            set @SDate = DateAdd(dd, -100, GetDate())
            EXEC SPRT_GetRecords @StartDate = @SDate, @EndDate = null, @GetLogs = 0, @LogType = 0"></asp:SqlDataSource>

【问题讨论】:

    标签: c# asp.net datagrid webforms sqldatasource


    【解决方案1】:

    好的,在与其他人讨论并没有提出太多想法之后,我能够从不同的角度看一看并找出答案。

    我假设由于我在存储过程中设置了默认值,因此我不必为可选参数指定值。这不是真的……

    我的问题的解决方案是添加默认值。注意下面的两个 asp:parameters 现在有默认值。我希望这会引发一个错误并让它冒泡给我,而不是默默地爆炸而不返回任何结果。

            <asp:SqlDataSource ID="employeesSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DbConnectionString %>" SelectCommand="SPRT_GetRecords" SelectCommandType="StoredProcedure">
                <SelectParameters>
                    <asp:ControlParameter ControlID="startDate" Name="StartDate" PropertyName="Text" Type="DateTime" />
                    <asp:ControlParameter ControlID="endDate" Name="EndDate" PropertyName="Text" Type="DateTime" />
                    <asp:Parameter DefaultValue="false" Name="GetLogs" Type="Boolean" />
                    <asp:Parameter DefaultValue="0" Name="LogType" Type="Decimal" />
                </SelectParameters>
    

    此外,我在 Page_Load 方法中设置了开始/结束日期的默认值,如下所示:

        if (!IsPostBack)
        {
            // Default the grid to the last 24 hours worth of data
            employeesSqlDataSource.SelectParameters["startDate"].DefaultValue = DateTime.Now.AddDays(-1).ToString();
            employeesSqlDataSource.SelectParameters["endDate"].DefaultValue = DateTime.Now.ToString();
            ...
            ...
        }
    

    为了让按钮工作,我只是做一些验证,然后在 onclick 中调用 SqlDataSource 上的 DataBind()。

    protected void searchButton_Click(object sender, EventArgs e)
    {
        CheckDates();
    
        if (string.IsNullOrEmpty(startDate.Text)) employeesSqlDataSource.SelectParameters["startDate"].DefaultValue = SqlDateTime.MinValue.ToString();
        if (string.IsNullOrEmpty(endDate.Text)) employeesSqlDataSource.SelectParameters["endDate"].DefaultValue = SqlDateTime.MaxValue.ToString();
    
        employeesSqlDataSource.DataBind();
    }
    

    重要的是最后一行。

    希望这可以帮助其他有需要的人。

    【讨论】:

      【解决方案2】:

      您能在按钮单击事件的代码中执行此操作吗?或者将其设为日期时间而不是字符串,然后执行相同的操作。

        string startdate = startDate.text;
        //OR DateTime 
        employeesSqlDataSource.SelectParameters.Add("@SDate", startdate);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多