我做的程序时一个页面中又75个指标,用户要对这75个指标进行打分,打分后会存储到数据库中
现在有一个BUTTON_CLICK的事件。这个事件里包括这么几个功能:
1 判断这75个指标是否都进行了评测,如果有没评测的指标,就会提示“评分没有完成,请从新评分”
2 如果都进行了评测,将数据存储到数据库中
3 另外还有一个TextBox1_TextChanged事件就是用于确定:之前已经评测过的年份不能再进行评测了
现在出现了这么一个问题:
如果我评测没有全部进行完成,系统会提示'评分没有完成,请从新评分',然后返回。可是我再想从新进行评分的时候,数据库中已经有这个年份的数据行了,所以会提示你“此年已经进行过测评了”,就无法再继续评分了。
有人曾经建议说加上一个break,可是我试了试,还是不行。请大家指点一下吧
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
for (int h = 1; h <= 75; h++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + h) as DropDownList;
int a = int.Parse(ddl.SelectedValue);
if (a == 10)
{
Response.Write("<script>alert('评分没有完成,请从新评分');location='ZHtjqrx.aspx'</script>");
break;
}
}
for (int i = 1; i <= 75; i++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + i) as DropDownList; if (ddl != null)
{
int a = int.Parse(ddl.SelectedValue);
int b = Convert.ToInt32(TextBox1.Text); SqlConnection con = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=12345;");
try
{
con.Open(); string str = "update ZHtjqrx set zb" + i + "='" + a + "'where year='" + b + "'";
SqlCommand cmd = new SqlCommand(string.Format(str), con);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{ con.Close();
}
}
} protected void TextBox1_TextChanged(object sender, EventArgs e)
{
int y = Convert.ToInt32(TextBox1.Text);
SqlConnection comm = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=12345;");
SqlDataReader dr = null;
try
{
comm.Open();
string select_count = "select count(*) from ZHtjqrx where year=" + y + "";
SqlCommand cmd = new SqlCommand(select_count, comm); dr = cmd.ExecuteReader();
if (dr.Read() && Convert.ToInt32(dr[0]) > 0)
{
Response.Write("<script>alert('此年已经进行过测评了!');location='ZHtjqrx.aspx'</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (dr != null)
dr.Close();
comm.Close();
}
现在有一个BUTTON_CLICK的事件。这个事件里包括这么几个功能:
1 判断这75个指标是否都进行了评测,如果有没评测的指标,就会提示“评分没有完成,请从新评分”
2 如果都进行了评测,将数据存储到数据库中
3 另外还有一个TextBox1_TextChanged事件就是用于确定:之前已经评测过的年份不能再进行评测了
现在出现了这么一个问题:
如果我评测没有全部进行完成,系统会提示'评分没有完成,请从新评分',然后返回。可是我再想从新进行评分的时候,数据库中已经有这个年份的数据行了,所以会提示你“此年已经进行过测评了”,就无法再继续评分了。
有人曾经建议说加上一个break,可是我试了试,还是不行。请大家指点一下吧
代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
for (int h = 1; h <= 75; h++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + h) as DropDownList;
int a = int.Parse(ddl.SelectedValue);
if (a == 10)
{
Response.Write("<script>alert('评分没有完成,请从新评分');location='ZHtjqrx.aspx'</script>");
break;
}
}
for (int i = 1; i <= 75; i++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + i) as DropDownList; if (ddl != null)
{
int a = int.Parse(ddl.SelectedValue);
int b = Convert.ToInt32(TextBox1.Text); SqlConnection con = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=12345;");
try
{
con.Open(); string str = "update ZHtjqrx set zb" + i + "='" + a + "'where year='" + b + "'";
SqlCommand cmd = new SqlCommand(string.Format(str), con);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{ con.Close();
}
}
} protected void TextBox1_TextChanged(object sender, EventArgs e)
{
int y = Convert.ToInt32(TextBox1.Text);
SqlConnection comm = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=12345;");
SqlDataReader dr = null;
try
{
comm.Open();
string select_count = "select count(*) from ZHtjqrx where year=" + y + "";
SqlCommand cmd = new SqlCommand(select_count, comm); dr = cmd.ExecuteReader();
if (dr.Read() && Convert.ToInt32(dr[0]) > 0)
{
Response.Write("<script>alert('此年已经进行过测评了!');location='ZHtjqrx.aspx'</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (dr != null)
dr.Close();
comm.Close();
}
我的意思是验证判断是否输入正确之类的,完全应该放在客户端进行!
例如你的
if (a == 10)
......
这样在后台判断,要回发服务器一次,放在客户端用js验证,就不需要回发服务器!!
也可ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "", "if(Conform('')) document.getElemnyById('hf').value='1'; else document.getElemnyById('hf').value='0'; ", true);
if(hf.Value.Equals("1"))
{
}
{
Response.Write("<script>alert('评分没有完成,请从新评分');location='ZHtjqrx.aspx'</script>");
break;改为 return;
}
}应该可以了。