printing a page after checking validator - c#

i want that on button click it check first validator then print command is executed
here is my ASPX Code:
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<style type="text/css">
.style1
{
}
#media print
{
.header, .style1, .footer,.hide
{
display:none
}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RequiredFieldValidator" style="color: #FF0000"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="window.print();" />
</asp:Content>
problem is that when i click on button it execute the print function and dont check validator.

The JavaScript OnClientClick will fire before your page is validated. You could try validating the page before printing it.
function Validate() {
if(Page_ClientValidate())
window.print();
}
Then call it like so.
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();" />

Set the CausesValidation Property of the button to true!

Related

Modal Popup Extender Not Working

I have this piece of code in my aspx page open a modal popup. I found this code online but for some reason the page refreshes and the popup does not appear. I tried several code snippets but the issue keeps on occurring. It must be an issue with my page.
This is my .aspx code
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<style type="text/css">
.ModalPopupBG
{
background-color: Gray;
filter: alpha(opacity=70);
opacity: 0.7;
}
.popup
{
background:white;
}
</style>
<script>
function onAgree() {
alert("Thank you for your approval")
}
function onDisagree() {
alert("I appreciate your feedback; i will work harder next time")
}
</script>
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<asp:Button ID="Button1" runat="server" Text="feedback"/>
<asp:Panel ID="Panel1" runat="server" CssClass ="popup">
please give me your feedback about the paragraph above?
<hr />
<asp:Button ID="btnagree" runat="server" Text="Agree" />
<asp:Button ID="btndisagree" runat="server" Text="Disagree" />
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender1"
runat="server"
TargetControlID ="Button1"
PopupControlID="Panel1"
BackgroundCssClass="modalBackground"
DropShadow="true"
OkControlID="btnagree"
CancelControlID="btndisagree"
OnOkScript="onAgree()"
OnCancelScript ="onDisagree()">
<Animations>
<OnShown>
<Fadein />
</OnShown>
<OnHiding>
<Fadeout />
</OnHiding>
</Animations>
</asp:ModalPopupExtender>
</div>
</asp:Content>

radiobutton.checked always is false after postback

I have 3 radiobutton control and a submit button in my aspx page which are inside a Content control.
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:RadioButton ID="rd0" runat="server" GroupName="g1" Checked="true" />
<asp:Image ID="img0" runat="server" ImageUrl="/image/img0.png" />
<asp:RadioButton ID="rd1" runat="server" GroupName="g1" />
<asp:Image ID="img1" runat="server" ImageUrl="/image/img1.png" />
<asp:RadioButton ID="rd2" runat="server" GroupName="g1" />
<asp:Image ID="img2" runat="server" ImageUrl="/image/img2.png" />
</asp:Panel>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</asp:Content>
The user can select one of these radiobuttons and press submit button.
Now in code behind i want to diagnose which radiobutton is selected,
RadioButton checkedButton;
if (rd0.Checked)
checkedButton = rd0;
else if (rd1.Checked)
checkedButton = rd1;
else
checkedButton = rd2;
but the checked property of all of the radiobuttons is false.
I have even set the checked property of the first radiobutton to true but it becomes to false after postback again.
Use this in your .cs:
Request.Form["YourRadioButton"]
Do you have PostBack control in your Page_Load code section?
Try to add following code to your Page_Load event.
if(IsPostBack) return;

ASP.NET ValidationGroup not working server-side

I have a page with two sets of controls, with ValidationGroup set differently for each set, but if I attempt to fill in one form correctly, the validators for the other form stop the Click event from firing.
I've noticed that it's behaving normally client-side, but the button then causes a postback event and the server validators do not seem to be honouring the validation groups. I'm unsure what I'm doing wrong. Surely the built in validators all support validation groups on the server side?
Below is a complete standalone example:
<%# Page Language="C#" %>
<script runat="server">
protected void SubmitButton_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid) label.Text = "OK: " + ((Button)sender).ID.ToString();
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<style>
body { font-family: sans-serif;}
.warning { color: red;}
</style>
</head>
<body>
<form id="f1" runat="server">
<h1>Validator Test</h1>
<asp:Label ID="label" runat="server" Font-Bold="true" ForeColor="Green" EnableViewState="false" /><br />
<div style="width:40%; float:left; height:200px; border:1px solid #eee; padding:15px; margin:5px;">
<h2>Left Form:</h2>
Email: <asp:TextBox ID="leftBox" runat="server" Columns="20" MaxLength="80" Width="160px" ValidationGroup="LEFT" />
<asp:RequiredFieldValidator ID="left2" runat="server" ControlToValidate="leftBox"
ErrorMessage="You must enter a value." ValidationGroup="LEFT">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev8" runat="server" ControlToValidate="leftBox" ValidationGroup="LEFT" Text="*"
ErrorMessage="Email address format is invalid." ValidationExpression="^[\.\-\w]+#([\w\-]+\.)+\w+$" />
<asp:Button ID="LeftSubmitButton" runat="server" Text="Save Left" OnClick="SubmitButton_Click" ValidationGroup="LEFT" />
<asp:ValidationSummary ID="LeftValidationSummary" runat="server" HeaderText="Error:" ValidationGroup="LEFT" CssClass="warning" />
</div>
<div style="width:40%; float:left; height:200px; border:1px solid #eee; padding:15px; margin:5px;">
<h2>Right Form:</h2>
Email: <asp:TextBox ID="rightBox" runat="server" Columns="20" MaxLength="80" Width="160px" ValidationGroup="RIGHT" />
<asp:RequiredFieldValidator ID="rfv9" runat="server" ControlToValidate="rightBox"
ErrorMessage="You must enter an value." ValidationGroup="RIGHT">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev10" runat="server" ControlToValidate="rightBox" ValidationGroup="RIGHT" Text="*"
ErrorMessage="Email address format is invalid." ValidationExpression="^[\.\-\w]+#([\w\-]+\.)+\w+$" />
<asp:Button ID="RightSubmitButton" runat="server" Text="Save Right" OnClick="SubmitButton_Click" ValidationGroup="RIGHT" />
<asp:ValidationSummary ID="RightValidationSummary" runat="server" HeaderText="Error:" ValidationGroup="RIGHT" CssClass="warning" />
</div>
</form>
</body>
</html>
Remove the explicit full page validation - that is this line:
Page.Validate(); it disregards the validation groups.
You'll notice that Page.Validate() has also override that allows you to specify the exact validation group - like Page.Validate("RIGHT"). This is intended for framework purposes; web form infrastructure already calls the proper Validate() for you so no need to call it explicitly.

asp.net how to close a dialoge box using intrapage linking in asp

I made a dialog box showing wrong password or username error but this doesn't close.
how to close this dialog box without refreshing the page,Code is like
<asp:Panel ID="errorMsg" runat="server" Visible="false">
<asp:Label ID="msg" Text="" runat="server"></asp:Label>
<asp:Label ID="errorHead" Text="Something is really Wrong :" runat="server"></asp:Label>
<asp:button ID="try" OnClick="try_Click" runat="server" Text="Try Again" />
</asp:Panel>
Code Behind :
protected void try_Click(object sender, EventArgs e)
{
errorMsg.Visible = false;
}
errorMsg.visible is set to true when wrong input is made but after making it again false it doesn't close.
The OnClick event will fire a PostBack refreshing the page.
You could try using javascript:
<script text="text/javascript">
function hide(id){
if(id)
document.getElementById(id).style.display = "none";
}
</script>
<asp:Button ID="try" OnClientClick='hide("<%=errorMsg.ClientId%>")'
runat="server" Text="Try Again" />
Or you could simply wrap the dialogbox with an Update Panel. The Update Panel allows you only to refresh a part of your website.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="errorMsg" runat="server" Visible="false">
...
<asp:Button ID="try" OnClick="try_Click"
runat="server" Text="Try Again" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

fire a textbox's OnTextChanged while enabled=false

I have an ajax toolkit calendar extender attached to my textbox and i a trying to fire the OnTextChanged after the date was entered into the textbox.
The thing is i don't want the user to enter data manually so i disabled the textbox but the event won't fire for a disabled text box.
is there anyway around it? i thought about changing to a label but is doesn't have OnTextChanged event.
Thanks
<asp:TableCell CssClass="cssWidth" Width="150px">
<asp:CalendarExtender ID="CalendarExtender1" PopupButtonID="Image1" runat="server" TargetControlID="TextBoxAddDate" Format="dd/MM/yyyy"></asp:CalendarExtender>
<asp:TextBox ID="TextBoxAddDate" ReadOnly="true" CssClass="cssWidth" ToolTip="תאריך הוספה" runat="server" Style="font-size: large;background-color:aliceblue;" AutoPostBack="true" AutoCompleteType="Search" MaxLength="0" TextMode="SingleLine" OnTextChanged="txtSearch_TextChanged" ViewStateMode="Enabled" autocomplete="off" >
</asp:TextBox>&nbsp
<asp:ImageButton runat="Server" ID="Image1"
ImageUrl="~/Calendar_scheduleHS.png" AlternateText="Click to show calendar" /><br />
</asp:TableCell>
Can you use ReadOnly instead of Enabled = false?
UPDATE:
Ok, this is the complete solution that worked for me:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
function dateSelectionChanged(x) {
debugger;
javascript: __doPostBack('TextBoxAddDate', '')
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit www.asp.net.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
</p>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CalendarExtender ID="CalendarExtender1" PopupButtonID="Image1" runat="server" TargetControlID="TextBoxAddDate" Format="dd/MM/yyyy" OnClientDateSelectionChanged="dateSelectionChanged" ></asp:CalendarExtender>
<asp:TextBox ID="TextBoxAddDate" CssClass="cssWidth" ToolTip="תאריך הוספה" runat="server" Style="font-size: large;background-color:aliceblue;" AutoCompleteType="Search" MaxLength="0" TextMode="SingleLine" OnTextChanged="txtSearch_TextChanged" ViewStateMode="Enabled">
</asp:TextBox>&nbsp
<asp:ImageButton runat="Server" ID="Image1"
ImageUrl="~/Calendar_scheduleHS.png" AlternateText="Click to show calendar" /><br />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
It seems that the CalendarExtender "steals" away the events from the texbox it uses.
Using this approach I managed to fire up the OnTextChanged event and the handler txtSearch_TextChanged gets executed.
Did you make the textbox ReadOnly?

Categories