0

这是问题所在:

如果值与TextBox2.Textand in相同Label2.Text,我想这样做,我不会被重定向到下一页,而是会要求我输入不同的文本。这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
   con.Open();
   SqlCommand cmd = new SqlCommand();
   cmd.Connection = con;
   cmd = new SqlCommand("SELECT Question FROM Animals WHERE Question = @Question", con);
   cmd.Parameters.AddWithValue("@Question", Label2.Text);
   cmd.ExecuteNonQuery();
   dr = cmd.ExecuteReader();

   if (dr.Read())
   {
      Response.Write("Enter different question");
   }
   else
   {
      if(dr[0].ToString() != TextBox2.Text)
         Session["question"] = Label2.Text;
         Session["Animal3"] = TextBox1.Text;
         Session["Question2"] = TextBox2.Text;
         Session["Animal2"] = Label3.Text;
         Session["Animal"] = Label4.Text;

         Response.Redirect("~/StartGame2.aspx");
   }
4

2 回答 2

0

您可以在 TextBox2 上使用 CompareValidator

<asp:CompareValidator
        id="Validator1" 
        ControlToValidate="TextBox2" 
        ControlToCompare="Label2" 
        Type="String"
        Text="Enter different text." 
        runat="server" />
于 2013-05-12T13:38:48.147 回答
0

尝试用以下方法替换您的方法:

     protected void Button1_Click(object sender, EventArgs e)
     {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd = new SqlCommand("SELECT Question FROM Animals WHERE Question = @Question", con);
        cmd.Parameters.AddWithValue("@Question", Label2.Text);
        cmd.ExecuteNonQuery();
        dr = cmd.ExecuteReader();

        if (dr.Read())
        {
           if (dr[0].ToString() == TextBox2.Text)
           {
              Response.Write("Enter different question");
              return;
           }
        }

        Session["question"] = Label2.Text;
        Session["Animal3"] = TextBox1.Text;
        Session["Question2"] = TextBox2.Text;
        Session["Animal2"] = Label3.Text;
        Session["Animal"] = Label4.Text;

        Response.Redirect("~/StartGame2.aspx");

     }
于 2013-05-12T15:31:52.863 回答