Please give advice on itt . also want if date invalid it should same page plus .use whatever break do not excute more
code inside button click event if date is not valid
protected void btn_ok_Click(object sender, EventArgs e)
{
//DateTime dt = new DateTime();
//dt = Convert.ToDateTime(tb_age.Text);//"2011-12-30"
//TimeSpan ts = DateTime.Now.Subtract(dt);
//int y = ts.Days / 365;
DateTime dtStart = DateTime.Parse(tb_age.Text);
TimeSpan sp = DateTime.Now - dtStart;

if (sp.Days < 18 * 365)
{
Response.Write("<script>alert('Age not validated. You are year old.')</script>");

tb_age.Focus();

}
else { Response.Write("<script>alert('Age valid')</script>"); }

Label1.Visible = true;
Label1.Text = " Why are you runnning in button click if not valid age if valid than ok ";

}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = false;
}
Collapse | Copy Code
<form id="form1" runat="server">

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<h4>Date format must be like :07/01/1989(MM/DD/YYYY)</h4>
<asp:TextBox ID="tb_age" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="MM/dd/yyyy" TargetControlID="tb_age">
</asp:CalendarExtender>
<asp:Button ID="btn_ok" runat="server" Text="Test Age" onclick="btn_ok_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</div>
</form>

Recommended Answers

All 2 Replies

Its slightly hard to understand what you want but I have answered the best I can.

You have a } after ("<script>alert('Age valid')</script>"); which means that it will ALWAYS run the code below.

Label1.Visible = true;
Label1.Text = " Why are you runnning in button click if not valid age if valid than ok ";

Fix:

Remove the } from the end of this line:
else { Response.Write("<script>alert('Age valid')</script>"); }

I hope this is what you were asking.

Really difficult to figure out what's being asked here, but I believe you're trying to exit the sub at a particular point. Use "return" to exit the sub without executing any more code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.