Absracting out a Regular Expression in ASP.Net validation - asp.net

I have searched everywhere and I cannot find a good solution to my problem. I have a regular expression used to validate the text input in an ASP textbox.
Given the fact that this regex is repeated on a number of controls I want to code it once and then call it where it needs to be used. As opposed to repeating the regex across all textboxes, which IMHO is bad practice i.e. if its needs updating then I need to updated it everywhere.
The only possible solution I found was Setting RegularExpressionValidator ValidationExpression at runtime. This does work but I am wondering if it is the only way to do it. The reason being I need to create a RegularExpressionValidator for each textbox control to validate and set the Error message and Expression on each in the Page_Load. Not the end of the world but seems kind of heavy.
Any ideas?
Thanks,
Michael

You could set the value in a constant or static variable. You could then create a class that inherits from the RegularExpressionValidator and set the property to that variable or constant.
Then your custom derived validator will always have that property set.

Related

ASP Net - Casting a Request.Form control from Code Behind

This seems really simple, but for some reason Im stumped..
Im dynamically generating an HTML Select Box, lets call it myselect. Im creating this select box based on some database values Im generating an HTML Select Box.. almost like a string that Im just spitting out to the page. So it's never a control in the codebehind, just part of a string thats rendered as HTML by the browser. If I submit my form, and in my codebehind I perform:
Dim myVal as String = Request.Form("myselect")
That code will give me the VALUE of the myselect select box. How can I refer to this control to cast it as a System.Web.UI.HtmlControls.HtmlSelect control? Request.Form seems to give me the value, but I want to reference the object itself..
If this select box isn't declared statically on your page (that is, you're adding it to the Control Collection), you'll have to do something like this: Dim MySelect as HtmlSelect = Page.FindControl("MySelect") as HtmlSelect
You'll have to forgive me if my casting syntax is a bit off -- I'm used to casting in C#.
If your dynamically generating the control in your code file, then it will not be available to you on post back. As long as you generate it again before the viewstate is processed (you can do it in your oninit), then you can access it as you would anything else.
MySelect.SelectedValue
In response to the comments above (thanks for your help), I found what Gabriel McAdams and jwiscarson had to say were true. In browsing the Request object, I found that its nothing more than a collection of key\value pairs. Performing Request.Form("myformobj") will return a value, because thats all thats available to the application. If necessary, I suppose I can whip up some nifty javascript to track form object types, but it's certainly not necessary in my case.
Thanks for the help

asp.net validation: how to compare values of 2 controls on the form and using some functions like substring,

I have to compare 2 values which are the text property of 2 textbox controls on my .aspx form.
Using Compare validator does not enough for, I have to use some functions like substring.
As far as I know, the Compare validator does not allow to use substring.
Using Custom validator and handle it's serer-side "ServerValidate" event, as you know, It gives 2 arguments (source and args), how can I compare a text property of other control in this function?
If I can not, what is your solution?
In the "ServerValidate" event, you can access the controls text property
if(txtbox1.Text!=textbox2.Text)
{
//you can return what you want here.....
}

ASP.Net: Is it possible to skip databinding of an element if an error occurs?

I use a lot of repeaters for different elements of our sites, and I've always wondered if there was a way to have the repeater skip an element if an exception occurs instead of having the whole page crash?
In particular, I've inherited a system from another developer that using a similar design, however he didn't include any kind of validation for his business objects, and it a single property is missing, the whole thing goes up in smoke.
Any suggestions?
Thanks in advance!
The simplest suggestion I can offer is the check the validity of the data before it's passed to the repeater. I don't believe there's any way to get the stock repeater to skip a data element on error.
The other approach is to build your own repeater, inheriting from the base Repeater, to add that functionality but I've no sample code to offer. Perhaps someone else may be able to help there.
The way I see it, you have at least three options.
You could create a custom repeater control that inherits System.Web.UI.WebControls.Repeater and override the databinding behaviour to be more try-catchy (probably fail silently on databinding errors). You couldd then easily replace all instances of the standard Repeater with this new one.
You could filter your datasources before databinding to remove items you know are going to cause problems beforehand. This option may be quite laborious and something of an iterative process.
You could try adding default values to the business objects, so that the properties you're binding to return a default instance rather than null (not nice either).
That's my thoughts anyway.
One question - you say "when a property is missing". Do you mean he's using a style of databinding syntax that offers no compile-time checking and is referencing properties that don't exist, or is referecing properties that are null?
Edit
OK, so you're referencing properties that are null. If you have access to the code for the business objects you could modify them so they return a new, non-null instance (this is the third option I gave).
You don't say if you're using .net 3.5, but I'll assume you are. You could add a new property "IsValidForDataBinding" on to each of your business objects. In the getter logic you could check each of the necessary properties and sub-objects to check for validity, non-nullness etc and return a bool. When you come to bind your repeater, write a simple linq statement that filters-out the invalid items (i.e. where IsValidForDataBinding = false). Having said that, I still think that writing a derived repeater control could be your easiest option.
Have you tried using string.isnullorempty("the string") to check for a value before referencing the property?
Here's a reference: MSDN

Why should I create my child controls in CreateChildControls() on a CompositeControl?

Ok so the obvious answer is, because the flow of a composite control demands my childcontrols to be created at a certain point in time. I got a problem i think other people must have had as well.
My control is a composite "container/collection" control. It will be fed with an object and based on that objects data it will create a number of childcontrols. So my control will render a header (always) and x-number of, say TextBox controls (based on the object it was fed with).
I'm creating my header in CreateChildControls() obviously, but i can't possibly create my TextBoxes there as well, because i don't know if the object (to base the TextBoxes on) has been fed yet? I thought of exposing a property/method to set/fed the object through, but i'm not sure when it will be called.
So what do i do? I mean i can't possibly create the TextBoxes in CreateChildControls() or can I? I mean - when is CreateChildControls() called - i know i can call EnsureChildControls() (which i already do in a property to set the innerText of the header - since i need the header to be created before setting its innerText obviously).
How about this
var c = new MyContainerControl();
c.Header = "fun";
c.TextBoxObject = myTextBoxes;
That would raise an error (or at best not create any TextBox'es) if i put the building of the TextBoxes in CreateChildControls().
Would it be more sane to instead just store the Header in a member variable and thus not having to call EnsureChildControls() in the exposed method/property setting the Header innerText. I just don't like this aproach much, since it would be complicating things by adding extra logic to store temporarely and later having to figure out when to set it (probably in PreRender).
Also i guess i could make some kind of Databound control, ensuring the data be present at the time of calling .DataBind(). I really don't like this either since last i looked at creating databound controls it got very complicated.
This really should be an easy task to solve - I know I'm missing something somewhere...
what you're describing IS a databound control. And yes, it's somewhat complicated, but it's the proper design paradigm for this type of instance.
That said, had you considered utilizing the repeater control rather than trying to roll out your own composite which behaves in the exact same manner? Rather than passing it a random object, pass it a collection or an iList with the number of text areas you're wanting.

What are the naming guidelines for ASP.NET controls?

We are in the process of nutting out the design guidelines we would like to use in our development team and got into a discussion today around how ASP.NET controls should be named. I am talking about our good friends Label, TextBox, Button etc.
We came up with the following three possibilities that we voted on: (Example is a TextBox to enter/display a FirstName)
Add the control type as a postfix to your controls ID: [FirstName_TextBox] or [FirstName_tbx]
Add the control type as a prefix to your controls ID [tbxFirstName]
Set the ID of the control to FirstName and name related fields (like a label for the textbox or a validator) as in option 2 [lblTextBox].
We ended up deciding to use option 2. It's not as verbose as option 1 and I like that it specifies what control it is before the name of the control.
My question is whether Microsoft has released any guidelines for these prefixes and or if you have any comments about our decision.
The reason Visual Studio adds "TextBox1" when you add it to the page is because Microsoft has no way of knowing how you intend to use it. Naming it "Control1" would be too confusing because it could be any number of controls.
Microsoft provides guidance in general for OO naming conventions, but not specifically for naming UI controls. Since UI controls are ultimately variables used in code, they should follow the same convention as any other variable - no hungarian notation prefix.
msdn.microsoft.com/en-us/library/xzf533w0(vs.71)
msdn.microsoft.com/en-us/library/ms229002(VS.80)
The main reasons are...
Type of control may change from textbox to listbox, then all associated code will have to be fixed (noted earlier)
Your code should be more concerned with the content of the control and less with what type of control it is. When you are concerned with the type of the control, you start to depend on certain functionalities and you break encapsulation - you should be able to easily swap controls without changing much or any code. (Basic OOP principle)
It is fairly easy to come up with prefixes for the standard controls, but new controls are being developed every day. You may make your own WebUserControl, or you may purchase a set of third party controls. How will you decide which prefix to use for the custom controls? Instead of focusing on the type of control, your code should be concerned with what information is contained in it.
Examples
txtFirstName => firstName or FirstName
txtState => state or State
cboState => state or State (prime example of changing control types what about lstState or rdoState - they should all have the same name because your code is not concerned about the type of control,rather the state the user selected)
ctlBilling => billingAddress or BillingAddress (custom control - with hungarian notation it is not very evident what the control even is, but with a meaningful name I begin to understand the information contained in it. i.e. billingAddress.Street, billingAddress.FullAddress etc.)
Not sure about Microsoft official standards, but this is what i've done through out my development career.
I generally abbreviate the control type in front of the the name of what the control does.
I keep the abbreviation lower case and the control's name CamelCase.
E.g. A texbox for username becomes tbUserName
Here is a list of standard abbreviations I use:
Abbr - Control
btn - Button
cb - CheckBox
cbl - CheckBoxList
dd - DropDownList
gv - GridView
hl - Hyperlink
img - Image
ib - ImageButton
lbl - Label
lbtn - LinkButton
lb - ListBox
lit - Literal
pnl - Panel
ph - PlaceHolder
rb - RadioButton
rbl - RadioButtonList
txt - Textbox
I find that most of the time I care about what kind of information the control is for rather than what control type is currently being used to capture that data, so I prefer the type of information before the control type, so I can find it in a sorted list in the IDE:
AgeRangeDropDownList
AgreedToTermsCheckBox
FirstNameTextBox
LastNameTextBox
VS:
chkAgreedToTerms
ddlAgeRange
txtFirstName
txtLastName
I'm not sure of the guidelines regarding ASP.NET, but in the book Framework Design Guidelines from Microsoft, there are several best-practice guidelines about naming of class members. Since ASP.NET Controls in most cases result in a protected field of the appropriate type, I consider these naming guidelines to apply for ASP.NET controls as well. In fact Code Analysis does not differentiate on Control reference fields and other fields.
These guidelines recommend using a naming scheme that implies the logical use rather than a type-descriptive variant. There are several reasons for this. The prefix is implies a type to the developer that might not be correct due to later changes. It adds an extra step in code maintainence. If you change your Button control into a LinkButton control the name also needs to be changed to correct the prefix.
For that reason I would call the control FirstNameEdit etc...
Microsoft does provide some guidance here.
When you drag a control onto a web form you get something like "TextBox1" automatically. That's the IDE telling you that you should change the "1" part for your specific needs.
In that case, "TextBoxFirstName" seems like the way to go.
Two reasons why I prefer option 1:
FirstNameTextBox more closely matches my business object.
More usable with IntelliSense.
Having said that I am considering changing to FirstNameCtrl for the reason csgero pointed out about changing control types. So why bother with any postfix or prefix, to reduce/remove the posibility of conflicts with asp/win form properties.
I think it is better to use option 1 because it is easy to find the field by its meaning and its usage to understand programming coding down the road . Also,it is more usable with IntelliSense to find where we use this field for in our programming code. Therefore I can find the right control by the name of the meaningful field. I will not remmember what kind of control I use for this field but I can find this field by using the meaningful of field name instead of the type of control example I want to find "City" control , I just typ "City" , Intellisence will show me all information for this control but if I do not remember what kind of control I use for , I do not know what to begin....
Abbreviation || ASP.NET Control
STANDARD CONTROLS:
btn Button
cb CheckBox
cbl CheckBoxList
ddl DropDownList
fu FileUpload
hdn HiddenField
lnk Hyperlink
img Image
ibtn(btn) ImageButton
lbl Label
lbtn(btn) LinkButton
lb ListBox
lit Literal
mv MultiView
pnl Panel
ph PlaceHolder
rb RadioButton
rbl RadioButtonList
tbl Table
txt TextBox
v View
DATA CONTROLS
dtl DataList
dp DataPager
dtv DetailsView
ets EntityDataSource
fv FormView
gv GridView
lds LinqDataSource
lv - ListView
ods ObjectDataSource
qe QueryExtender
rpt Repeater
smd SiteMapDataSource
sds SqlDataSource
xds XmlDataSource
VALIDATION CONTROLS
cpv CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
vs ValidationSummary
VALIDATION CONTROLS:
cpv // CompareValidator
ctv CustomValidator
rv RangeValidator
rev RegularExpressionValidator
rfv RequiredFieldValidator
We also use number 2, however I'm not totally convinced it is a good approach. It is Hungarian notation from the "bad" kind, meaning that the prefixes signal the type (syntax) and not the purpose (semantics). The problem with this is that what starts as a TextBox might later become a DropDown and then a RadioButtonGroup, and you'll have to rename the control each time.
Almost everyone uses Hungarian-style prefixes (option 2).
Semantic naming is less useful because "Firstname" is actually the texbox.Text value, not the textbox itself.
I tend to go with the control type as a prefix and the name of the control afterwards but I always CamelCase so in your example for different types of controls you might have..
TxbFirstName
DdFirstName
ChbFirstName
For intellisense reasons I also always fully qualify the name of the control so I wouldn't do any of the following...
TxbFName
TxbClientNo
TxbNoOfCpn
But ultimately down to personal preference
I've been struggling with this problem too. I used to use the "hungarian style prefix".
Now I take a different approach, I try to see the controls as private fields from my class. I don't pre- of postfix my private fields with their type, so why should I do that to an TextBox?
So what used to be:
var newCustomer = new Customer();
newCustomer.Name = txtName.Value;
newCustomer.Address = txtAddress.Value;
newCustomer.City = txtCity.Value;
newCustomer.HasEnoughMoney = cbHasMoney.Selected;
Becomes:
var newCustomer = new Customer();
newCustomer.Name = name.Value;
newCustomer.Address = address.Value;
newCustomer.City = city.Value;
newCustomer.HasEnoughMoney = hasMoney.Selected;
To be honest, I couldn't care less if the "name" control is a text box or what else, I just want it's value.
And if it's not clear enough if your talking about your control or about another field/variable, I think you should either reconsider the name of that field/variable or the function of you class (meaning it might be a little bit to big?).
If you look at it from a code maintainance point of view what would be the best notation to look at after you did the code 2 years ago. Although we try to ensure that forms don't have too many fields on them we all know that this sometimes happens. If we used the Hungarian type notation by prepending the control type I think it would be easier to see where that value is coming from instead of having to figure it out in the event the variable name doesn't make it obvious. If you are using any type of Refactoring tool then changing the name of the control would change the code automatically thereby reducing the change control arguement.
I don’t think there’s a right or wrong answer here, whatever you decide, I think the most important aspect is just to be consistent when it actually comes to coding.
I use uxCity so that you know it's definitely the User Interface control and not some other object but you don't need to change it if you go from a TextBox to a DropDownList.
However If I had a DropdownList and a Textbox, I'd need to use dlCity and txtCity
Or I'd use a combo cboCity.
Hungarian notation was de rigeur when you were limited to 8-character names and no intellisense or debug highlighting. It was a discipline and you could see that if the coding style was correct, the code was likely to be correct. It was used on variables too so you could read the code and understand it as it was a DIY type enforcement.
However, I do use CityTextbox, CityTextboxLabel
CityUx, CityUxLbl
It all depends on who is setting the standards on the project.
I found the only reason to use Hungarian notation was that the IDE did not have intellisense and it was not easy to figure out what was what so iCounter was an integer
However the time of using brief to develop is long gone and the IDE shows you the info in a second
Then you inherit VB.NET code and it is not case sensitive so what do you do?
lblFirstName, txtFirstName
One is a label the other is the textbox
So how do you name these without case sensitivity and actually know what they are?
uxFirstName & uxFirstName does not work
the only answer i have found is to use Hungarian notation, yes i threw up in my mouth. Then again its vb.net and it should be case sensitive since .net is and all of the compiles to IL.
Not really sure about any guidelines, i suspect there are, but I always use number 2 as well!
these are really just based on your preferences but yes, option 2 as described is less verbose and indicates to you the type of control even before its name is shown.

Resources