using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Samples
{
	/// <summary>
	/// ValidNow 的摘要说明。
	/// </summary>
	public class ValidNow : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.ValidationSummary validationSummary;
		protected System.Web.UI.WebControls.Label price;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredSharesValidator;
		protected System.Web.UI.WebControls.RangeValidator RangeSharesValidator;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredTickerValidator;
		protected System.Web.UI.WebControls.CustomValidator CustomTickerValidator;
		protected System.Web.UI.HtmlControls.HtmlInputText symbol;
		protected System.Web.UI.HtmlControls.HtmlInputText shares;
		protected System.Web.UI.HtmlControls.HtmlInputHidden flag;
		protected System.Web.UI.HtmlControls.HtmlGenericControl buyDiv;
		protected System.Web.UI.HtmlControls.HtmlInputButton submitButton;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.submitButton.ServerClick += new System.EventHandler(this.submitButton_ServerClick);
			this.ID = "form";
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void submitButton_ServerClick(object sender, System.EventArgs e)
		{
			//判断页面所有验证控件是否有效
			if ( Page.IsValid && Request["buttonClick"]=="true")
			{
				Response.Write("<script language=javascript>");
				Response.Write("window.alert('验证通过，保存成功！')");
				Response.Write("</script>");
			}
		}
		protected void ServerValidateTicker( object source, ServerValidateEventArgs args)																													
		{
			string value = symbol.Value.Trim();
			
			// 如果提交按钮单击了，则股票名称已经通过验证
			if(Request["buttonClick"]!="true")
			{
				//判断股票名称是否有效，这里可以通过从数据库中读取数据来判断
				if ( value == "AAA" ||  value == "BBB")
				{
					args.IsValid = true;
					price.Text = "@100";
				}	
				else
				{
					price.Text = string.Empty;
					args.IsValid = false;
				}
			}
			else
			{
				args.IsValid = true;
			}
			//这里不希望验证“数量”的验证控件有效，通过代码设置验证控件有效，可跳过验证控件进行验证
			if ( shares.Value.Trim() == string.Empty )
			{
				RequiredSharesValidator.IsValid = true;
			}
		}
	}
}
