单击SqlDataSource query on按钮

本文关键字:按钮 on query SqlDataSource 单击 | 更新日期: 2023-09-27 18:05:05

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

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

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

现在,在配置SqlDataSource时,它想要使用controlName。实际页面上不存在的文本。controlName.value。

如果我尝试绑定asp:ControlParameter到controlName。我得到一个错误消息,值不是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>

单击SqlDataSource query on按钮

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

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

好吧,在和别人讨论过之后,我能从不同的角度看一看,并找出答案。

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

我的问题的解决方案是添加默认值。注意下面两个asp:参数现在有默认值了。我希望这将抛出一个错误,让它气泡,而不是无声地爆炸,不返回任何结果。

        <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();
}

重要的位是最后一行。

希望这篇文章能帮助到有需要的人。