首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么实现Calendar日历控件对数据的查询

2013-08-01 
如何实现Calendar日历控件对数据的查询前台代码asp:GridView IDGridView1 runatserver AutoGenerat

如何实现Calendar日历控件对数据的查询
前台代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns > 
<asp:BoundField DataField="ID" HeaderText="用户ID" ReadOnly="True" /> 
<asp:BoundField DataField="name" HeaderText="姓名" ReadOnly="true" />
<asp:BoundField DataField="sex" HeaderText="性别" ReadOnly="true" />  
<asp:BoundField DataField="tel" HeaderText="联系方式" ReadOnly="true" />
<asp:BoundField DataField="date" HeaderText="日期" ReadOnly="true" />  
    <asp:TemplateField HeaderText="业务情况">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="server">
                <asp:ListItem>开通</asp:ListItem>
                <asp:ListItem>未开通</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("bus") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
<asp:CommandField HeaderText="选择" ShowSelectButton="True" /> 
<asp:CommandField HeaderText="编辑" ShowEditButton="True" /> 
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> 
</Columns > 
<RowStyle ForeColor="#000066" /> 
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
</asp:GridView >

        日期选择:<asp:Button ID="Button3" runat="server" BorderStyle="Inset" 


            BorderWidth="1px" Height="21px" Width="102px" onclick="Button3_Click"/>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
            <asp:ListItem Value="0">全部</asp:ListItem>
            <asp:ListItem Value="1">开通</asp:ListItem>
            <asp:ListItem Value="2">未开通</asp:ListItem>
        </asp:DropDownList>
    
        <asp:Button ID="Button1" runat="server" Text="查询" onclick="Button1_Click" />

        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="导出数据" />
    
        <asp:Calendar ID="Calendar1" runat="server" Visible="false" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
        
后台代码
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;


public partial class admin : System.Web.UI.Page
{
    public string j;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\系统雏形\\App_Data\\user.mdf;Integrated Security=True;User Instance=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;


        bind();
    }

    //删除数据
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from [user] where ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strCon);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }

    //更新数据
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string value = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2")).SelectedValue;
        DateTime dt = DateTime.Now;
        dt.ToString("yyyyMMdd");
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update [user] set bus='"
            + value + "', date='"+ dt +"' where ID='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }

    //取消数据
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }


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

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Button3.Text = Calendar1.SelectedDate.Year.ToString() + "-" + Calendar1.SelectedDate.Month.ToString() + "-" + Calendar1.SelectedDate.Day.ToString();
        Calendar1.Visible = false;
        
        
    }

    //绑定数据
    public void bind()
    {
        //修改下面两行代码,注意where后面要有空格
        string i;
        i = DropDownList1.SelectedItem.Text;
        if (int.Parse(DropDownList1.SelectedValue) == 0)
            j = "bus= '开通' or bus= '未开通'";
        if (int.Parse(DropDownList1.SelectedValue) == 1)
            j = "bus='" + i + "'";
        if (int.Parse(DropDownList1.SelectedValue) == 2)
            j = "bus='" + i + "'";

        string sqlstr = "select * from [user] where " + j;
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "[user]");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };//主键
        GridView1.DataBind();
        sqlcon.Close();


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string i;
        i = DropDownList1.SelectedItem.Text;
        if (int.Parse(DropDownList1.SelectedValue) == 0)
            j = "bus= '开通' or bus= '未开通'";
        if (int.Parse(DropDownList1.SelectedValue) == 1)
            j = "bus='" + i + "'";
        if (int.Parse(DropDownList1.SelectedValue) == 2)
            j = "bus='" + i + "'";
        string sqlstr = "select * from [user] where "+ j;
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "[user]");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };//主键
        GridView1.DataBind();
        sqlcon.Close();
    }

    public static void ConvertToExcel(System.Web.UI.Control control, string encoding, string filename)
    {
        //设置文件名格式,防止中文文件名乱码
        string FileName = System.Web.HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(filename));
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.Buffer = true;
        System.Web.HttpContext.Current.Response.Charset = "" + encoding + "";
        //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开


        //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
        System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("" + encoding + "");
        //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
        System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
        control.EnableViewState = false;
        // 定义一个输入流
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        control.RenderControl(oHtmlTextWriter);
        //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
        System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
        System.Web.HttpContext.Current.Response.End();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string filename = String.Format("业务表.xls", DateTime.Now.Month.ToString());
        ConvertToExcel(GridView1, "UTF-8", filename);
        bind();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void gvSettlement_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }


    }
   
}

界面如下图:
怎么实现Calendar日历控件对数据的查询

要实现对日期和业务情况的同时查询,业务情况查询已经做好了,可是日期查询总有问题,这段代码里没有日期查询,我已经给删了。请高手指教!
日历 控件 ASP 数据查询 .net
[解决办法]

引用:
我不是要这样的啦,我要从Button3.Text提取数据的,而且不是时间段,是时间点,你把代码看一遍吧,求解决方案

select * from 表 where 日期 between '2013-7-25 00:00:00' and '2013-7-25 23:59:59' 
1、时间: 就是Button3.Text的值 如果你能获取到这个 跟2013-7-25 00:00:00 这些有什么区别?
2、时间点: 你说的时间点是怎么样的?具体的时间?如果你查的数据跟你数据库时间类型都不一样怎么查?
3、 string s = "date like '%" + Button3.Text + "%'"; 获取到的值像是这样的('2013-7-25' )?如果前面都一样,前面那个%就不用了,且判断时间用大小最好,别用like,还有,类型转换问题:convert(varchar,brush_datetime ,100) like '013-7-25%' ,你可以这样转换试下
4、如果还是断点找不出来哪个sql语句错了,麻烦你把断点到查询的和输出的值该sql语句贴出来
5、如果还是不行,对不起,爱莫能助,既然你知道哪里有问题,干嘛不把他那些sql语句到数据库查询更改多尝试一下是值问题还是类型问题===

热点排行