Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Repeater containing a validator

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rory Becker - 03 Dec 2007 09:49 GMT
Hi All

It's been a while since I tried anything like this and it's just driving
me nuts..

I want to have simple templated repeater which contains (Per item) a Label,
a textbox and a CustomValidator.
Below the Repleater is a single button witha serverside function which changes
the text of the button.

I was hoping that when I clicked the button, each row would validate and
postback would not occur if any one of the validators raised an issue.

However the button's text changes regardless of what I put in any of the
boxes.

Can someone point me in the direction of my blunder?

Thanks for any help

FYI I'm using VS2008
Code below

--------------------------------------------------------
   <form id="form1" runat="server">
   <div>
       <asp:Repeater ID="Repeater" runat="server">
       <ItemTemplate>
           <asp:Label ID="MCLabel" runat="server" Text="Label"></asp:Label>
           <asp:TextBox ID="MCTextbox" runat="server"/>
           <asp:CustomValidator ID="MCValidator" runat="server"  ControlToValidate="MCTextBox"
ErrorMessage="Failed" />
       </ItemTemplate>
       </asp:Repeater>
   </div>
   <asp:Button ID="Button1" runat="server" Text="Button" />
   </form>
--------------------------------------------------------

--------------------------------------------------------
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
       Repeater.DataSource = New Object() {1, 2, 3, 4, 5, 6}
       Repeater.DataBind()
   End Sub

   Private Sub Repeater1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater.ItemCreated
       Dim Label As Label = CType(e.Item.FindControl("MCLabel"), Label)
       Dim Validator As CustomValidator = CType(e.Item.FindControl("MCValidator"),
CustomValidator)
       Dim TextBox As TextBox = CType(e.Item.FindControl("MCTextBox"), TextBox)

       Label.Text = "Fred"
       Validator.ControlToValidate = TextBox.ID

       AddHandler Validator.ServerValidate, AddressOf Validator_Validate
   End Sub
   Sub Validator_Validate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
       args.IsValid = CStr(args.Value).Trim = "1" 'Only Validate if Text
= "1"
   End Sub

   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click
       Button1.Text = "Worked"
   End Sub
--------------------------------------------------------

--
Ror
Rory Becker - 03 Dec 2007 10:45 GMT
Ok this seems to have been the result of 2 issues.

1.> I should not have been redatabinding on postback. Ie I should test "if
not isPostback"  before performing databind in the Page_Load

2.> I need to check Page.IsPageValid to determine server side if any validators
have detected non-compliance.

Changes needed to make code work are

--------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Not Page.IsPostBack then '<----------------New "If" test
Repeater.DataSource = New Object() {1, 2, 3, 4, 5, 6}
Repeater.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click
If Not Page.IsValid Then '<----------------New "If" test
Exit Sub
End If
Button1.Text = "Worked"
End Sub
--------------------------------------------------------

--
Ror

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.