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.
Related
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>
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
I have created a user control in asp.net which contains a form and a button which saves the data. So when the popup loads the user control is inside. I click on the button to save the form data and the click event is not fired?
this is a jquery popup below with the user control inside.
<div id="dialog_form" title="Create new user">
<uc1:SettingsPopUp runat="server" />
</div>
here is the user control
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="SettingsPopUp.ascx.cs" Inherits="FRDashboard.Content.Dashboards.SettingsPopUp" %>
<link href="../../Styles/styles.css" rel="stylesheet" />
<link rel="Stylesheet" href="../../Styles/bootstrap.css" type="text/css"/>
<div id="divChartSettings" runat="server">
<asp:UpdatePanel ID="upd_chart_settings" runat="server" UpdateMode="Conditional">
<ContentTemplate>
//FORM ELEMENTS GO HERE
<asp:Button ID="applySettings" runat="server" OnCommand="applyChartSettings_Click" EnableViewState="true" Text="Save" CssClass="button" />
<asp:Label ID="lbl_message" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Change your aspx page,
<uc1:SettingsPopUp runat="server" /> to
<uc1:SettingsPopUp runat="server" ClientIDMode="AutoID"/>
Because you are using the button control inside an asp:UpdatePanel.
Fixed the problem. The user control was in a JQuery UI popup which was stopping the postback. The following fixed the problem:
dialog.parent().appendTo(jQuery("form:first"));
I'm having surprisingly bad luck finding a decent resource for this. But What I'm doing is creating a list of tickets using a ListView. One of the controls in the item template is a link button that shows the header of the ticket. When the user clicks the link button, I want to open a modal window that shows the ticket in readable format. To get the ticket, I'll be passing that page a variable and it'll do the rest; nothing complicated. The page will have 3 buttons and the windows needs to close when you click one of them.
Now, I figured out how to use the ModalPopupExtender from the Ajax tookit more or less. You create the panel in it's own div which stays hidden. I created a click event that basically uses a webclient to download the html from the page and insert it into that div's innerHtml. This seems to work the first time. But as soon as you click a different link, the page pukes and says something about it being in an illegal state.
I'm not sure if I'm going about this wrong, and I can't find any decent examples of how to do this. Well there's one, but the english is hard to understand and it's in VB instead of C#. Any help?
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<br />
<asp:ModalPopupExtender
DropShadow="true"
OkControlID="btnOk"
CancelControlID="btnClose"
runat="server"
PopupControlID="Panel1"
id="ModalPopupExtender1"
TargetControlID="Button1" />
<asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" style="display:none;">
This is basic modal popup.
<br /><br />
<asp:Button ID="btnOk" runat="server" Text="Ok" />
<asp:Button ID="btnClose" runat="server" Text="Close Me" />
<iframe src="http://www.google.com"></iframe>
</asp:Panel>
<asp:Button ID="Button1" runat="server" Text="First Modal Popup" />
</div>
</form>
Try this it should work.
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