积极答复者
response.write出来的窗口为什么要点两次确定?

问题
-
在formview中我设置了一个CustomValidator控件,当验证为通过时,会弹出“合同号已存在!”的错误提示窗口。
奇怪的是,现在窗口会弹出来两次,也就是要我点两下弹出窗口的“确定”才行,这该怎么解决啊?
下面是可能相关的2部分代码,请大家帮忙看看,谢谢!-----------------------------前台----------------------------
<asp:FormView ID="FormView1" runat="server" DataKeyNames="CID" DataSourceID="SqlDataSource_FormView" HorizontalAlign="Center" BorderColor="#CCCCCC"> <asp:CustomValidator ID="CustomValidator_ContractNumber" runat="server" Font-Size="10px" onservervalidate="CustomValidator_ContractNumber_ServerValidate" Display="Dynamic" ErrorMessage="合同号已存在"></asp:CustomValidator> </asp:FormView>
-------------------------后台---------------------------
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["UName"].ToString() != "ccc") { Response.Redirect("~/index.htm"); } else { Session.Timeout = 30; string str1 = ((Label)this.FormView1.FindControl("Label_Pic")).Text; //Response.Write(str1); if (str1 != "") { ((HyperLink)this.FormView1.FindControl("HyperLink_Pic")).NavigateUrl = "~/Pictures/ContractPic\\" + str1; } else { ((HyperLink)this.FormView1.FindControl("HyperLink_Pic")).NavigateUrl = "~/indexnull.htm"; } } } } protected void CustomValidator_ContractNumber_ServerValidate(object source, ServerValidateEventArgs args) { string strconn = "Data Source=.;Initial Catalog=长铁物业公司资产数据库;Integrated Security=True"; SqlConnection cn = new SqlConnection(strconn); cn.Open(); //string strConditionLabel = ((Label)this.FormView1.FindControl("ContractNumberLabel")).Text; string strContractNumber = ((TextBox)this.FormView1.FindControl("ContractNumberTextBox")).Text; string strCID = ((Label)this.FormView1.FindControl("CIDLabel")).Text; string strSearch = "BEGIN TRAN UPDATE ContractTable SET ContractNumber='" + strContractNumber + "' WHERE CID=" + strCID + " IF ( SELECT COUNT(*) FROM ContractTable WHERE ContractNumber='" + strContractNumber + "' ) <2 BEGIN ROLLBACK TRAN SELECT '1' END ELSE BEGIN ROLLBACK TRAN SELECT '0' END"; //Response.End(); //取sql事务的结果,1或者0 SqlCommand comm = new SqlCommand(strSearch, cn); string strResult = comm.ExecuteScalar().ToString(); if (strResult == "1") { args.IsValid = true; } else { args.IsValid = false; Response.Write("<script language=javascript>alert('合同号已存在!');</script>"); } cn.Close(); }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
2013年8月2日 2:47
答案
-
CustomerValidation可以设定是否客户端验证,如果要和服务端的那边联系,恐怕需要要异步机制。不过我可以给你演示用服务端代码弹出对话框:
public partial class Default : System.Web.UI.Page { protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { //必须为1 if (args.Value != "1") { ClientScript.RegisterStartupScript(GetType(), "s", "alert('必须为1!');", true); args.IsValid = false; } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 linjiangxian11 2013年8月20日 8:07
2013年8月17日 8:26
全部回复
-
Response.Write("<script language=javascript>alert('合同号已存在'));
这一句删除——因为没有必要,CustomerValidation会自己验证,一旦设置IsValid=false,则自动弹出ErrorMessage的东西。If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report2013年8月2日 3:23 -
把这个控件的Text设置为"*",然后删除"ErrorMessage"属性,
注意我代码最下面if……部分:
protected void CustomValidator_ContractNumber_ServerValidate(object source, ServerValidateEventArgs args) { string strconn = "Data Source=.;Initial Catalog=长铁物业公司资产数据库;Integrated Security=True"; SqlConnection cn = new SqlConnection(strconn); cn.Open(); //string strConditionLabel = ((Label)this.FormView1.FindControl("ContractNumberLabel")).Text; string strContractNumber = ((TextBox)this.FormView1.FindControl("ContractNumberTextBox")).Text; string strCID = ((Label)this.FormView1.FindControl("CIDLabel")).Text; string strSearch = "BEGIN TRAN UPDATE ContractTable SET ContractNumber='" + strContractNumber + "' WHERE CID=" + strCID + " IF ( SELECT COUNT(*) FROM ContractTable WHERE ContractNumber='" + strContractNumber + "' ) <2 BEGIN ROLLBACK TRAN SELECT '1' END ELSE BEGIN ROLLBACK TRAN SELECT '0' END"; //Response.End(); //取sql事务的结果,1或者0 SqlCommand comm = new SqlCommand(strSearch, cn); string strResult = comm.ExecuteScalar().ToString(); if (strResult != "1") { Response.Write("<script language=javascript>alert('合同号已存在!');</script>"); } args.IsValid = (strResult=="1");
cn.Close();If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已建议为答案 Fred BaoModerator 2013年8月9日 10:04
2013年8月2日 6:00 -
谢谢解答,但是问题依旧!我觉得这不是if语句引起的问题,而是服务器做出了两次反应!
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2013年8月5日 1:29
2013年8月5日 1:29 -
谢谢解答,但是问题依旧!我觉得这不是if语句引起的问题,而是服务器做出了两次反应!
除了以上代码,你有没有启用Client(脚本检查)?
有没有写过一些脚本验证的代码?
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report2013年8月6日 8:22 -
很奇怪哦!我还有其他的控件,但只有这个控件是自定义判断的。
我尝试用validationSummary,其他的验证控件都能弹出错误提示窗口,唯独这个CustomValidator没反应,不能弹出窗口,但可以正确执行验证。
这个问题号奇怪啊!
也没什么脚本验证!
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2013年8月13日 0:32
2013年8月13日 0:28 -
CustomerValidation可以设定是否客户端验证,如果要和服务端的那边联系,恐怕需要要异步机制。不过我可以给你演示用服务端代码弹出对话框:
public partial class Default : System.Web.UI.Page { protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { //必须为1 if (args.Value != "1") { ClientScript.RegisterStartupScript(GetType(), "s", "alert('必须为1!');", true); args.IsValid = false; } } }
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 linjiangxian11 2013年8月20日 8:07
2013年8月17日 8:26