Validation in ASP .Net 3.5

Topics: General discussion, Validation Application Block
Sep 10, 2009 at 2:26 PM
Edited Sep 10, 2009 at 2:55 PM

Hi everyone. I am currently building ASP.Net 3.5 site and I am having problems with Page life cycle and validation. I can't get validation to work and i think that i have general life cycle problems. Here is what i want:

1. When page is first viewed data is displayed in ListView control with "edit" button.
2. When "edit" button is pressed that entity/row should be displayed in edit mode. EDIT( But there shouldn't be the list of other rows )
3. When finished editing, pressing "update" will display validation errors if any.

What happens is that page is ALWAYS valid, like the validators are validating old values, while in lvEdit_Updating when i get controls with FindControl("cName") they have new values.

My aspx page contains two ListView controls ( 1st for selecting 2nd for editing of entity. 1st has LayoutTemplate and ItemTemplate while 2nd has LayoutTemplate, empty ItemTemplate and EditTemplate)
In 2nd ListView ( the one for editing ) i have empty ItemTemplate so only rows in edit mode whill be shown. In EditItem template i have various input controls and PropertyProxyValidator set accordingly in example:


PropertyName="Position" EDIT(Misspeled translation)


Here is class attribute:


[NotNullValidator(MessageTemplate="{1} se mora uneti.",Ruleset = "Standard")]
public int Position


In code behind i have this logic:

PageLoad - Load data in private variable and set lvEdit.Visible = false
Page_PreRender - Set DataSource for both ListView-s ( from private variable ) and call their DataBind method. If lvEdit.EditIndex != -1 populate ComboBox inside lvEdit with data.
lvDisplay_SelectedIndexChanged - (edit button has "Select" CommandName) Hide lvDisplay , Show lvEdit and set lvEdit.EditIndex = e.NewSelectedIndex;
lvEdit_ItemUpdating - i have test for validation using Page.Validate() and getting one validator with FindControl and using his Validate method. Both show success while in same event handler i get txtEditPosition.Text which has error and looks like "123asd".


I think the problem lies in Page life cycle but i can't find the other way than databinding in PreRender because if i am binding in PageLoad i get one postback delay ( example: when i have only ListView which displays selected items. Selecting 2nd row makes no change. Selecting 5th row display 2nd row as selected. Selecting X row will display 5th row...)

Any suggestions are welcome, and thanks for help.

Best regards,
Zlatibor Veljkovic

Sep 11, 2009 at 12:00 AM

I finnaly got it after 5 days :D So for reference to other having same problems:

In my example:

1. In Page_Load  if !Page.IsPostBack assign data source to lvDisplay ( visible, displays tabular data ) and call its DataBind method. Hide all others ( visible = false ).

2. In event handlers generated by lvDisplay ( ItemEditing, ItemDeleting, InsertItem_Click ). Set controls visibility ( ie: On edit hide lvDisplay show lvEdit ). Set DataSource and call DataBind. After that data bind child controls.

3. In event handlers generated by now visible control ie: lvEdit_ItemCanceling just switch visibility without data binding. That's important because lvDisplay will have same items ( from ViewState )  ie 2: lvEdit_ItemEditing - do your edit. Switch visibility ( to show lvDisplay with new values ) and set lvDisplay DataSource to new data and call DataBind.


This is how i got it working... I have used programmatic data binding so i dont know if this will work with declarative data binding. And few tips... remember that controls will hold their values over postbacks because of ViewState. This infromation is applied before Page_Load so there is no need to DataBind every time, just do it when you change data. ListView displays all items it has data bound with respective templates, so you cant switch to InsertItem template like in DetailsView. Use caching. Make all call's for data to go through caching layer and invalidate cache when doing database update. Security application block is awsome :D So is Validation :D Use PropertyProxyValidator for doing validation with VAB. You can find it in ValidationApplicationBlock.AspNetIntegration.dll (something like that)....

Happy coding :D