How to change Label value in MVC2 - c#

In my application (written in c#) I have a textbox and a submit button.
If the user clicks "submit" without entering any value, I want him to stay at the same page and display a message saying "Please insert value"
I thought to put a label, and then just edit its value and/or visibility.
The structure of my application is as following:
Controller A
inside View, under A, I have B.aspx
and inside B.aspx I call a partial C.ascx
The submit form with my label are inside C.ascx
How can I change the lable value and/or visibility?

You should use the Data Validation Attributes for this.
Something like this:
[Required]
String textBoxValue{get;set;}
This will create a custom javascript/jquery file that will perform the appropriate validation for you.
You can even change the default error message for Required.
[Required(ErrorMessage = "The textbox value is required because I said so :)")]

ASP.NET MVC 2 support both server-side and client side validation. I believe this version takes a dependency on the jQuery Validation plugin. In your view, you can use the Html.ValidationMessage or Html.ValidationMessageFor helper methods that will allow your application show error messages whenever validation fails
Here's a link to a post that may be of further assistance.
http://goo.gl/Jxozv

Related

how do I get the validationsummary text to put in a custom alert ASP.NET

for a recent schoolproject I need to make an ASP-website.
On the loginform I have a few validators and a validationsummary.
The summary itself works, but I would like to get the text from it,
so I could put it into my own custom "popup". The popup is just a twitter-bootstrap model wich I tweaked a bit so I can put in a title and some text easily from any code behind file.
Can anybody help me? Any c# code behind, or jquery solution is fine to me.
Try the solution in this answer:
Can I run some custom script after ASP.NET client side page validation fails?
Basically you call the Page_ClientValidate("") function and write custom JavaScript to display the alert

AJAX / ASP.Net / C# : Show next content only after validation?

I currently have an ASP.Net Web Form that looks like this:
Each section of this web form is wrapped in its own div so that they could be shown or hidden by changing the CSS display: property to none. My intention is to show each section one at a time, starting with the first one. When the user clicks "Next" the currently visible section will be hidden and the next section will be displayed in its place.
Many of the fields have ASP.Net validation controls attached to them (the "Zip Code" validation error message is displayed in this image as an example).
These validation controls have the runat=server
The whole Web Form is being loaded into a parent div dynamically through AJAX. Each Next button is not of type submit (they are of <input type= "button">).
How can I ensure that all fields in a given section are validated before allowing the user to hit "Next" ?
I am pretty stuck on this so some assistance would be really appreciated!
One option is each DIV you can have Next button and also you can use one ValidationGroup for button and the controls need to be validated inside div. if you set validation group it will validated only same group controls before post back.
Or you can have one Next button and change validation group of that button by using client side script or code behind when click on next button.
changing validation group of button with javascript on client side

Calling Asp Validator from Javascript

I need to call a validator to show an error message from JavaScript
For Example:
I have a textbox in which i take time as input and a Button to submit the value.
I have a minimum time and maximum time.
I'm validating the time textbox using JavaScript and using an alert message to show if there is an error in the time entered
Now instead of showing the alert box I want to call the validation callout extender
to show the error message
Can you help me out?
Have you tried using ASPX validators, these solve javascript and also server side parts.
You can then just ask page.IsValid.
here is some more information about ASPX validator controls
Validators are pretty easy to use, as they support also regular expressions and own functions. But sometimes you've to use two validators on one field. e.g. requiredFieldValidator and Compare validator
http://asp.net-tutorials.com/validation/required-field-validator/
http://msdn.microsoft.com/en-us/library/bwd43d0x.aspx
http://msdn.microsoft.com/en-us/library/debza5t0.aspx

How can I pass textbox value from usercontrol(ascx) to another page (aspx) using Server.Transfer()

I am writing one user control (webpart) in kentico. I want to pass textboxes' value from usercontrol to aspx page using Server.Transfer().
Can it be? If so, how can I do like that?
Best Regards,
Reds
I don't particularly like this method, I prefer to use Sessions to pass data between pages, but if you need to do this here how it's done according to the this page.
Here's TL;DR summary. It requires three scripts/pages:
Form.ascx - this will be the control that contains the text box value.
FormParsingScript.aspx (referenced in the Form.ascx in the Action attribute) - this will perform the actual Server.Transfer "FinalScript.aspx" call
FinalScript.aspx which will display the contents of Response.Form["TextBoxName"] (HTTP POST) or Response.QueryString["TextBoxName"] (HTTP GET)

Refreshing the asp.net web page after validation

I have an asp.net web page (C# 2008) where the user would enter an EmployeeID, and when they tab out of the textbox (the page executes a validation code block in the codebehind), they get a messagebox prompting them to select one of two values from a dropdown listbox.
The code for the message prompt in the codebehind is :
Response.Write("<script>window.alert('Please select Alpha or Beta')</script>");
After the prompt is displayed, and the user clicks "ok" and returns to the page, the text on the page appears distorted (the text in labels are a size larger, the labels get wrapped to another line etc)
I tried putting a Response.Redirect("UserProfileMaint.aspx"); after the messagebox in the codebehind, but now, the messagebox does not appear;
So this is my squence:
User enters EmployeeID
If user has NOT selected Alpha or
Beta, then show messagebox
If user HAS selected Alpha or Beta,
then don't show messagebox
I want to display the messagebox validation, and ensure the appearance of the text on the page is not distorted. How can I do this?
Response.Write writes directly to the output stream, placing it before <html> which the browser gets very confused by (causing your text issues). What you want to do instead is this:
ClientScript.RegisterStartupScript(GetType(), "alert",
"alert('Please select Alpha or Beta');", true);
ClientScript.RegisterStartupScript includes the script in the page to be run on load rather than put in the response too early. The last argument: true is telling it to wrap that alert in <script> tags, keeping your code-behind cleaner.
The best way to handle this would be to use Javascript and do Client side validation. If you really want to do server side validation then instead of showing a alert by using Response.Write you should use RegisterStartupScript or better show the message using a Label at the top.
HTH
When you call Response.Redirect, that occurs on the server side, whereas you want the redirect to occur on the client side after a choice is made.
To do that, when you write your script with Response.Write (btw, there are much better ways to do this), you would have logic that determined what the user selected, and then based on the selection, either requery them for data, or set the location property of the inherent document object to the url you want to redirect to.

Categories

Resources