一个GridView,一个DropDownList,一个Button ,
DropDownList为(0,最近7天),(1,最近15天),(2,最近30天),(3,查询所有)
当button点击时,根据DropDownList的SelectedIndex值查询。
该怎么循环查询好些呢。 protected void btn_cha_Click(object sender, EventArgs e)
{
if (ddl7.SelectedIndex == 0)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 1)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader(); GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 2)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 3)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
}
DropDownList为(0,最近7天),(1,最近15天),(2,最近30天),(3,查询所有)
当button点击时,根据DropDownList的SelectedIndex值查询。
该怎么循环查询好些呢。 protected void btn_cha_Click(object sender, EventArgs e)
{
if (ddl7.SelectedIndex == 0)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 1)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader(); GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 2)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
if (ddl7.SelectedIndex == 3)
{
GridView1.DataSource = null;
string sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
}
}
解决方案 »
- ListView中的Button为什么无法触发ItemCommand事件
- 我页面编码用的gb2312 但是Textbox后台取值变长乱码
- fileupload控件暴露
- .net中,在做动态网页时,母板页与内容页如何同步?求教!
- OracleException的奇怪问题
- 求救--------关于网页预览的问题.
- 请教个问题,有两个页面,a.aspx,b.aspx如何在a.aspx.cs里给b.apsx里的控件赋值,如控件label ,Url,Session,Cookie这些我知道,不用这些
- 100求一页面导航问题
- 在asp.net中能否实现把<SELECT></SELECT>的值存入数据库? 其能否响应 button事件?
- 怎么样在ASP.NET中弹出一个提示框和对话框???
- 在asp.net 中有这样的时间控件吗?
- 如何让网页不失效?
还可以对datatable进行本地筛选,这样就不用频繁访问数据库了
{
GridView1.DataSource = null;
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
sdr.Close();
} protected void btn_cha_Click(object sender, EventArgs e)
{
string sql = "";
if (ddl7.SelectedIndex == 0)
{
sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";
}
if (ddl7.SelectedIndex == 1)
{
sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";
}
if (ddl7.SelectedIndex == 2)
{
sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";
}
if (ddl7.SelectedIndex == 3)
{
sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";
}
ExecSql(sql);
}