后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Linq;
using System.Xml.Linq;
using Club.Model;
public partial class GL_HDYY : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Get_Article();
BindResult();
}
}
private void Get_Article()
{
try
{
gvData.DataSource = GetCodeBy(0);
gvData.DataBind();
}
catch
{
}
}
public DataSet GetCodeBy(int iCount)
{
PubConstant date = new PubConstant();
string strTop = "";
if (iCount > 1)
{
strTop = "top " + iCount.ToString();
}
string sql = "select " + strTop + " * from Activity where [open]='是' ";
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = null;
try
{
con.Open();
ds = new DataSet();
da.Fill(ds);
}
catch (SqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return ds;
}
#region 数据绑定
/// <summary>
///绑定列表
/// </summary>
private void BindResult()
{
//组合查询条件
string strSqlWhere = string.Empty;
if (this.KSCX.SelectedValue.Trim() != "全部")
{
if (this.txtCXNR.Text != "")
{
strSqlWhere += " and " + this.KSCX.SelectedValue.Trim() + " like '%" + this.txtCXNR.Text.Trim() + "%'";
}
}
T_HD modelList = new T_HD();
DataTable dt = modelList.GetListINFO(strSqlWhere).Tables[0];
if (dt != null)
{
this.gvData.DataKeyNames = new string[] { "ActID" };
this.gvData.DataSource = dt;
this.gvData.DataBind();
//ShowStats(dt.Rows.Count.ToString());
}
}
#endregion
#region 设置翻页按钮事件
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (gvData.PageIndex < (this.gvData.PageCount - 1))
{
gvData.PageIndex += 1;
}
break;
case "prev":
if (gvData.PageIndex > 0)
{
gvData.PageIndex -= 1;
}
break;
case "last":
if (gvData.PageCount != 0)
{
gvData.PageIndex = (gvData.PageCount - 1);
}
else
{
gvData.PageIndex = 0;
}
break;
default:
gvData.PageIndex = System.Convert.ToInt32(arg);
break;
}
//绑定列表
BindResult();
}
#endregion
#region 按钮事件
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{
BindResult();
}
protected void btnSelectAll_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
bool isOK = false;
if (btn.Text == "全选")
{
isOK = true;
//找到PagerTemplate中的全选按钮
btnSelectAll.Text = "取消全选";
}
else
{
btnSelectAll.Text = "全选";
}
foreach (GridViewRow row in gvData.Rows)
{
((CheckBox)row.Cells[0].FindControl("chkSelect")).Checked = isOK;
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
for (int rowIndex = 0; rowIndex < gvData.Rows.Count; rowIndex++)
{
if (((CheckBox)gvData.Rows[rowIndex].Cells[0].FindControl("chkSelect")).Checked)
{
if(DelCode(Convert.ToInt32(gvData.DataKeys[rowIndex].Value)))
{
gvData.Rows[rowIndex].Visible = false;
}
if (UpdateRenShu(Convert.ToInt32(gvData.DataKeys[rowIndex].Value)))
{
}
}
}
BindResult();
ResponseMessage(this.Page, "申请成功!");
}
public bool @DelCode(int id)
{
bool isOK = false;
//string sql = "insert into Reservation(ClassID,artID,artName,classname,classmajor,classphone)values('" + Session["ID"].ToString() + "','" + id + "','" + Session["Artname"].ToString() + "','" + Session["Classname"].ToString() + "','" + Session["Classpmajor"].ToString() + "','" + Session["Classphone"].ToString() + "')";
string sql = "insert into Reservation(UserID,actID)values('" + Session["ID"].ToString() + "','" + id + "')";
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
isOK = true;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
return isOK;
}
public bool UpdateRenShu(int id)
{
bool isOK = false;
string sql = "update Activity set ActNumber=ActNumber+1 where id=" + id;
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
isOK = true;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
return isOK;
}
protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvData.PageIndex = e.NewPageIndex;
BindResult();
}
}