Show CreateUserWizard's error message label - asp.net

I've got a CreateUserWizard control and am performing server-side validation. Is there a way to force the CUW's error message to display from the code-behind? At the moment, if the user enters a duplicate name, the controls DuplicateUserNameErrorMessage property is displayed. However, if the user has turned javascript off, or sends a custom POST header, with invalid characters, I rely on my server-side validation to catch the error. How can I then display the same error message in the control's ErrorMessage label, instead of creating a custom label and faking it?
Edit: Just to clarify, the server side validation tests various aspects. Duplicate user was just an example of when the ErrorMessage label gets called by the control.
Thanks

Update:
Here is something that will work, but requires private reflection:
void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e) {
typeof(CreateUserWizard).GetField(
"_unknownErrorMessage",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.SetValue(sender, "My error message");
e.Cancel = true;
}
Basically, you set this field to the error message you want, and CreateUserWizard picks it up. Being private reflection, it's not a 'supported' technique, but it's at least an option to consider if nothing else works.
I don't think you should have to do anything special for this to work. Generally, anything that supports client side validation has matching server side validation logic. In fact, for something like duplicate name, there is only server side validation, so I wouldn't think that turning off javascript should affect the scenario.
What exactly are you seeing when you disable javascript and you post a duplicate name? ARe you able to repro this issue with a simple page, or is there some added factors that could affect it?
I tried with a plain CUW and disabled javascript, and the duplicate user error was correctly displayed.

Related

How to validate controls inside editform template of ASPxGridView?

I have an ASPxGridView with edit form template and some bound controls inside. After update I want to validate, check the values in controls on server side. As far as I could find this is not possible. DevExpress recommends subscribing to the RowUpdating event, but this is plain wrong. Useless as is very much of their so called support.
The problem is, that if controls contains some invalid text and it raises an exception somewhere long before RowUpdating and it gets eaten by devexpress. All it comes back to client is some message like "Input string was not in a correct format".
I want to validate input controls on the server side.
And yes, I do row validating also, but this is useful only for validating business logic.
So, how to validate controls that are bound inside EditForm template on server side?
Could you please clarify? You want to validate the values after you update or actually before you write the values to the database or each control individually as it loses focus before you can initiate the update? If it is necessary to do server side validation then I would recommend doing it in the RowUpdating and RowInserting server side event handlers as was recommended by DevExpress. Why do you think this is wrong? You can validate each of the bound controls' values in the e.NewValues collection of grid's Updating and Inserting events. If any of the values do not pass validation you can cancel the update/insert action. Could you outline your desired workflow in a little more detail?
A previous poster said a hack was necessary, putting a container inside the edit form template, which is not true. You can use the edit form template itself via the .NamingContainer of any control in the edit form template. Put your validation routine in the server side _Validation event handler of the specific controls.
You can evaluate the template controls as a group:
EditFormValid = ASPxEdit
.AreEditorsValid(myGrid.FindEditFormTemplateControl("myControl")
.NamingContainer);
Or you can update a class variable during each control's validation routine
public class foo
{
bool EditFormValid = true;
.
.
.
void myControl_Validation(object sender, ValidationEventArgs e)
{
EditFormValid = EditFormValid && myControl.IsValid;
}
void myGrid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
{
If(EditFormValid)
{
.
.
.
}
else e.Cancel = true;
}
}
I have found DevExpress extremely effective and flexible. However the flexibility can be a double edge sword as there are many ways to almost do everything you need most of the time but usually one way to do everything you need all of the time. A developer can easily build on top of what works in one instance but isn't necessarily right/best practice and get to a point where they have coded into a corner as they continue to build upon a project.
If you can provide more specifics, so can I.
As far as I know this is not possible to do. Devexpress controls leave a lot to wish for. There is no way to check if validation was successful. Clearly a big issue.
What you can do is to run validation again with ASPxEdit.AreEditorsValid(). But for this you would have to do a little hack (as always with devexpress).
Put a container inside your edit form, a simple div with runat="server" and ID would do. This is your container.
Than get his div with FindEditFormTemplate() and use it in ASPxEdit.AreDitorsValid().
This workaround has drawbacks:
clutters your aspx code with unnecessary elements
page execution on server side is slower
page rendering on browser side is slower
ValidateEditorsIncontainer() runs validation again so there is a big
performance hit
All of the above are trademarks of DevExpress controls. But look at it from the bright side. Their grid sometimes takes up to five unnnecesary server and database roundtrips just to start editing.

How to register the client side added data for postback validation on server side?

If I add Items to a Select element at the client side and then submit the form, I get the following error:-
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I do not want to disable the EventValidation. How can I register the data added at the client side to a particular element for postback validation at server side ?
I know the RegisterForEventValidation method is there, but I have never used it and even I don't understand from the msdn.
If anyone has ever done this, can you please share a sample code with brief explanation ?
To be honest I never had good luck with this, I found it much easier to subclass the necessary control just in that particular case in order to avoid ASP.NET attempting to validate it. This is better than turning of validation for the page as everything but that control will still be validated as always. I'm assuming in your case you're using a server side HtmlSelect (same can be done with DropDownList). Here is one option:
public class NoEventValdationHtmlSelect : HtmlSelect
{
}
You'll have to retrieve the selected value via Request.Form[UniqueNameOfControl] since SelectedIndex/Value/Item will be null on the server side.
In my case, I know all the values in advance that can be added to the ListBox at the Client side, so fortunately, there is a solution for this:-
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(lstBox.UniqueID, "ListItemValue
that can be added at client side");
base.Render(writer);
}

Adding error to page validation on ASP.NET Web Forms

I would like add error message to page validation in order to display it on the validation summary but how?
here is my goal on the code behind file;
try {
DateTime.Parse(txtBirthDate.Text);
}
catch (Exception err) {
//Adding error message to page validation
return;
}
NOTE: Of course I used validations on my aspx page and I both validate them on either client side and server side but I waana know if this thing is possible. It can be done on the controller on ASP.NET MVC with ModelState.AddModelStateError() Method. I am exactly looking for something like that.
If you are using the ASP.NET validation controls, then they will automatically set the validation summary to the error message that you specify.
If you want to validate on the server side, then use CustomValidator. If your server-side validator indicates a problem, then the validation summary should still display the error message specified in the validation control.
Do not use a try catch block to validate your date input. If you really want to do this in code behind, use tryparse instead, in combination with a CustomValidator:
void ServerValidation (object source, ServerValidateEventArgs arguments)
{
arguments.IsValid = (DateTime.TryParse(arguments.value, out dateValue));
}
See also On postback, how can I add a error message to validation summary?
You can create a new Validator control and add it to the page to create a new validation error message, if a CustomValidator doesn't fit what you are trying to do

How do I display an error message in asp.net?

I have a bunch of text boxes and a save button to update something. When I click Save I have code that determines whether they are correctly filled in, in the code behind file.
If they are not correctly filled in, I want to display an error message in the form of an alert.
What is the best way to do this? Pressing the button obviously makes the page postback, so I thought about adding something to the URL like mypage.aspx?errormessage=blahblah but I don't know if this is the best way or even how to do it...
Any suggestions?
Modal alerts are bad, as are postbacks. Try to check as much as possible on the client-side without a round-trip to server. See jQuery Validation plugins for a less intrusive way of validation.
Could you use a CustomValidator to trigger client side script that shows a alert box?
You could use the ClientScript.RegisterStartupScript() method in the server side error handling code to write a javascript snippet which calls alert('message'), something like this
private void ShowErrorMessage(string message)
{
string script = "alert('" + message + "');";
ClientScript.RegisterStartupScript(typeof(MyPage), "errorScript", script, true);
}
But I would recommend you use a validator instead. If you implement your own custom validator, you can make it emit client-side script which can run before the submit, to avoid the postback altogether.
A good thing about validators is that their error messages can be displayed in a ValidatorSummary on the page, avoiding the ugly alert box.
First of all I won't recommend showing an modal alert box to the user.
You can call a javascript function from the server side code and in that function you can pop out the error.
Or you can issue an AJAX request and after the validation on server side you can send back a response to the client.
ASP.NET's various validation controls (with client-side validation enabled), coupled with proper error messages and/or summary message will be good enough for most scenarios.
For 'AJAX feel and behaviour', put the controls into an updatepanel will be easy to implement too.
Use ye olde Validators as much as poss, they render out some javascript to the client so yu can do alot of validation using these controls and only when they are all satisfied does it allow the page to submit.
They do fire on every submit so if you don't want every submit action to fire them you make the controls part of a validation group.
They can validate input using regular expressions, make sure a field has a value and there is a few more as well.
Then there is property to declare a 'message' and a control to show all the validator messages. All very swish and built right into the IDE.
Go check out the validator controls.
Try the following code in your button click event:
string strErr="Error!";//Put your error message here
ClientScript.RegisterStartupScript(GetType(), "scrptName", "javascript: alert('"+strErr+"'); ", true);
It will show an alert message.
Else put a label on your aspx page and set visible false in page_load event.
When error occurs in your button event set the label visibility 'true' and fill the label text with the error message.

Is there any native way in ASP.NET to do a "success message"?

Say you have something like an ASP.NET ASP:DetailsView to show and edit a single record in a database.
It's simple to record the error cases... you add validation and a validation summary. When your update form fails validation it naturally makes noise: it shows the validation message and/or the validation summary. Not a single code behind is required.
But then, you pass validation, and it makes your update completely silently. There's no sense that anything happened, and there doesn't seem to be any default settings to make a success message without code-behinds.
But, even code-behinds are confusing. What event should show the success message? onItemUpdate, right? Fine, but then let's say you make another change and get a validation error? Your success message stays. I wasn't able to find an event that would reliably turn off an existing success message if there were a validation error.
This should be web development 101! Why is it so hard?
EDIT:
Someone suggested using the ItemCommand event... I tried this and many other events, but that success message just won't disappear. Here's some code.
My message in ASP.NET
<label id="successMessage" class="successMessage" runat="server"></label>
And my DataView tag (simplified):
<asp:DetailsView
Id="EditClient"
DataKeyNames="LicenseID"
DataSourceID="MySource"
runat="server"
OnItemUpdated="SuccessfulClientUpdate"
OnItemCommand="ClearMessages">
And, my code-behind:
protected void SuccessfulClientUpdate(object sender, DetailsViewUpdatedEventArgs e)
{
successMessage.InnerText = string.Format("Your changes were saved.");
successMessage.Visible = true;
}
protected void ClearMessages(object sender, DetailsViewCommandEventArgs e)
{
successMessage.InnerText = string.Empty;
successMessage.Visible = false;
}
Once I do a successful update, however, nothing seems to make that message disappear, not even failed validation.
2nd EDIT:
Just want to be clear that I did try putting the ClearMessages code in Page_Load. However, nothing seems to make that successMessage label disappear when I hit update a 2nd time WITH a validation error. Can anyone suggest any other troubleshooting tips?
As far as I know, there is no native way of doing this. You may rant about it, maybe Microsoft will hear it :).
Resetting the "success message" on Page_Load, or wherever in your code-behind, won't work. This is because ASP.NET validation is usually done both client and server-side. This means for every validation control you put on the page, ASP.NET generates some client-side Javascript that does the validation and renders the error on the client, without going back to the server. So you're stuck with both the success message and the error message at the same time.
What you can do about it:
place a <div> control on your page, that would show up the success message (as already suggested by others above). Whenever you update something (in server-side code), show up the control and set a meaningful "Successful!" message text.
register a custom Javascript function that would lookup the <div> and hide it on every page submit. Be aware that the function needs to be called before the autogenerated client script that does the validation.
If you look at the client source of an ASP.NET page (with validators on it), here's what you can find:
<form name="aspnetForm" method="post" action="MyPage.aspx" onsubmit="javascript:return WebForm_OnSubmit();id="aspnetForm">
The WebForm_OnSubmit is generated by ASP.NET and calls the javascript that does the validation. Sample:
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
return false;
return true;
}
To register your custom code that hides the success message, you should place (in your code-behind) something along these lines:
if (!Page.ClientScript.IsOnSubmitStatementRegistered("ClearMessage"))
{
string script = #"document.getElementById('" +
yourDivControl.ClientID + "').style.display='none'";
Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "ClearMessage", script);
}
This will turn your page's autogenerated WebForm_OnSubmit into the following:
function WebForm_OnSubmit() {
document.getElementById('ctl00_yourDivControl').style.display='none';
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
return false;
return true;
}
The effect:
On every postback (e.g. when ItemCommand is triggered, or when some Save button is clicked, or anything else), you will show up the div control with the "success" message. On the next postback, just before submitting the page to the server, this message is cleared. Of course, if this postback also triggers a "success", the message is shown again by the code-behind on the server. And so on and so forth.
I hope the above is helpful. It's not the full-blown solution, but it gives sufficient hints to point you in the right direction.
Use the ItemCommand event and perform reset to clean state for every command that is given. Then if validation fails on update, the validation error will be displayed. If it succeeds, it will show the success message. Subsequent, button clicks will invoke the ItemCommand event and you can reset again to start fresh.
Daniel, I needed something similar so I derived from a Label and bound it directly to the events raised by the various DataSourceControl derivatives (LinqDataSource, ObjectDataSource, SqlDataSource). I also used client-side JavaScript to hide the label during validation and/or form submission (like Dan.C's answer).
The control exposes separate message text properties for successful Deleted, Inserted and Updated events.
I've published the commented source for the solution at http://codehq.net/files/UpdateSuccessMessage.zip
Hope this helps.
Why not turn the validation message off in the Page_Load. I assume that if a post back occurs you'd want to get rid of the message so make sure it's always off. Or disable view state for the control by setting EnableViewState="False" in the markup (aspx page).
So:
protected void ClearMessages(object sender, DetailsViewCommandEventArgs e)
{
successMessage.InnerText = string.Empty;
successMessage.Visible = false;
}
becomes:
protected void Page_Load(object sender, EventArgs e)
{
successMessage.InnerText = string.Empty;
successMessage.Visible = false;
}
Also as a side note if you buiding .net sites you need to get comftorble with code behinds IMHO:-)

Resources