Use of Validation Group in asp.net - c#

I can't not to use Validation Group in asp.net c#.
I have searched solution in Google without success.
In this simple form asp.net the TextBox pcs is always validated even when the value is null or the value is not a number.
My code asp.net below, what is wrong?
HTML:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="pcs" runat="server" Width="100" CssClass="ddl_Class" ValidationGroup="First"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="pcs"
ErrorMessage="PCS !" Text="***" Display="None" ValidationGroup="First"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorPcs" runat="server" ControlToValidate="pcs"
ErrorMessage="PCS only number" Text="***" Display="None" ValidationExpression="^\d+$" ValidationGroup="First"></asp:RegularExpressionValidator>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="/Images/save_button.gif" OnClientClick="return confirm('Confirm ?');" ValidationGroup="First" CausesValidation="true" />
<asp:ValidationSummary ID="First" runat="Server" ShowMessageBox="true" />
</div>
</form>
</body>
</html>
EDIT 1
The problem is the OnClientClick event.
If delete this event in asp:ImageButton the validation group working.
Why?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="pcs" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="pcs"
ErrorMessage="PCS !" Text="***" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="pcs"
ErrorMessage="PCS only number" Text="***" Display="None" ValidationExpression="^\d+$" EnableClientScript="true"></asp:RegularExpressionValidator>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="/Images/save_button.gif" />
</div>
<asp:ValidationSummary ID="First" runat="Server"
ShowMessageBox="true" />
</form>
</body>
</html>

OnClientClick is a useful property introduced in ASP.NET 2.0 that allows us to add some client-side behavior to button control. Using is as simple as providing the script to be called when a button is clicked by a user:
<asp:Button runat="server" ID="Save"
OnClientClick="return confirm('Are you sure?');" />
The problem is that if you use it like that, client-side validation won't fire. Looking at the rendered HTML quickly explains the situation:
onclick="return confirm('Are you sure?');WebForm_DoPostBackWithOptions(...)"
As you can see, the validation doesn't even have a chance to fire (which happens when WebForm_DoPostBackWithOptions is called).
Solving the issue is simple (or not). All that has to be done is a little change in our OnClientClick script (a piece of code found somewhere on the Internet):
<asp:Button runat="server" ID="Save"
OnClientClick="if (!confirm('Are you sure?')) return false;" />
Now we only return false (preventing the submit) in case a user didn't confirm the action, otherwise, the rest of the script will be called thus firing validation.
The reason I said that it may not be a simple issue is the fact, that the validation happens AFTER the confirmation, which is not the best thing in my opinion. Why ask the user about saving his data if there are still errors on the form, of which we will inform him after he confirms that he wants to save it?
After analyzing a bit, the code responsible for dealing with OnClientScript, I have come to a conclusion, that solving this problem is not an easy task. It would require some dirty hacks on the server side to make it pretty or calling validation routines on the client, before displaying the confirmation dialog (keeping in mind that checking if there are validation routines present at all is necessary in this case).
Used From : http://vaultofthoughts.net/OnClientClickBreaksValidation.aspx

Related

ASP.NET Web Form, Client Side Enter Key to Submit (WM 5.0)

Currently trying to fix an ASP.NET web application. Everything works fine, except being able to press the "enter" or "return" key to submit the form, from a Windows Mobile 5.0 device.
Basically, the site has one text box (the search box) and one button that has a PostBackUrl attached to it. Pressing the button works fine, but the device that is being used, mainly, has a big enter key as well as a scanner that uses the "enter" or "return" function. When the "enter" key is pressed down, or a scan is made and "enter" is passed along, it appears to refresh the page but will not submit.
I have tried numerous suggestions such as: defaultbutton attribute, wrapping in a panel and designating defaultbutton, javascript, usesubmitbehavior, and more. I have spent hours on this and haven't gotten anywhere. Your help is greatly appreciated!
I have taken everything back down to the base code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="searchcode.aspx.cs" Inherits="search_inventory_control_searchcode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server" defaultfocus="CodeSearchBox" defaultbutton="SearchButton">
<div class="auto-style1">
<a href="index.aspx">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/logo.png" />
</a>
</div>
<p class="auto-style1">
<strong>Search Inventory by Product Code</strong></p>
<p class="auto-style1">
<asp:TextBox ID="CodeSearchBox" runat="server" Width="180px"></asp:TextBox>
</p>
<p class="auto-style1">
<asp:Button ID="SearchButton" runat="server" Text="Search" Width="180px" Height="30px" PostBackUrl="~/search/inventory_control/resultscode.aspx" />
</p>
</div>
</form>
</body>
</html>

ASP.NET Web widget deployment

I've created my first simple widget. It has some textboxes for input and output data and a button. After clicking it, it connects to web service through soap and send back needed data. Now, I've stumbled upon the issue of its deployment. What's the simplest way to deploy it on any sample site without it interfering with the content? How do I approach this and what should I do?
Here's my WebForm.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WidgetTest3.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Thank you for your time.
The simplest way to deploy this from within Visual Studio is to use the Build -> Publish command and choose the File System publish method.
To prevent it from interfering with existing files, make sure that the "Delete all existing files prior to publish" is unchecked in the File Publish Options section of the Publish Web dialog.

How to hyperlink list from Access database in C# ASP.Net

My assignment states: Create a site that is similar to Figure 8.9 in ASP.NET 4 Unleashed, but instead of a list of movie hyperlinks, create a list of your favorite activities (sports, reading, shopping etc.) The image in question is just a simple vertical listing of hyperlinked movies. I am running into issues as it will not build correctly though I have no errors or warnings. I do have a message that states
"Message 1 Validation (ASP.Net): Attribute 'ConnectionString' is not a valid attribute of element 'AccessDataSource'. c:\users\owner\documents\visual studio 2010\Projects\Activities\Activities\Activities.aspx 41 5 Activities"
My code is as follows
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Activities.aspx.cs"
Inherits="Activities.Activities" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<style type="text/css">
.floater
{
float:left;
border:solid 1px black;
padding:5px;
margin:5px;
}
</style>
<title>Activities</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater
id="Repeater1"
DataSourceId="srcActivities"
Runat="server">
<ItemTemplate>
<asp:HyperLink
id="HyperLink1"
Text='<%# Eval("Type") %>'
NavigateUrl='<%# Eval("Activities.aspx?id={0}") %>'
runat="server" />
<br />
</ItemTemplate>
</asp:Repeater>
<asp:AccessDataSource
id="srcActivities"
ConnectionString="Data Source=.\Access;
AttachDbFilename=|Desktop|WD364|Activities.accdb;
Integrated Security=True;User Instance=True"
SelectCommand="SELECT Id, Type FROM Activities"
Runat="server" />
</div>
</form>
</body>
</html>
The database is a simple MS Access Table (there is only one) with a few rows and two columns. One the ID and one the Type with the names of the activities. Any help would be greatly apprecitated
Try this
<asp:hyperlink id="hlxx" runat="server"
NavigateUrl='<%# "~/Activities.aspx?id="+Eval("Type") %>'
Target='<%# "_blank" %>'>Eval("Type")</asp:hyperlink>
Or see this
Send string with QueryString in Repeater Control in ASP.net
and below link have multiple answers
Link

WebForms submitting specific form with enter key (not clicking)

Say I have one full-page form, but within the form, there are two or more events that need to take place on submission: Login & Register
Site.Master:
<%# Master Language="C#" AutoEventWireup="true"
EnableViewState="true" CodeBehind="Site.Master.cs"
Inherits="Site.SiteMasterPage" %>
<!doctype>
<html>
<head runat="server">
<%-- stuff --%>
</head>
<body>
<form ID="MainForm" action="" runat="server">
<asp:Login id="LoginControl" runat="server" />
<asp:CreateUserWizard id="RegisterControl" runat="server" />
</form>
</body>
</html>
If my cursor is focused inside of an input type="text" for asp:Login, and I hit Return (with javascript off), the page submits, but I am not logged in.
The same thing happens when I attempt to register (filling out the createUserWizard and hitting the Return key instead of actually clicking "Register", firing some event)
Is there any non-JavaScript solution for getting the Return key to submit the proper, currently focused portion of the form?
The panel control allows you to define a default button within the scope of it's contents:
<asp:Panel runat="server" DefaultButton="submitButtonA">
<asp:LinkButton ID="submitButtonA" runat="server" Text="Submit A"/>
</asp:Panel>
<asp:Panel runat="server" DefaultButton="submitButtonB">
<asp:LinkButton ID="submitButtonB" runat="server" Text="Submit A"/>
</asp:Panel>
The default button sounds like it might be your friend tonight - http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.defaultbutton.aspx
Actually it might not be, I haven't ever tried it with no Javascript.

why don't recognize the textfield and the button like component's asp

i have in my code a table with a text field and a button on each cell. My problem it's that the button and the field are not recognized like System.Web.UI.WebControls.Button and System.Web.UI.WebControls.TextBox respectively. In fact recognize like buton are textfield html plain. I'm going to put my code so if anybody can see what i'm doing wrong.
Note: If i put this elemets out of the table work so i assumed that something wrong in the table
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MapaPrueba._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Uso de google map</title>
<link href='/assets/css/styles.css' rel='stylesheet' type='text/css' />
</head>
<body>
<form id="form1" runat="server">
<asp:Panel runat="server" Height="1024" Width="768" style="text-align: center">
<div id="map-canvas" style="width: 700px; height: 500px" align="center"></div>
<asp:Table id="Table1" runat="server" CellPadding="10" GridLines="Both" HorizontalAlign="Center">
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
</form>
</body>
</html>
I would follow atrljoe's suggestion. Don't use an asp table, just use
<table>
<tr>
<td>
controls go here
</td>
<tr>
</table>
Tip: If you add the table first, switch in VS to Design View and using Ctrol + Alt + arrows you can add new rows and columns easily
You cant access the controls because your placing a Control (ASP Button & ASP TextBox) within a Control (ASP Table) ASP.NET Table. Every ASP.net page has a control tree, all the controls (HTML and server controls are in this control tree based on their position in page hierarchy). Thus since it is contained within the Control, the system is not picking it up. To gain access to this control you would need to use FindControl in your code behind.
As I had asked, If you really need to control this button and textbox, then consider changing to an HTML table. Otherwise when you reference these controls in your code behind you will need to use FindControl

Categories