Response.Redirect不是清除表单。为什么? [英] Response.Redirect is not clearing form. Why?

查看:72
本文介绍了Response.Redirect不是清除表单。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以填写的表单。当用户单击提交按钮时,表单会弹出一条成功消息并重定向回相同的表单,但数据不会被清除。我做错了什么?



<前lang =c#> 受保护 void Submit_Click( object sender,EventArgs e)
{

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ HotConnectionString ]的ConnectionString)。
con.Open();

SqlCommand cmd = new SqlCommand( 插入表格2013(INST_ID,UNITID,ASTUDENTS,ACOMPLETED,ATRANSFERS,BSTUDENTS,BCOMPLETED,BTRANSFERS,YEAR,DATE)值(@ INST_ID,@ UNITID,@ STUDPENTS,@ ACOMPLETED,@ ATRANSFERS,@ BSTUDENTS,@ BCOMPLETED, @BTRANSFERS,@ YEAR,@ DATE)插入表2014(INST_ID,UNITID,ASTUDENTS,ACOMPLETED,ATRANSFERS,BSTUDENTS,BCOMPLETED,BTRANSFERS,YEAR,DATE)值(@ INST_ID,@ UNITID,@ STUDPENTS,@ ACOMPLETED,@ ATRANSFERS, @ BSTUDENTS,@ BCOMPLETED,@ BTRANSFERS,@ YEAR,@ DATE),con);

cmd.Parameters.AddWithValue( @ ASTUDENTS,TextBoxTNUGSC.Text );
cmd.Parameters.AddWithValue( @ ACOMPLETED,TextBoxTNUGSCD.Text);
cmd.Parameters.AddWithValue( @ ATRANSFERS,TextBoxTTOUG.Text);
cmd.Parameters.AddWithValue( @ BSTUDENTS,TextBoxTNGSC.Text);
cmd.Parameters.AddWithValue( @ BCOMPLETED,TextBoxTNGSCD.Text);
cmd.Parameters.AddWithValue( @ BTRANSFERS,TextBoxTTOG.Text);
cmd.Parameters.AddWithValue( @YEAR,TextBoxYEAR.Text);
cmd.Parameters.AddWithValue( @ DATE,TextBoxDATE.Text);
cmd.Parameters.AddWithValue( @ UNITID,TextBoxUNITID.Text);
cmd.Parameters.AddWithValue( @ INST_ID,TextBoxINST_ID.Text);

cmd.ExecuteNonQuery();
con.Close();

if (Page.IsValid)
{
ScriptManager.RegisterStartupScript( this this .GetType(), script alert('您已成功提交群组'); true );
}
else
{
Response.Redirect( Gradrate.aspx);
}


}

解决方案

您只能重定向页面无效...



您的代码:

  if (Page.IsValid)
{
ScriptManager.RegisterStartupScript( this this .GetType(), script alert('您已成功提交同类群组'); true ) ;
}
else
{
Response.Redirect( Gradrate.aspx);
}





你应该写:

  if (Page.IsValid)
{
ScriptManager.RegisterStartupScript( this .GetType(), script,< span class =code-string> alert('您已成功提交同类群组'););

Response.Redirect( Gradrate.aspx);
}





  if (Page.IsValid)
{
ScriptManager.RegisterStartupScript( this this .GetType(), script alert('您已成功提交同类群组'); true );
}

Response.Redirect( Gradrate.aspx );


我已将其添加到代码中。



  if (Page.IsValid)
{
ScriptManager.RegisterStartupScript( this this .GetType(), script alert('您已成功提交群组'););
}
else
{
Response.Redirect( Gradrate.aspx);
}

TextBoxUNITID.Text = string .Empty;
TextBoxTNUGSC.Text = string .Empty;
TextBoxTNUGSCD.Text = string .Empty;
TextBoxTTOUG.Text = string .Empty;
TextBoxTNGSC.Text = string .Empty;
TextBoxTNGSCD.Text = string .Empty;
TextBoxTTOG.Text = string .Empty;

}


I have a form that a user can fill out. When the user clicks the submit button the form pops up a success message and redirects back to the same form but the data is not cleared out. What did I do wrong?

protected void Submit_Click(object sender, EventArgs e)
       {

           SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
           con.Open();

           SqlCommand cmd = new SqlCommand("Insert into Table2013 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE)Insert into Table2014 (INST_ID, UNITID, ASTUDENTS, ACOMPLETED, ATRANSFERS, BSTUDENTS, BCOMPLETED, BTRANSFERS, YEAR, DATE) values (@INST_ID, @UNITID, @ASTUDENTS, @ACOMPLETED, @ATRANSFERS, @BSTUDENTS, @BCOMPLETED, @BTRANSFERS, @YEAR, @DATE)", con);

           cmd.Parameters.AddWithValue("@ASTUDENTS", TextBoxTNUGSC.Text);
           cmd.Parameters.AddWithValue("@ACOMPLETED", TextBoxTNUGSCD.Text);
           cmd.Parameters.AddWithValue("@ATRANSFERS", TextBoxTTOUG.Text);
           cmd.Parameters.AddWithValue("@BSTUDENTS", TextBoxTNGSC.Text);
           cmd.Parameters.AddWithValue("@BCOMPLETED", TextBoxTNGSCD.Text);
           cmd.Parameters.AddWithValue("@BTRANSFERS", TextBoxTTOG.Text);
           cmd.Parameters.AddWithValue("@YEAR", TextBoxYEAR.Text);
           cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
           cmd.Parameters.AddWithValue("@UNITID", TextBoxUNITID.Text);
           cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);

           cmd.ExecuteNonQuery();
           con.Close();

           if (Page.IsValid)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
           }
           else
           {
               Response.Redirect("Gradrate.aspx");
           }


       }

解决方案

You redirect only if page invalid...

Your code:

if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
}
else
{
  Response.Redirect("Gradrate.aspx");
}



You should write:

if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);

  Response.Redirect("Gradrate.aspx");
}


or

if (Page.IsValid)
{
  ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
}

Response.Redirect("Gradrate.aspx");


I have added this to the code.

if (Page.IsValid)
           {
               ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('You Have Successfully Submitted the Cohort');", true);
           }
           else
           {
               Response.Redirect("Gradrate.aspx");
           }

           TextBoxUNITID.Text = string.Empty;
           TextBoxTNUGSC.Text = string.Empty;
           TextBoxTNUGSCD.Text = string.Empty;
           TextBoxTTOUG.Text = string.Empty;
           TextBoxTNGSC.Text = string.Empty;
           TextBoxTNGSCD.Text = string.Empty;
           TextBoxTTOG.Text = string.Empty;

       }


这篇关于Response.Redirect不是清除表单。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆