C#语句:
protected void BindQuery() //组成查询条件
{
int j = 0;
//部门
string strSql = " and DC.dept_id in (";
for (int i = 0; i < this.cblDeptId.Items.Count; i++)
{
if (cblDeptId.Items[i].Selected)
{
j = 1;
strSql = strSql + "'" + cblDeptId.Items[i].Value + "',";
}
}
strSql = strSql + "$$$";
strSql = strSql.Replace(",$$$", ")");
if (j == 0) { strSql = ""; }
//状态
string strSql2 = " and DC.dinner_status in (";
for (int i = 0; i < this.cblDinnerStatus.Items.Count; i++)
{
if (cblDinnerStatus.Items[i].Selected)
{
j = 2;
strSql2 = strSql2 + "'" + cblDinnerStatus.Items[i].Value + "',";
}
}
strSql2 = strSql2 + "$$$";
strSql2 = strSql2.Replace(",$$$", ")");
if (j == 2) { strSql = strSql + strSql2; }
//日期
if (cblDate.Items[0].Selected == true)
{
if (txtBegT.Text.Trim() == "" || ! IsDate(txtBegT.Text))
{
return;
}
else
strSql = strSql + " and DC.rec_tim>=#" + txtBegT.Text + "#";
}
if (cblDate.Items[1].Selected == true)
{
if (txtEndT.Text.Trim() == "" || ! IsDate(txtEndT.Text))
{
return;
}
else
strSql = strSql + " and DC.rec_tim<#" + txtEndT.Text + "#";
}
//查询记录函数
Bind(strSql);
}
protected void Bind(string Str) //查询记录
{
string strTab;
//判断查询当前记录还是历史记录
if (this.rdlTab.SelectedValue == "DAY")
strTab = "dinner_choice";
else
strTab = "dinner_choice_bak";
//连接数据库
OleDbConnection Conn = new OleDbConnection(connStr);
Conn.Open();
string strSql = "select ID,dept_nam,user_nam,dinner_rmk,dinner_cnt,class_rmk,rec_tim,iif(dinner_status='00','待确认',iif(dinner_status='01','已确认','不允许')) as dinner_status,dinner_memo,app_tim from " + strTab + " DC,dinner_info DI,dinner_class CR,user_info UI,dept_info EI where DC.dinner_id=DI.dinner_id and DC.dinner_class=DI.dinner_class and DC.dinner_class=CR.dinner_class and DC.user_id=UI.user_id and DC.dept_id=EI.dept_id " + Str + " order by DC.rec_tim";
//结果集对象
OleDbDataAdapter rs = new OleDbDataAdapter(strSql, Conn);
//集合
DataSet ds = new DataSet();
//把rs给了ds
rs.Fill(ds);
//绑定到GridView
gvSubmit.DataSource = ds.Tables[0].DefaultView;
gvSubmit.DataBind();
Conn.Close();
gvColor();
}
2.用户输入的日期格式不正确,给出错误提示。
HTML语言:
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Label ID="lblTimErr" runat="server" Font-Bold="True" ForeColor="Red" Width="120px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
C#语句:
//日期较验语句
if (cblDate.Items[0].Selected == true)
{
if (txtBegT.Text.Trim() == "" || ! IsDate(txtBegT.Text)) //调用日期判断函数
{
lblTimErr.Text = "开始日期不合法,不能查询!";
return;
}
else
strSql = strSql + " and DC.rec_tim>=#" + txtBegT.Text + "#";
}
if (cblDate.Items[1].Selected == true)
{
if (txtEndT.Text.Trim() == "" || ! IsDate(txtEndT.Text))
{
lblTimErr.Text = "结束日期不合法,不能查询!";
return;
}
else
strSql = strSql + " and DC.rec_tim<#" + txtEndT.Text + "#";
private bool IsDate(string s) //日期判断函数
{
try
{ //利用时间函数的Parse方法
DateTime d = DateTime.Parse(s);
return true;
}
catch
{
return false;
}
}
}
3.GridView实现分页显示和嵌入CheckBox复选框进行多选,实现批量提交。
HTML语言:
<asp:GridView ID="gvSubmit" runat="server" AutoGenerateColumns="False" CellPadding="4" onPageIndexChanging="gvSubmit_PageIndexChanging"
EmptyDataText="没有任何数据可以显示。" ForeColor="Black" GridLines="None" Width="882px" OnRowDataBound="gvSubmit_RowDataBound" AllowPaging="True" ShowFooter="True" > //允许分页和显示页脚
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:CheckBox ID="chkGV" runat="server" /> //嵌入复选框
</ItemTemplate>
<ControlStyle Font-Bold="True" Font-Size="Medium" />
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="dept_nam" HeaderText="部门" />
<asp:BoundField DataField="user_nam" HeaderText="姓名" />
<asp:BoundField DataField="dinner_rmk" HeaderText="餐名" />
<asp:BoundField DataField="dinner_cnt" HeaderText="数量" />
<asp:BoundField DataField="class_rmk" HeaderText="民族" />
<asp:BoundField DataField="rec_tim" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" HeaderText="订餐时间"
HtmlEncode="False" />
<asp:BoundField DataField="dinner_status" HeaderText="状态" >
<ItemStyle Font-Bold="True" />
<FooterStyle Font-Bold="True" />
</asp:BoundField>
<asp:BoundField DataField="dinner_memo" HeaderText="备注" />
<asp:BoundField DataField="app_tim" DataFormatString="{0:yyyy-MM-dd HH:mm:ss}" HeaderText="审批时间" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
C#语句:
protected void gvSubmit_PageIndexChanging(object sender, GridViewPageEventArgs e) //分页显示
{
gvSubmit.PageIndex = e.NewPageIndex; //显示所有页号
BindQuery(); //调用查询,刷新记录
}
protected void chkAll_CheckedChanged(object sender, EventArgs e) //全选和反选的实现
{
if (chkAll.Checked != true)
chkAll.Text = "全选";
else
chkAll.Text = "取消";
for (int i = 0; i <= gvSubmit.Rows.Count - 1; i++) //遍历当前页所有记录
{
CheckBox chk = (CheckBox)gvSubmit.Rows[i].FindControl("chkGV");
//“不允许”状态的记录不能操作
if (gvSubmit.Rows[i].Cells[0].Enabled == false)
continue;
if (chkAll.Checked == true)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
//逐条记录判断GridView的复选框是否有效
private bool gvCheck(String swt)
{
bool j = false;
if (gvSubmit.Rows.Count == 0)
{
Response.Write("<script language=javascript>window.alert('当前没有任何记录!');</script>");
return (j);
}
//开始逐条判断checkbox
for (int i = 0; i <= gvSubmit.Rows.Count - 1; i++)
{
CheckBox chk = (CheckBox)gvSubmit.Rows[i].FindControl("chkGV"); //取出checkbox的值
if (chk.Checked ==true)
{
j=true ;
break;
}
}
if (j == false)
{
Response.Write("<script language=javascript>window.alert('请至少选择一条记录!');</script>");
return (j);
}
}
|