|
-
May 15th, 2013, 04:48 PM
#1
Thread Starter
Frenzied Member
-
May 16th, 2013, 07:42 AM
#2
Thread Starter
Frenzied Member
Re: Dynamically generated CompareValidator not working right.
Well, I never figured out why they are showing valid on the server side, but I did discover that when I set textbox.causesValidation = true, the client side validation occurs and the value never even goes to the server. So basically my problem is solved - no more invalid values getting to the server.
I feel like such a noob.
 I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
May 16th, 2013, 03:25 PM
#3
Re: Dynamically generated CompareValidator not working right.
Hello,
Is it possible to show the code that you are using? It sounds like the controls that you are creating don't have their event handlers hooked up at the right point, therefore when the events fire, they are not being called.
The reason that I ask is that the problem isn't really solved. You can't rely on JavaScript being enabled on the page, or that some malicious user won't manually switch off JavaScript. As a result, you should definitely still validate the input on the server side. Client side validation should be seen as a convenience for the user preventing the need for round trips to the server, not as the only validation.
Thanks
Gary
-
May 16th, 2013, 04:51 PM
#4
Thread Starter
Frenzied Member
Re: Dynamically generated CompareValidator not working right.
OK. Here's the snippet of code that creates the panel, textbox, and validator:
Code:
'add a spot to hold the value
Dim ValueCell As New Web.UI.HtmlControls.HtmlTableCell
Dim Panel As New UpdatePanel
Panel.UpdateMode = UpdatePanelUpdateMode.Conditional
Panel.ID = "panel_" & Variable.Name
ValueCell.Controls.Add(Panel)
NewRow.Cells.Add(ValueCell)
'add the editor for this item
Dim EditorControl As Control = Editor.CreateVariableEditor(Variable, HostTable.Page, VariableManager)
Panel.ContentTemplateContainer.Controls.Add(EditorControl)
'add the validation control - if any.
Dim ValidationControl As CompareValidator
ValidationControl = Editor.CreateVariableValidator(Variable, HostTable.Page, VariableManager)
If ValidationControl IsNot Nothing Then
ValidationControl.ControlToValidate = EditorControl.ID
Panel.ContentTemplateContainer.Controls.Add(ValidationControl)
End If
Here's the snippet from CreateVariableEditor that creates the textbox:
Code:
Dim TextBox As New TextBox
TextBox.AutoPostBack = True
TextBox.CausesValidation = True
TextBox.ID = "VAR_" & Variable.Name
TextBox.Text = Page.Session.Item(Variable.Name)
AddHandler TextBox.TextChanged, AddressOf Editor.VariableTextBox_TextChanged
CreateVariableEditor = TextBox
and here's the snippet from CreateVariableValidator that creates the CompareValidator:
Code:
Dim ValBox As New CompareValidator()
With ValBox
.ValueToCompare = 10
.Type = ValidationDataType.Integer
.Operator = ValidationCompareOperator.GreaterThan
.ErrorMessage = "Value must be greater than " & .ValueToCompare.ToString
.ID = Variable.Name.ToUpper & "_VALIDATOR"
.SetFocusOnError = True
End With
Finally, the event handler and a related function:
Code:
Public Shared Sub VariableTextBox_TextChanged(sender As Object, e As System.EventArgs)
With CType(sender, TextBox)
'get the variable name
Dim VarName As String = .ID.Substring(4)
'fish out any validator and check it
Dim Validator As CompareValidator = GetValidator(CType(sender, TextBox), VarName)
'apply the variable value
Using VariableMgr As New SessionVariables(.Page.Session)
If Validator IsNot Nothing Then
If Validator.IsValid Then
VariableMgr.SetSessionVariable(VarName, .Text)
End If
Else
VariableMgr.SetSessionVariable(VarName, .Text)
End If
End Using
End With
End Sub
Private Shared Function GetValidator(ByVal C As Control, Name As String) As Control
For Each ValC As Control In C.Parent.Controls
If ValC.ID.Contains(Name & "_VALIDATOR") Then
Return ValC
End If
Next
End Function
 I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
May 17th, 2013, 07:14 AM
#5
Re: Dynamically generated CompareValidator not working right.
Hey,
Which page level events are you doing this work in though?
Is it possible to upload the entire aspx and aspx.vb files?
If not, can you put it together in a small sample application and upload that?
Ultimately, I suspect the issue is going to be with when the control are created within the ASP.Net Page Life Cycle, as described here:
http://www.4guysfromrolla.com/articles/092904-1.aspx
Gary
-
May 19th, 2013, 02:41 PM
#6
Thread Starter
Frenzied Member
Re: Dynamically generated CompareValidator not working right.
It's happening through the page_load event. I'll try to upload the page and support class tomorrow.
 I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
May 20th, 2013, 03:01 AM
#7
Re: Dynamically generated CompareValidator not working right.
 Originally Posted by dolot
It's happening through the page_load event. I'll try to upload the page and support class tomorrow.
Ok, so as per the article that I linked to, that is not early enough in the ASP.Net Page Life Cycle. You will have to alter your code as per the article.
Gary
-
May 20th, 2013, 07:45 AM
#8
Thread Starter
Frenzied Member
Re: Dynamically generated CompareValidator not working right.
Thanks, Gary. I'll go ahead and read that article and the companion articles with it and go from there. If I'm still having issues I'll post again.
 I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|