下面就是VS中的aspx页面的设计了,设计web页面如下:
对于图4,我之前在GridView相关的文章里面介绍了,你可以自己找找,我就不“喂”你了。下面我们来看看图3的后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
//num++;
SqlConnection DBCon = DBClass.GetConnection();
if (RadioButtonList2.SelectedItem == null)
{
Response.Write("<script>alert('请选择你的联盟英雄的看法!')</script>");
}
else if (RadioButtonList1.SelectedItem == null)
{
Response.Write("<script>alert('请选择联盟英雄!')</script>");
}
else
{
////////数据库连接
DBCon.Open();
//投票人数存储过程代码
int heroid = Convert.ToInt32(RadioButtonList1.SelectedIndex)+1;
SqlCommand cmd = new SqlCommand("CountNum", DBCon);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter heroID = new SqlParameter("@id", SqlDbType.NVarChar, 4);
heroID.Value = heroid;
cmd.Parameters.Add(heroID);
cmd.ExecuteNonQuery();
//投票的英雄存储过程代码
string heroname = RadioButtonList1.SelectedItem.ToString();
int heroid1 = Convert.ToInt32(RadioButtonList1.SelectedIndex) + 1;
SqlCommand Sqlcom = new SqlCommand("hero", DBCon);
Sqlcom.CommandType = CommandType.StoredProcedure;
SqlParameter heroID1 = new SqlParameter("@id", SqlDbType.NVarChar, 4);
heroID1.Value = heroid1;
Sqlcom.Parameters.Add(heroID1);
SqlParameter heroname1 = new SqlParameter("@name", SqlDbType.NVarChar, 10);
heroname1.Value = heroname;
Sqlcom.Parameters.Add(heroname1);
Sqlcom.ExecuteNonQuery();
//投票英雄的结果存储过程绑定
string opinion = RadioButtonList2.SelectedItem.ToString();
int heroid2 = Convert.ToInt32(RadioButtonList1.SelectedIndex) + 1;
SqlCommand OpinionCom = new SqlCommand("opinion", DBCon);
OpinionCom.CommandType = CommandType.StoredProcedure;
SqlParameter heroID2 = new SqlParameter("@ID", SqlDbType.NVarChar, 4);
heroID2.Value = heroid2;
OpinionCom.Parameters.Add(heroID2);
SqlParameter opinion1 = new SqlParameter("@result", DBCon);
opinion1.Value = opinion;
OpinionCom.Parameters.Add(opinion1);
OpinionCom.ExecuteNonQuery();
}
}
//投票结果
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("VoteResult.aspx");
}
}
在这里,我详细的给出了数据库中存储过程的引入过程代码。请你自己好好看看,我在之前的一些文章里面都写过一样的代码。在这里讲下,对于两个参数的存储过程,我们只需要定义一个SqlCommand,而后定义两个参数SqlParameters分别来添加我们想要的参数值就行。下面,我们看看图4的后台代码:public partial class VoteResult : System.Web.UI.Page
{
SqlConnection myConn = DBClass.GetConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DVBind();
}
}
public void DVBind()
{
string sqlStr = "select * from Vote";
SqlDataAdapter da = new SqlDataAdapter(sqlStr,myConn);
DataSet ds = new DataSet();
da.Fill(ds,"Vote");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataBind();
}
//处理删除操作
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string delete_sql = "delete from Vote where HeroName='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
bool delete = ExceSQL(delete_sql);//调用ExceSQL执行删除操作
if (delete)
{
Response.Write("<script language=javascript>alert('删除成功!')</script>");
Response.Cookies["userIP"].Expires = DateTime.Now.AddDays(-1);//将这个Cookie过期掉.
Response.Cookies["IPaddress"].Expires = DateTime.Now.AddDays(-1);//将这个Cookie过期掉.
DVBind();//调用自定义方法重新绑定控件中数据
}
else
{
Response.Write("<script language=javascript>alert('删除失败!')</script>");
}
}
public bool ExceSQL(string strSqlCom)
{
//定义数据库连接字符串
string strCon = @"server=.;database=Study;uid=sa;pwd=120110123456;";
//创建数据库连接对象
SqlConnection sqlcon = new SqlConnection(strCon);
SqlCommand sqlcom = new SqlCommand(strSqlCom, sqlcon);
try
{
if (sqlcon.State == System.Data.ConnectionState.Closed)//判断数据库是否为连连状态
{ sqlcon.Open(); }
sqlcom.ExecuteNonQuery();//执行SQL语句
return true;
}
catch
{
return false;
}
finally
{
sqlcon.Close();//关闭数据库连接
}
}
//处理编辑操作
protected void GridView1_Updating(object sender, GridViewUpdateEventArgs e)
{
//取得点击编辑的行数的值
string heroname = GridView1.DataKeys[e.RowIndex].Value.ToString();
//取得更新之后的文本框中的内容
string nameText = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string opinionText = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
//更新SQL语句
//string updateSql = "update Vote set HeroName='" + nameText + "',VoteOpinion='" + opinionText + "' where HeroName='" + heroname + "'";
SqlConnection DBCon = DBClass.GetConnection();
DBCon.Open();
string updateSql = opinionText;
int heroid2 = Convert.ToInt32(heroname);
SqlCommand OpinionCom = new SqlCommand("opinion", DBCon);
OpinionCom.CommandType = CommandType.StoredProcedure;
SqlParameter heroID2 = new SqlParameter("@ID", SqlDbType.NVarChar, 4);
heroID2.Value = heroid2;
OpinionCom.Parameters.Add(heroID2);
SqlParameter opinion1 = new SqlParameter("@result", DBCon);
opinion1.Value = updateSql;
OpinionCom.Parameters.Add(opinion1);
object obj=OpinionCom.ExecuteScalar();
if (obj != null || obj != DBNull.Value)
{
Response.Write("<script>alert('修改成功!')</script>");
//设置GridView控件的编辑项的索引为-1,即取消编辑
GridView1.EditIndex = -1;
DVBind();
}
else
{
Response.Write("<script>alert('修改失败!')</script>");
}
OpinionCom.Dispose();
DBCon.Close();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DVBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DVBind();
}
}
关于代码的介绍,请看下篇文章。