Using Validation Tag Helper in ASP.NET 5 - asp.net

I have an ASP.NET 5 app. I'm creating this app as a learning exercise on a Mac. Currently, I'm trying to use the validation tag helper. In my view, I have the following code:
<form method="POST" action="~/account/login">
<h1>Login</h1>
<div class="validation" asp-validation-summary="All" />
#Html.ValidationSummary(false, "", new { })
<input id="Username" name="Username" />
<br />
<input id="Password" name="Password" />
<br />
<button type="submit">Login</button>
#Html.AntiForgeryToken()
</form>
The traditional #Html.ValidationSummary(...) approach works. However, the <div class="validation" asp-validation-summary="All" /> approach does not. I want to break away from using the tradition #Html. approach and use tag helpers as much as possible.
Why won't the validation results appear in the asp-validation-summary?
Thank you!

Since this commit
the ValidationSummaryTagHelper takes a value of the ValidationSummary enum instead of a string
So you need to write:
<div class="validation" asp-validation-summary="ValidationSummary.All" />
See also in the sample on Github.

Related

How can i set Bootstrap language manually?

last week i began developing an ASP.NET Page with Bootstrap (with LESS).
My website has an language switcher between German and English. This works fine for my own components.
But the Bootstrap sign-in form automatically uses the browser language. If an field is empty, i get a message to fill out all fields. I want to set the used language manually in all forms in Bootstrap.
Is this possible, or are there other options?
Thanks.
Edit:
here is my Code
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div class="container">
<div class="form-signin" role="form">
<h2 class="form-signin-heading">
<asp:Localize ID="LocalizeHeading" runat="server"
meta:resourcekey="LocalizeHeadingResource1" Text="Anmeldung"></asp:Localize>
</h2>
<input ID="InputUsername" runat="server" type="text" class="form-control" placeholder="<%$ Resources: Authentification, PlaceHolderUsername %>" required autofocus>
<input ID="InputPassword" runat="server" type="password" class="form-control" placeholder="<%$ Resources: Authentification, PlaceHolderPassword %>" required>
<label class="checkbox">
<input type="checkbox" value="remember-me">
<asp:Localize ID="LocalizeRememberMe" runat="server"
meta:resourcekey="LocalizeRememberMeResource1" Text="Anmeldung speichern"></asp:Localize>
</label>
<asp:Button ID="ButtonSignIn" runat="server" Text="Einloggen"
class="btn btn-lg btn-primary btn-block" type="submit"
meta:resourcekey="ButtonSignInResource1" onclick="ButtonSignIn_Click" />
</div>
</div> <!-- /container -->
</asp:Content>
This is not a Bootstrap issue. The tooltip error is a default behaviour of HTML5 so, at the very end, it generates from the browser. The error generates since the input tag has the attribute required, then the browser will try to ensure that you place any text on it.
Please check changing the language of error message in required field in html5 contact form - in particular Rikin's answer in order to set
setCustomValidity message using javascript.

Form tag in html not working

I am not a skilled programmer and I am building a site using a third party's CMS. I have created a <form> in order to gather information and send it to an external site. My code doesn't work. The reason I am told is that the site is built on ASP.NET and as the 'master'page already has a <form> element in it and <form> elements cannot be embedded inside one another; so what I've written will never work. I have been advised to look for a java based solution, but I'm at a loss.
My present code is pretty basic...
<form action="http://ExternalWebSite.com" method="post" id="subForm">
<label for="name">Name:</label><br /><input type="text" name="name" id="name" /><br />
<label for="Email">Email Address:</label><br /><input type="text" name="email" id="email" /><br />
<label for="Dog name">Your dog's name:</label><br /><input type="text" name="dogname" id="dogname" /><br />
<label for="Town where you live">Town where you live:</label><br /><input type="text" name="town" id="town" /><br />
<input type="submit" value="Subscribe" />
</form>
How can I make this execute without using the tag inside my html code?
ASP.NET web forms adds one <form runat="server"> in your application. Therefore, you can't nest forms, but you can get multiple forms through a hack, mentioned here.
You can make an ajax call (post in this case) too to that url
$.ajax({
type: "POST",
url: "http://ExternalWebSite.com",
data: { name: $("#name").val(), $("#email").val(), $("#dogname").val(), $("#town").val() },
success: function(result){
//here you handle what to do after the post
}
});
For further reference on how to use jquery go to: This link The most imortant here is the ajax called which is foinf to make a post the specified utl sending the named parameters,the success function which will receive anything you write on the response on the url you are calling, and the selectors $("#name").val() which means "give me the value of the element with the id name"

Working with WebDriver (findElement function)

I'm writing a program in Java that submits a form using WebDriver. There is a method findElement that allows you to select elements using class name, css selector, id, link text, name, partial link text, tag name, or xpath.
Here's the button that I want to select:
<div class="editable" style = "width:82px;float:right;margin-right:10px;">
<a href="#" onclick="$('order_form').submit(); return false;" class="btn">
<img class="btn" src="/myhuds/images/rd_images/btn_place_order.gif" alt="Place Order" width="82" height="17" border="0">
</a>
</div>
I can't use class name because there are multiple buttons on the page. Any ideas on how I might go about using the findElement method to select this button?
Thanks!
Try using this. You can add some browser plugin to get XPath.
driver.findElement(By.xpath("Your XPath"));
You can add an id to the button (may be to all buttons in your page) and find as -
driver.findElement(By.id("Your ID"));
Use CSS selectors. You can find a short explanation here W3, and couple examples here :Examples.
Therefore, try to use driver.findElement(By.cssSelector(...));
For your specific case you can search based on img #alt attribute using XPath
driver.findElement(By.xpath("//img[#alt='Place Order']")).click();
Step 1:
With the available DOM Structure Generate CSS Selector for the "Place Order"
Image
css=a[onclick*='order_form'] > img[src*='btn_place_order.gif']
Then Perform Click on "Place Order" Image.
driver.findElement(By.cssSelector("a[onclick*='order_form'] > img[src*='btn_place_order.gif']")).click();
Following example should help:
Html File:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1 class="page-header">Login</h1>
<ul class="errors"></ul>
<div>
<form class="login-form" action="checkLogin.html" method="post">
<label for="username-field">Username1</label>
<input type="text" name="username" class="username-field" />
<br />
<label for="password-field">Password1</label>
<input type="password" name="password" class="password-field" />
<br />
<input type="submit" value="Submit" />
</form>
</div>
<div>
<form class="login-form" action="checkLogin.html" method="post">
<label for="username-field">Username2</label>
<input type="text" name="username" class="username-field" />
<br />
<label for="password-field">Password2</label>
<input type="password" name="password" class="password-field" />
<br />
<input type="submit" value="Submit" />
</form>
</div>
</body>
</html>
To locate the input field for Password1 :
WebElement passwordField1: driver.findElement(By.cssSelector("div:nth-child(3) .password-field"));
To locate the input field for Password2 :
WebElement passwordField2: driver.findElement(By.cssSelector("div:nth-child(4) .password-field"));
Learn more about nth-child() selectors here.

How do I retrieve HTML POST data for manipulation in ASP.NET WebForms?

I have the following html:
<html>
<body>
<form runat="server">
Name: <input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?
(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)
If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:
string nameTextPosted = Request.Form["name"];
Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.
Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.
<asp:TextBox Id="Name" runat="server"></asp:TextBox>
...
string name = Name.Text;
Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.
ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.
<form runat="server">
Name: <input type="text" name="name" id="name" runat="server" />
<br />
<input type="submit" name="submit" id="name1" runat="server" />
</form>
Now after this you can control their behavior:
name.Value="Hellow World !"
You have to add id and runat="server" in each control. like this :
<input type="text" name="name" id="name" runat="server" />
Its better to use asp:TextBox like this :
<asp:TextBox ID="name" runat="server"></asp:TextBox>

Access a form in asp.NET C#

All the examples I can find are in Visual Basic, but I am using C#.
I want to get the data that is in a textbox in a form.
My code so far:
<form action="login.aspx" method="get">
<p>Username: <input type="text" name="username" /></p>
<p>Password: <input type="text" name="password" /></p>
<input type="submit" value="Submit" />
</form>
So what could I do? Because I keep getting told to do this:
Dim UserName
UserName = Request.Form("UserName")
But it doesn't work in C#.
Don't need to do that on asp.net; simply change your markup like so:
<form action="login.aspx" method="post" runat="server">
<p>Username: <input type="text" name="username" runat="server" id="txtUsername" /></p>
<p>Password: <input type="text" name="password" runat="server" id="txtPassword"/></p>
<input type="submit" value="Submit" />
</form>
And on code behind:
string UserName= txtUsername.Value;
And yes, Shawn also caught a good one, you should use POST.
Your method should be POST.
<form action="login.aspx" method="post">
The sample code you posted is vb.net. C# has it's own syntax and keywords.
to get the value you can use the following (not an optimal solution for webforms)
string userName = Request.Form["UserName"];
I would suggest going through some c# tutorials to get a handle on the language. Here's the first one i found http://www.csharp-station.com/Tutorial.aspx

Resources