none
sqldatasourse的值怎么在后台取到? RRS feed

  • 问题

  •     在2个TextBox中输入一前一后的日期,作为SqlDatasource的参数,得到一个值。这个值怎么在c#后台代码中取到呢?

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Try_BackGoundValue_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox_DateFrom" runat="server" AutoPostBack="True"></asp:TextBox>
            <asp:TextBox ID="TextBox_DateTo" runat="server" AutoPostBack="True"></asp:TextBox>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                SelectCommand="SELECT COUNT(LastName) AS a FROM Employees WHERE (BirthDate &gt; @TimeFrom) AND (BirthDate &lt; @TiemTo)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox_DateFrom" Name="TimeFrom" 
                        PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox_DateTo" Name="TiemTo" 
                        PropertyName="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
        
        </div>
        </form>
    </body>
    </html>


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年10月31日 7:53

全部回复

  • Sorry, I can't input Chinese when using this laptop. There are two methods you can handle it. The first one is through front end. You can click "Configure Data Source", then click "Where". Next, bind the parameter and the control (Textbox). You also need to create a button, names "Search". Then Run it, It's useful.

    Another Method is to write code in the .cs file. Create a SqlDataSource Object, and add the sqlcommand. Then, add parameter which is binding with Textbox.

     

    2013年10月31日 20:06
  • linjiangxian11:

    如果要获取某个值,我建议你直接用SqlCommand的ExecuteScalar方法,而不是用您的参数化设置——因为这个设置仅是为数据绑定控件作为方便使用的。


    Click For donating:Free Rice For the poor
    For spamming-sender issues, you can either report it at Microsoft Spamming Issue, or just find "Report Spam Here+Number" at Forum Issue;You can also find "Verify Your Account+Number" at "Forum Issue", where you can submit to be confirmed to paste links or images.
    For more things to talk about? StackOverFlow is your choice.

    2013年11月1日 3:17
  • 你好,

         你能尝试接下来的方法:

       1. 首先在HTML Ui设计视图设定SqlDataSource1属性 DataSourceMode="DataReader"

       2.尝试下面的代码:

      
      IDataReader reader = ((IDataReader)((IEnumerable)
                 SqlDataSource1.Select(DataSourceSelectArguments.Empty)));
                   
                    while (reader.Read())
                    {
                        xtname += reader["a"].ToString();
                    }
           希望这个可以帮到你。

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年11月5日 2:03
    版主