但是如果想增加查询就不太方便了。
比如我想首次进入页面时在gridview里面显示全部的数据,然后下面有些查询条件比如姓名、email等,输入姓名点查询则会在gridview里显示过滤后的数据。以前是这么做的:在SqlDataSource里面写查询分支,比如
if @op=1
begin
select .....
end
else if @op=2
begin
select....
end
但是这样有个问题就是你必须在SqlDataSource里面把查询参数事先设定好,如果我想在用户不输入email时默认是不过滤email的查询,所以这样就不太好弄。
现在的方法是点击查询后重写SqlDataSource的selectcommand,但是这样一来如果想修改查出来的数据就会出错,因为postback以后SqlDataSource里面固定的selectcommand又出来了。这样我就想增加一个变量来记录是过滤查询还是全部的查询,不过在asp.net里的变量在下一次postback时就又重新初始化了,除非设定为static的,但是当有多个用户时其他人可以改变这个static值,相当于是全局的。所以这种情况需要用session或者viewstate来实现。
说了这么多我都乱了,不说了,把代码贴出来给遇到同样问题的兄弟们看看,如果大家有更好的办法请赐教。
protected void Page_Load(object sender, EventArgs e)
#region 触发事件
private void dosearch()
{
if ((string)ViewState["search"] == "1")
{
string sSql = "SELECT DB_OA.dbo.OA_USER.FLD_USERNAME, InformPerson.InformPersonID, "
+ "InformPerson.InformPersonName, InformPerson.Email, InformPerson.Mobile, "
+ "InformPerson.CreateUserID FROM InformPerson LEFT OUTER JOIN DB_OA.dbo.OA_USER"
+ " ON InformPerson.CreateUserID = DB_OA.dbo.OA_USER.fld_UserId";
string sWhere = " where 1=1";
if (txb_search1.Text != "")
{
sWhere += " and InformPersonName like " + Common.QuotedStr("%" + txb_search1.Text + "%");
}
if (txb_search2.Text != "")
{
sWhere += " and Email like " + Common.QuotedStr("%" + txb_search2.Text + "%");
}
if (txb_search_mobile.Text != "")
{
sWhere += " and Mobile like " + Common.QuotedStr("%" + txb_search_mobile.Text + "%");
}
sWhere += " order by InformPersonID";
sSql += sWhere;
sds_data.SelectCommand = sSql;
gv_data_list.AllowPaging = false;
}
else
{
string sSql2 = "SELECT DB_OA.dbo.OA_USER.FLD_USERNAME, InformPerson.InformPersonID, "
+ "InformPerson.InformPersonName, InformPerson.Email, InformPerson.Mobile, "
+ "InformPerson.CreateUserID FROM InformPerson LEFT OUTER JOIN DB_OA.dbo.OA_USER"
+ " ON InformPerson.CreateUserID = DB_OA.dbo.OA_USER.fld_UserId"
+ " order by InformPersonID";
sds_data.SelectCommand = sSql2;
gv_data_list.AllowPaging = true;
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_dosearch_Click(object sender, EventArgs e)
{
ViewState["search"] = "1";
dosearch();
gv_data_list.DataBind();
}
protected void btn_search_cancel_Click(object sender, EventArgs e)
{
gv_data_list.AllowPaging = true;
pn_search.Visible = false;
ViewState["search"] = "0";
dosearch();
gv_data_list.DataBind();
}
#endregion
#region 触发事件
private void dosearch()
{
if ((string)ViewState["search"] == "1")
{
string sSql = "SELECT DB_OA.dbo.OA_USER.FLD_USERNAME, InformPerson.InformPersonID, "
+ "InformPerson.InformPersonName, InformPerson.Email, InformPerson.Mobile, "
+ "InformPerson.CreateUserID FROM InformPerson LEFT OUTER JOIN DB_OA.dbo.OA_USER"
+ " ON InformPerson.CreateUserID = DB_OA.dbo.OA_USER.fld_UserId";
string sWhere = " where 1=1";
if (txb_search1.Text != "")
{
sWhere += " and InformPersonName like " + Common.QuotedStr("%" + txb_search1.Text + "%");
}
if (txb_search2.Text != "")
{
sWhere += " and Email like " + Common.QuotedStr("%" + txb_search2.Text + "%");
}
if (txb_search_mobile.Text != "")
{
sWhere += " and Mobile like " + Common.QuotedStr("%" + txb_search_mobile.Text + "%");
}
sWhere += " order by InformPersonID";
sSql += sWhere;
sds_data.SelectCommand = sSql;
gv_data_list.AllowPaging = false;
}
else
{
string sSql2 = "SELECT DB_OA.dbo.OA_USER.FLD_USERNAME, InformPerson.InformPersonID, "
+ "InformPerson.InformPersonName, InformPerson.Email, InformPerson.Mobile, "
+ "InformPerson.CreateUserID FROM InformPerson LEFT OUTER JOIN DB_OA.dbo.OA_USER"
+ " ON InformPerson.CreateUserID = DB_OA.dbo.OA_USER.fld_UserId"
+ " order by InformPersonID";
sds_data.SelectCommand = sSql2;
gv_data_list.AllowPaging = true;
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_dosearch_Click(object sender, EventArgs e)
{
ViewState["search"] = "1";
dosearch();
gv_data_list.DataBind();
}
protected void btn_search_cancel_Click(object sender, EventArgs e)
{
gv_data_list.AllowPaging = true;
pn_search.Visible = false;
ViewState["search"] = "0";
dosearch();
gv_data_list.DataBind();
}
#endregion