开发者

Given a user control with a form containing validation can I validate entirely server side?

开发者 https://www.devze.com 2022-12-25 04:03 出处:网络
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 Regula

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开发者_JAVA百科 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?


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));


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消