views:

45

answers:

3

We have an existing User Control that was built to dynamically generate a web form for an end user. This form includes required field validators, custom validators that use server side code and Regular Expression validatiors.

We now have a need to use all these validators to verify that all the needed data is entered when using a separate ordering process that cannot be validated in the same way, but has the same validation requirements before it is added to the database.

I would like to use this user control to validate the input by passing it all the values and checking the validation summary. The only way I know how to do this is to render it to a page on the client side and trigger the form submit.

Is there any way to populate and validate a web form entirely on the server side?

A: 

Not sure. It may be possible to create an instance of your user control's class, populate its fields by setting the .Text fields for each of its controls, call .Validate() on it, and check its isValid() value.

If that doesn't work, you may be better off designing separate validation for the "separate ordering process", if it is feasible. Can you give more details on how that is implemented?

ChessWhiz
I can give you some more detail, but I didn't want to distract too much from the core problem.We are implementing a "Reorder" option for items that a user has ordered in the past. That normally would be easy, but our shopping cart items are very complex. The custom form control I mentioned renders a form and stores the (dozens of) variables the user enters in. If the user comes back to reorder in 6 months that form could have totally changed since then.Now I need to verify that the user has all the current required fields. I was hoping to do that with the exact control we originally used.
JoshBaltzell
+1  A: 

It is definitely possible to create an instance of an .aspx page and invoke it on the fly, although it can be fraught with complications depending on what you are doing. Here's the basic idea:

Page instance = BuildManager.CreateInstanceFromVirtualPath(
                            "/myPath/myPage.aspx", 
                            typeof(MyPageCodeBehindType)) as Page; 

Presumably now you can call instance.ProcessRequest(HttpContext.Current) on it and have it go through the Page lifecycle. Again presumably you can check instance.IsValid and see if your Validators worked or not.

I say presumably because I have very little experience with this. Give it a shot - maybe check out the BuildManager class for some more examples.

EDIT: Looks like you can do this:

UserControl instance = (UserControl)BuildManager.CreateInstanceFromVirtualPath(
                            "~/Controls/Somefile.ascx", typeof(UserControl));
womp
Thanks a lot for the answer, sorry about the slow response. We will test this week so I can select the answer.
JoshBaltzell
+1  A: 

Yes, have you looked into LoadControl?

Dim dp As missico_UserControls_DropDownListDeviceProfile = LoadControl("~/missico/UserControls/DropDownListDeviceProfile.ascx")
'Dim dp As missico_UserControls_ComboDeviceProfile = LoadControl("~/missico/UserControls/ComboDeviceProfile.ascx")

Dim DeviceID As Integer
Dim MarketID As Integer = 0

DeviceID = dr("DeviceID").ToString

dp.SelectedValue = ProfileID
dp.DeviceID = DeviceID

dp.ID = "d" & DeviceID & "m" & MarketID

dp.OnClientChange = "pc(this);"

dp.ShowLabel(CBool(gscID = 1))

LoadControl should work for you. Yet, if the user control is not designed for true standalone operation, you could create a page that wraps the user control. Do a server transfer to the page. Have the page populate the user control, validate it, process the output, then transfer to an acknowledgement/error/original page.

I have used both these techniques, but can't find the server transfer code right now.

AMissico
Thanks a lot for the answer, sorry about the slow response. We will test this week so I can select the answer.
JoshBaltzell