Repeater containing a validator

R

Rory Becker

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
--------------------------------------------------------
 
R

Rory Becker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top