submit a form with AjaxToolkit? - c#

Can we submit a form in ASP.NET with AjaxToolkit ?
e.g:
below in our form:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div id="mainDiv">
<table style="width: 40%;" cellspacing="3" cellpadding="3">
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorUsername" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxUsername"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxUsername" runat="server"></asp:TextBox>
</td>
<td align="left">
: UserName
</td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
SetFocusOnError="True" Text="*" ControlToValidate="TextBoxPass"
ValidationGroup="login"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBoxPass" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td align="left">
: Pass
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="ButtonSubmit" runat="server" Text="Login"
ValidationGroup="login"/>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="LabelResult" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
I wanna log-in a user with Ajax so I have to send username and pass to a Webservice and get the result and show the result in LabelResult.
Can we do it with AjaxToolkit in ASP.Net?

I'm using ajax for a cascading dropdown menu set and i don't know how to submit the contents of the form to a controller. I think this is a related question becausewht I did was generate a hidden ajax control that triggered upon the change in value of the final ajax control in the form. The webservice that the control triggered then saved al of the form information to a database at which point the submitbuton was more of a comforting decoration for the user. Once the user submitted the form the values were available in a meta data field in the database and the submit handler accessed those values rather than reading them from the postback.
I didthis because it ended up being faster than learning what I needed to in order to read them form the postback using java script or some other method of retrieval form the postback so I wouldn't claim thatthisis the best approach but it worked for me. Hope this helpsyou somehow.
D.A.P.

for working of AJAX script manager must appear before the form which i think not possible and also
Cos Callis says very well
"If you want to submit the form, that is what a post back is for"

So, If we wanna send a form to server with Ajax, we have to use something like JQuery, and we can't do it with AjaxControlToolkit.

Related

Display message in c#

I am a beginner in C#, ASP.NET.
What i want is when clicks 'submit' button, need to display a message 'Your registration is succesful'.
But When i clicks here, nothing showing, but it showing message in other pc with same code.
Somebody can help me ? is there any configuration errors in my project ?
code here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
public partial class Registration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
Response.Write("Your registration is succesful"); // Not displaying
//MessageBox.Show("Test Display");
}
}
ASP.NET
<form id="form1" runat="server">
<div>
</div>
<table class="style1">
<tr>
<td class="style3">
Username</td>
<td class="style6">
<asp:TextBox ID="uname" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="uname" ErrorMessage="Username is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Email</td>
<td class="style6">
<asp:TextBox ID="email" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="email" ErrorMessage="Email is required" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="email" ErrorMessage="You must enter the valid email id"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3">
Password</td>
<td class="style6">
<asp:TextBox ID="pass" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="pass" ErrorMessage="Password is required" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Confirm Password</td>
<td class="style6">
<asp:TextBox ID="passr" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="passr" ErrorMessage="Confirm password is required"
ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="pass" ControlToValidate="passr"
ErrorMessage="Both passwords must be same" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style3">
Country</td>
<td class="style6">
<asp:DropDownList ID="country" runat="server" Width="180px">
<asp:ListItem>Select Country</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>GERMANY</asp:ListItem>
<asp:ListItem>FRANCE</asp:ListItem>
<asp:ListItem>INDIA</asp:ListItem>
</asp:DropDownList>
</td>
<td class="style8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="country" ErrorMessage="Select a country name"
ForeColor="Red" InitialValue="Select Country"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style7">
<asp:Button ID="submit" runat="server" Text="Submit" />
</td>
<td class="style5">
<input id="Reset1" type="reset" value="reset" /></td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style6">
</td>
<td>
</td>
</tr>
</table>
</form>
You need to understand the difference between server-side and client-side.
Server-side is everything that happens on the server (e.g. ASP, ASP.NET, PHP) that is used to create the HTML that is sent to the browser. It also deals with the information that is returned by the browser when the user submits stuff back (the post-back).
Client-side is everything that happens on the browser once the HTML has been received, or when user does something on the page like clicking an element.
What you are doing is trying to run a Windows Application style MessageBox.Show on the server... that's not going to work.
If you want the browser to display an "alert" window (a bit like a MessageBox) then you need to send client-side script to the browser. Try this...
protected void submit_Click(object sender, EventArgs e)
{
Response.Write("Your registration is succesful");
var script = "window.alert('Test Display');";
ClientScript.RegisterStartupScript(this.GetType(), "message", script, true);
}
As per the comment by the OP...
My problem is Response.Write("Your registration is succesful"); is not working
Instead of using Response.Write use a <asp:Literal> control (which gives you the advantage of positioning the control exactly where you need it) and set the .Text property of it (remember that this will remain on post-back, so you might need to clear it).
You could also use a <asp:Label> which would not only allow you to position it, but you can also including .CssClass or .Style attributes for better formatting.
If you want to show confirmation message box try to use jquery on your .aspx page. You can add function like this
function confirmationAccept() {
return confirm("Accept?");
}
And than add properties onclientclick like this:
<asp:Button ID="addButton" runat="server" Text="Add" OnClientClick="return confirmationAccept()">
You can show javascript alert message by using ClientScript.RegisterStartupScript.

Control 'txtUserName' of type 'TextBox' must be placed inside a form tag with runat=server

I have this error - Control 'txtUserName' of type 'TextBox' must be placed inside a form tag with runat=server. I am unable to get rid of it, my text box is placed inside a form tag with runat=server.
The page which I am getting this on is my 'reset password' page I want the users to be able to enter their username and once they press the reset password button it takes them to a page which allows them to change their password.
Here is the code that I am currently working with.
<head runat="server">
<title></title>
</head>
<body>
<div style="font-family:Arial">
<table style="border: 1px solid black; width:300px">
<tr>
<td colspan="2">
<b>Reset my password</b>
</td>
</tr>
<tr>
<td>
User Name
</td>
<td>
<form action="ChangePassword.aspx" method="get">
<asp:TextBox ID="txtUserName" Width="150px" runat="server"
ontextchanged="txtUserName_TextChanged">
</asp:TextBox>
</form>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnResetPassword" runat="server"
Width="150px" Text="Reset Password" onclick="btnResetPassword_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</body>
</html>
As the error message says, you need to add a runat="server" in the form tag. This is just something ASP.NET demands to work.
The way ASP.NET handles events and postbacks forces this requirement. It tries to keep the page's state inside the actual page (called the 'view state'), and on form submission it sends it back. This was invented to make the stateless web stateful.
Just put in the runat="server" inside the form tag and you will be set.
<form runat="server" action="ChangePassword.aspx" method="get">
Add < form> tag like this...,
it will work..

Default Button not working with multiple validation groups

Thank you very much for taking the time to read this. I have an issue in which I was setting a default button for an ASP.NET page on page load from code behind, but now that I have multiple validation groups targeting one control, that is no longer working. Now, when I hit enter while in that control (textbox), validation for both groups are triggered minus the validation summary text.
Here is my examplefied code:
ASPX
<table>
<tr>
<td><asp:Textbox runat="server" ID="validateMe"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
<td><asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</tr>
<tr>
<td><asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup"/></td>
<td><asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup"/></td>
</tr>
<table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup"/>
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
this.Form.DefaultButton = firstButton.UniqueID;
}
If I use this and hit 'Enter' while inside of the textbox without typing anything into it, then neither of the validation summaries will appear but I will have two asterisks next to the textbox (one for each group). If the user presses 'Enter' I would expect a full validation using only the first group which is supposed to be assigned to the DefaultButton (here, 'firstButton'). Is there any way to achieve this functionality and initiate the client-side validation that would have happened had the user clicked 'firstButton' instead?
I have also tried wrapping the whole table plus the validation summaries inside of an asp:Panel and setting the DefaultButton there, but I received the same results. Any help or pointers in the right direction would be greatly appreciated!
Set
EnableClientScript="false"
in RequiredFieldValidator control. It will help.
<asp:Panel runat="server" DefaultButton="secondButton">
<table>
<tr>
<td>
<asp:TextBox runat="server" ID="validateMe"></asp:TextBox></td>
<td>
<asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup" /></td>
<td>
<asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup" /></td>
</tr>
</table>
<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup" />
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup" />
</asp:Panel>

Using AJAX to send create new user details via asp.net

What's the best way to send a username and password via ajax to the code behind page in .net.
I have an index.aspx page like so:
<asp:TemplatedWizardStep>
<CustomNavigationTemplate>
<div style="margin-bottom:7px;text-align:left;">To create an account, simply send us your e-mail address. A password will be sent to this address upon submission.</div>
<table border="0" cellpadding="3" cellspacing="0" style="width:99%;">
<tr>
<td style="text-align:left; vertical-align:middle;width:90px;">E-Mail:</td>
<td style="text-align:left; vertical-align:middle;">
<asp:TextBox ID="UserName" runat="server" CssClass="newUserTextbox"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align:left; vertical-align:middle;width:90px;padding-top:5px;">Screen name:</td>
<td style="text-align:left; vertical-align:middle;padding-top:5px;">
<asp:TextBox ID="ScreenName" runat="server" CssClass="newUserTextbox"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align:left; vertical-align:middle;"> </td>
<td style="text-align:right; vertical-align:middle;padding-top:5px;">
<asp:imagebutton ID="CreateUser" CommandName="CreateUser" Enabled="true" runat="server" AlternateText="OK" ImageUrl="powerstats/inc/img/ok_up.png"></asp:imagebutton>
<img src="powerstats/inc/img/cancel_up.png" style="cursor:pointer;" onmouseover="SwapImageIndex(this,'cancel_ov.png')" onmouseout="SwapImageIndex(this,'cancel_up.png')" />
</td>
</tr>
</table>
</CustomNavigationTemplate>
</asp:TemplatedWizardStep>
I want the imagebutton to be clicked and send an ajax call with the username and screenname (and eventually password), to the back end page. If I do onClick=X the page refreshes. I want the page NOT to refresh and send an alert back if something is wrong.
You would need to use an UpdatePanel with a ScriptManager. If a submit happen inside the UpdatePanel, it's gonna return only what's inside the UpdatePanel itself through AJAX.
If you want to handle possible Error, you can use the AsyncPostBackError Event of the ScriptManager. See MSDN page for a complete exemple of error handling.
Code would look like (+ your error handling logic):
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TemplatedWizardStep>
<CustomNavigationTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
/* Your login controls here ...
Make sure to include all the fields
to be refreshed + the submit button */
</ContentTemplate>
</asp:UpdatePanel>
</CustomNavigationTemplate>
</asp:TemplatedWizardStep>

asp.net custom control with form field validation issue

I have created a custom asp .net control some fields have validation controal associated with them.
The problem arised when I declare more than one initialization on a page. When I hit submit on one of the intialized control, form validation occur on all other control that were declared. Due to this issue I cannot submit a form. Here is the code
<%# Control Language="C#" AutoEventWireup="true" CodeFile="FinancialAdvisorHelp.ascx.cs" Inherits="FinancialAdvisorHelp" %>
<table width="316" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2" >
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorMember" runat="server"
ErrorMessage="Pleae enter member name. " ControlToValidate="TextBox_Name"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="Please enter email. " ControlToValidate="TextBox_email"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorDarryEmail" runat="server"
ControlToValidate="TextBox_email" ValidationExpression=".*#.*\..*"
ErrorMessage="<br>
Invalid Email."> </asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td width="165"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Member Name*</p>
<asp:TextBox ID="TextBox_Name" runat="server" CausesValidation="True"></asp:TextBox>
</td>
<td width="151" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0 0 0 4px; padding:0; text-align:left;">E-Mail Address*</p>
<asp:TextBox id="TextBox_email" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding-right:50px;"><br />
Telephone Number</p>
<asp:TextBox id="TextBox_phone" runat="server" CausesValidation="True"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Ask Darryl your question</p>
<asp:TextBox ID="TextBox_question" runat="server" Rows="7" Columns="48"
style="width:310px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2"><p style="font-size:10px; margin:0; padding:0; float:left; width:240px; line-height:12px;">Note: you should receive a response within two (2) business days.<br />
*Required information.</p>
<asp:ImageButton ID="ImageButton_Advisor" runat="server" alt="submit"
width="63" height="18"
style="border:0; padding:0; margin:10px 0 0 0; float:right;" ImageUrl="./images/investments/submit.gif"
/>
<asp:HiddenField ID="HiddenFieldAdvisorEmail" Value="" runat="server" />
</td>
<tr><td colspan="2">
</td></tr>
</tr>
</table>
Use the ValidationGroup attribute on all form elements that cause validation including the submit button. When validation is triggered, it will only check other elements with the same ValidationGroup specified.
To make sure it is unique across each instance of your control on the page use your UserControl's ID as the validation group in the code behind.
RequiredFieldValidatorEmail.ValidationGroup = this.ClientID;
More information can be found here: http://www.dotnet-guide.com/validationgroups.html
I'm having a hard time deciphering your question.
Validation Groups
It sounds like you might want to take advantage of validation groups . When you have multiple triggers that should validate different controls, this is what to use.
Check your Validators and Custom Controls
If you are still having issues, when validators have errors and you trigger validation, it appears that "nothing happens" (what really is happening is that the validators error out and the page kind of just stops). It can also be possible that your custom controls have issues.
Check your Validators and ID's
The problem arised when I declare more
than one initialization on a page
What do you mean by this? I wonder if you are declaring controls dynamically in your code? This could also be your problem. Each ASP.NET control (including validators, custom controls, ... ) needs it's own unique ID. You can also have more than one validator checking a single control, but a single validator can't validate multiple controls.

Categories