3

i've been working on this for some time now but I can't seem to be able to find the answer... Google doesn't really help me today!

I'm working on an C# website that uses a lot of forms with a lot of validation on it. Big forms which have to be typed by hand, (no problems there though).

The problem that I have is that I always want my ValidationControls to have the Display Property set to dynamic.

I have found a work around but I'm not convinced that this is the best solution.

Currently I have a BaseValidatorControlAdapter that sets the validators display type to "Dynamic" like this:

public class BaseValidatorControlAdapter : ControlAdapter
{
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        System.Web.UI.WebControls.BaseValidator _control = (System.Web.UI.WebControls.BaseValidator)this.Control;
        _control.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
        base.Render(writer);
    }
}

This is then triggerd by adding an App_Browser which looks like this

<browsers>
    <browser refID="Default">
        <controlAdapters>
            <adapter controlType="System.Web.UI.WebControls.BaseValidator"
                     adapterType="BaseValidatorControlAdapter" />
        </controlAdapters>
    </browser>
</browsers>

My question for you is. Is it possible for me to set the default value of Display for any validator control. Because my current solution just overrides what there is right now and there is no way of changing that. Therefor when the situation arrises that I do need something else then dynamic in the display option there is no solution for it.

I'm almost convinced that there is a better solution to this.

Any advice is appreciated!

Edit august 2nd 2012:

I finally settled with the solution to override the default asp.net controls and add the following constructor to them

public controlname ()
  : base ()
{
    this.Display = "Dynamic";
}

I added all these controls to a namespace and now I can do

<validator:RequiredField ID="RqrdFld_x" runat="server" Display="Static" />

Essentially overriding the default value in my control definition and overriding the default control Display value of Dynamic in the constructor.

4

2 回答 2

2

What you can do then use such code in Page_Init event:

foreach(Control control in Page.Controls){
  if(control is System.Web.UI.WebControls.BaseValidator)
  {
     control.Display = "Static";
  }
}

It will find all the controls and set the Display property accordingly.

于 2012-07-28T21:32:27.497 回答
2

This link should be helpful in solving your problem.

You can use a skin file to set defaults for web controls accross a .NET site.

I have added a folder called DefaultTheme to my App_Theme folder and added a file to it called Skin.skin, with the following contents:

<asp:RequiredFieldValidator runat="server" Display="Dynamic" />
<asp:CompareValidator runat="server" Display="Dynamic" />
<asp:RegularExpressionValidator runat="server" Display="Dynamic" />

..allowing me to have a default setting for a number of different validators (or other controls) across my website.

于 2012-09-14T11:55:52.373 回答