I have a form onto which I add programatically several controls. This includes Text Boxes, Validators, etc. Here is a section of code that does that adds the controls (AL is a PlaceHolder. RS is a DataReader):
In my save code, I am checking the Page.IsValid before proceeding. The routine saves the data and re-directs to another page. What I am seeing is the red validation errors on the screen that display quickly just before it saves and re-directs to the other
page. It doesn't stop on validation errors. For instance. If I blank out the first name and hit save, it's invalid, but Page.IsValid returns True. Here is the top portion of my save code:
if not Page.IsValid then
exit sub
end if
Problem is that it blows right past this and saves. I have also added Page.Validate() just before that statement, and it made no difference. My assumption is that there is something in the way I'm adding the controls to the page, but I don't know what
that is. I'm thinking that they're not being counted as server controls, however, the "runat" property doesn't exist, so I can't set it as a server control (if that is even the problem).
Does anyone know why Page.IsValid would return True even though it isn't?
I have just some experience working with dynamic controls and I remember that viewstate is sometimes a problem and then you need to take care of it by yourself.
I believe that validators requiere that viewstat is working ok and if you put dynamic controls you'll need to be careful about where do you add them. Here's some info:
http://msdn.microsoft.com/en-us/library/hbdfdyh7.aspx
My recommendation is to avoid dynamic controls. Maybe you can share what is your requirement and we can find another strategy.
If this set of controls is something that will show sometimes then you can define the controls on your ASPX code and box them inside a <DIV runat="server" that you can control programatically.
I believe that dynamic controls may be required in this instance. This is a verification screen in which the user is supposed to verify any number of people associated with a school. There could be 1, 2, 3, or 4 (maybe more) people listed there. So, I
need to create a DataReader that encompasses all of the records, loop through, and display a set of fields and validators for each one.
I attempted to do this initially with standard HTML form fields. While I could accomplish this, it seemed like an awful lot of verification code, so I thought that the dynamic controls with validators would work much better. I was unaware of the limitations
on dynamically generating controls, however. To hear a lot of people talk, there isn't anything you can't do dynamically in code that you can't do by putting a control directly in the form.
I suppose about the only thing I can do is set a max of (say) 5 <div>'s go ahead and put the controls on the form, then loop through the data reader, then just set the .Text value for each one and make the remaining ones invisible.
You can continue working with dynamic controls but it is very similar to using plain HTML controls because you will need to take care of "view state" since they are not maintained on every postback. That may be the reason why the validation is not working
server side.
In the past I have been forced to do something similar to your current situation and here's what I did:
- I generate HTML and the appropiate HTML controls like <input type="text"... having strong control on the ID of each one.
- I put generic JavaScript functions to validate client-side every control
- On the server side, I have to control view state manually
I've just re-coded the page to have 8 different divisions with the same info and named the fields with a 1,2,3,4... behind them. So, this will allow for up to 8 different advisors to be verified. Crude, but it works. At least I know not to try that again.
Jesse
Marked as answer by VorlonShadow on Aug 04, 2009 11:12 PM
No. I've re-written it to work differently now anyway. Less efficient and messy but it works. As I understand it, the problem is an issue with ViewState anyway, so I'm not sure that ValidationGroup would work.
Jesse
Marked as answer by VorlonShadow on Aug 04, 2009 11:10 PM
No. I've re-written it to work differently now anyway. Less efficient and messy but it works. As I understand it, the problem is an issue with ViewState anyway, so I'm not sure that ValidationGroup would work.
Try with the validation group , its working or not.
VorlonShadow
Member
7 Points
73 Posts
Page.IsValid returning True, even though it isn't
Jul 31, 2009 02:05 PM|LINK
I have a form onto which I add programatically several controls. This includes Text Boxes, Validators, etc. Here is a section of code that does that adds the controls (AL is a PlaceHolder. RS is a DataReader):
AL.Controls.Add(New LiteralControl("<tr>" & vbCRLF))
AL.Controls.Add(New LiteralControl(" <td align='right' valign='top'>First Name</td>" & vbCRLF))
AL.Controls.Add(New LiteralControl(" <td>" & vbCRLF))
TB = New TextBox
TB.id = "AdvisorFirstName" & RS("ID").ToString
TB.columns = 20
TB.maxlength = 25
TB.Text = RS("FirstName").ToString
AL.Controls.Add(TB)
RFV = New RequiredFieldValidator
RFV.ControlToValidate="AdvisorFirstName" & RS("ID").ToString
RFV.Display=ValidatorDisplay.Dynamic
RFV.Text="Required"
RFV.ErrorMessage="Primary Advisor First Name is required"
AL.Controls.Add(RFV)
AL.Controls.Add(New LiteralControl(" </td>" & vbCRLF))
AL.Controls.Add(New LiteralControl("</tr>" & vbCRLF))
In my save code, I am checking the Page.IsValid before proceeding. The routine saves the data and re-directs to another page. What I am seeing is the red validation errors on the screen that display quickly just before it saves and re-directs to the other page. It doesn't stop on validation errors. For instance. If I blank out the first name and hit save, it's invalid, but Page.IsValid returns True. Here is the top portion of my save code:
if not Page.IsValid then
exit sub
end if
Problem is that it blows right past this and saves. I have also added Page.Validate() just before that statement, and it made no difference. My assumption is that there is something in the way I'm adding the controls to the page, but I don't know what that is. I'm thinking that they're not being counted as server controls, however, the "runat" property doesn't exist, so I can't set it as a server control (if that is even the problem).
Does anyone know why Page.IsValid would return True even though it isn't?
Thanks,
Jesse
aderegil
Contributor
3318 Points
593 Posts
Re: Page.IsValid returning True, even though it isn't
Jul 31, 2009 02:50 PM|LINK
Hello,
I have just some experience working with dynamic controls and I remember that viewstate is sometimes a problem and then you need to take care of it by yourself.
I believe that validators requiere that viewstat is working ok and if you put dynamic controls you'll need to be careful about where do you add them. Here's some info: http://msdn.microsoft.com/en-us/library/hbdfdyh7.aspx
My recommendation is to avoid dynamic controls. Maybe you can share what is your requirement and we can find another strategy.
If this set of controls is something that will show sometimes then you can define the controls on your ASPX code and box them inside a <DIV runat="server" that you can control programatically.
Just some ideas.
VorlonShadow
Member
7 Points
73 Posts
Re: Page.IsValid returning True, even though it isn't
Jul 31, 2009 05:24 PM|LINK
I believe that dynamic controls may be required in this instance. This is a verification screen in which the user is supposed to verify any number of people associated with a school. There could be 1, 2, 3, or 4 (maybe more) people listed there. So, I need to create a DataReader that encompasses all of the records, loop through, and display a set of fields and validators for each one.
I attempted to do this initially with standard HTML form fields. While I could accomplish this, it seemed like an awful lot of verification code, so I thought that the dynamic controls with validators would work much better. I was unaware of the limitations on dynamically generating controls, however. To hear a lot of people talk, there isn't anything you can't do dynamically in code that you can't do by putting a control directly in the form.
I suppose about the only thing I can do is set a max of (say) 5 <div>'s go ahead and put the controls on the form, then loop through the data reader, then just set the .Text value for each one and make the remaining ones invisible.
Jesse
srinivaskotr...
Star
11228 Points
1792 Posts
Re: Page.IsValid returning True, even though it isn't
Jul 31, 2009 06:37 PM|LINK
hi,
this links may helps you
http://msdn.microsoft.com/en-us/library/0ke7bxeh.aspx
http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx
Thanks :)
Srinivas Kotra.
aderegil
Contributor
3318 Points
593 Posts
Re: Page.IsValid returning True, even though it isn't
Jul 31, 2009 08:29 PM|LINK
OK I understand you better now.
You can continue working with dynamic controls but it is very similar to using plain HTML controls because you will need to take care of "view state" since they are not maintained on every postback. That may be the reason why the validation is not working server side.
In the past I have been forced to do something similar to your current situation and here's what I did:
- I generate HTML and the appropiate HTML controls like <input type="text"... having strong control on the ID of each one.
- I put generic JavaScript functions to validate client-side every control
- On the server side, I have to control view state manually
VorlonShadow
Member
7 Points
73 Posts
Re: Page.IsValid returning True, even though it isn't
Jul 31, 2009 08:50 PM|LINK
I've just re-coded the page to have 8 different divisions with the same info and named the fields with a 1,2,3,4... behind them. So, this will allow for up to 8 different advisors to be verified. Crude, but it works. At least I know not to try that again.
Jesse
venkatu2005
All-Star
32487 Points
6742 Posts
Re: Page.IsValid returning True, even though it isn't
Aug 01, 2009 07:38 AM|LINK
have you tried to set ValidationGroup for Validators and Button
Thanks.
VorlonShadow
Member
7 Points
73 Posts
Re: Page.IsValid returning True, even though it isn't
Aug 01, 2009 12:19 PM|LINK
No. I've re-written it to work differently now anyway. Less efficient and messy but it works. As I understand it, the problem is an issue with ViewState anyway, so I'm not sure that ValidationGroup would work.
Jesse
venkatu2005
All-Star
32487 Points
6742 Posts
Re: Page.IsValid returning True, even though it isn't
Aug 03, 2009 03:43 AM|LINK
Try with the validation group , its working or not.
Thanks.
aderegil
Contributor
3318 Points
593 Posts
Re: Page.IsValid returning True, even though it isn't
Aug 04, 2009 10:12 PM|LINK
Hello VorlonShadow:
If your problem is resolved now please mark this thread as answered. Thanks!
Otherwise, give us more info about your current status.