2下拉列表和日历以在C#中显示gridview

时间:2018-08-23 08:36:04

标签: c#

我为我的学校项目使用了简单的订票系统,并在asp.net中使用Windows窗体。我有一个简单的搜索,该人可以在其中输入提货单,送票和日期。我正在使用下拉列表,用于附加和下拉菜单,以及将普通日历附加到文本框的日期。但是我无法填充gridview,例如在特定日期从一个位置到另一个位置的巴士车程,例如8月24日从A到B.我的日期与其他Nvarchar一起存储为日期。我可以绑定下拉列表,但搜索按钮什么也没显示。 下面是我的前端代码和后端代码。请我需要帮助,是C#和编码方面的新手。

<form id="form1" runat="server">
        <div>
            <asp:TextBox ID="tbdates" runat="server"></asp:TextBox>


            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />

            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>

            <asp:DropDownList ID="DropDownList2" runat="server">
            </asp:DropDownList>

        </div>
        <asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            CssClass="table table-hover table-striped">
            <Columns>
                <asp:BoundField DataField="BusNo" HeaderText="Bus Number" />
                <asp:BoundField DataField="date" HeaderText="Date" />
                <asp:BoundField DataField="Time" HeaderText="Time" />
                <asp:BoundField DataField="Bickup" HeaderText="Bick Up" />
                <asp:BoundField DataField="DropOff" HeaderText="Drop Off" />
                <asp:BoundField DataField="Fare" HeaderText="Fare" />
            </Columns>
            <HeaderStyle BackColor="#33CCFF" />
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
    </form>


public partial class DriverDisplay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_DropDownList1();
                fill_DropDownList2();
            }

        }
        private void fill_DropDownList1()
        {
            try
            {
                SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString);

                string sql = "SELECT * FROM Ticket";
                SqlCommand cmd = new SqlCommand(sql, con2);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "Bickup";
                DropDownList1.DataValueField = "Bickup";
                DropDownList1.DataBind();
            }
            catch (Exception)
            {

            }
        }

        private void fill_DropDownList2()
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ConnectionString);

                string sql = "SELECT * FROM Ticket";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "Dropoff";
                DropDownList2.DataValueField = "DropOff";
                DropDownList2.DataBind();
            }
            catch (Exception)
            {

            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;


        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            tbdates.Text = Calendar1.SelectedDate.ToShortDateString();
            Calendar1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //DateTime date = Convert.ToDateTime(tbdates.Text);
            SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase1ConnectionString"].ToString());

            sqlcon.Open();

            string query = "select * from Ticket where date = @Date";
            SqlCommand cmd = new SqlCommand(query, sqlcon);
            SqlParameter date = cmd.Parameters.Add("@Date", SqlDbType.DateTime);

            SqlDataReader rdr = cmd.ExecuteReader();

            GridView1.DataSource = rdr;
            GridView1.DataBind();

            sqlcon.Close();

        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的代码中存在一些错误,我建议您阅读有关使用ADO.Net和ASP.Net中的C#的CRUD操作以及如何使用SqlConnection和SqlCommand类型的信息。当您使用asp.net Web表单时,可以使用SqlDataSource控件为控件提供数据。

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="tbdates" runat="server"></asp:TextBox>


    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Bickup" DataValueField="Bickup">
    </asp:DropDownList>

    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1" DataTextField="DropOff" DataValueField="Dropoff">
    </asp:DropDownList>

</div>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              CssClass="table table-hover table-striped" DataSourceID="SqlDataSource2">
    <Columns>
        <asp:BoundField DataField="BusNo" HeaderText="Bus Number" />
        <asp:BoundField DataField="date" HeaderText="Date" />
        <asp:BoundField DataField="Time" HeaderText="Time" />
        <asp:BoundField DataField="Bickup" HeaderText="Bick Up" />
        <asp:BoundField DataField="DropOff" HeaderText="Drop Off" />
        <asp:BoundField DataField="Fare" HeaderText="Fare" />
    </Columns>
    <HeaderStyle BackColor="#33CCFF" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabase1ConnectionString %>" 
                   SelectCommand="SELECT [BusNo], [date], [time], [Bickup], [DropOff], [Fare] FROM [Ticket]">

</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabase1ConnectionString %>" SelectCommand="SELECT [BusNo], [date], [time], [Bickup], [DropOff], [Fare] FROM [Ticket] WHERE ([date] = @date)">
    <SelectParameters>
        <asp:CookieParameter CookieName="date" DbType="Date" Name="date" />
    </SelectParameters>
</asp:SqlDataSource>

这是您的DriverDisplay:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.DataBind();
                DropDownList2.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            tbdates.Text = Calendar1.SelectedDate.ToShortDateString();
            Calendar1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["date"] == null)
            {
                Request.Cookies.Add(new HttpCookie("date"));
            }
            Request.Cookies["date"].Value = tbdates.Text;
        }