Solved

Assign Different ValidationGroup For Each Instance of User Control

Posted on 2008-10-06
8
1,290 Views
Last Modified: 2013-11-08
I have an ASP.NET 3.5 page that has multiple instances of a user control on it. In the InsertItemTemplate of the ListView in the user control I am trying to limit the validation for when the Insert command is fired to only that specific instance of the user control, however the validation for every user control is fired when I try to insert. I have tried adding ValidationGroup='<%# Me.UniqueID %>' to the relevant controls inside the user control with no luck. Any ideas?
0
Comment
Question by:cjackson27
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 22656211
How about adding a Validation Group property to your usercontrol?  Then assign that value to the validationgroup property for your inner controls when the control is loaded.
0
 

Author Comment

by:cjackson27
ID: 22662269
@anoyes

How would I go about doing that.  I understand how to make a property for my control, however I am having trouble programatically assigning the validation group.  Could you provide a code example?
0
 
LVL 16

Accepted Solution

by:
anoyes earned 200 total points
ID: 22662758
I've attached a quick example of the codebehind for a simple control that has just a TextBox, RequiredFieldValidator, and Button.   You'll just add ValidationGroup="VG1" or whatever to your user controls.  The key is that you have to set the ValidationGroup property on everything in that "ControlLoad" sub.

    Private _ValidationGroup As String = ""
 
    Public Property ValidationGroup() As String
        Get
            Return Me._ValidationGroup
        End Get
        Set(ByVal value As String)
            Me._ValidationGroup = value
        End Set
    End Property
 
    Protected Sub ControlLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Me.TextBox1.ValidationGroup = Me._ValidationGroup
        Me.RequiredFieldValidator1.ValidationGroup = Me._ValidationGroup
        Me.Button1.ValidationGroup = Me._ValidationGroup
    End Sub

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:cjackson27
ID: 22662836
@anoyes

Thanks for the help!  The problem I'm now having is that the TextBox and RequiredFieldValidators that I want to get to are inside of a ListView InsertItemTemplate.  How would I set these properties dynamically?
0
 
LVL 16

Expert Comment

by:anoyes
ID: 22663147
You have to add a function to the ItemDataBound event of the list view that looks a little like this.  I think that should do the trick.


    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles ListView1.ItemDataBound
        DirectCast(e.Item.FindControl("TextBox1"), TextBox).ValidationGroup = Me._ValidationGroup
        DirectCast(e.Item.FindControl("RequriedFieldValidator1"), RequiredFieldValidator).ValidationGroup = Me._ValidationGroup
        DirectCast(e.Item.FindControl("Button1"), Button).ValidationGroup = Me._ValidationGroup
    End Sub

Open in new window

0
 

Author Comment

by:cjackson27
ID: 22663262
@anoyes

This is exactly what I've been trying, however I get an " Object reference not set to an instance of an object." error.  Here is the stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   Controls_CustomerContactListView.CustomerContactsBillingListView_ItemDataBound(Object sender, ListViewItemEventArgs e) in C:\Sites\Amp360\Controls\CustomerContactListView.ascx.vb:24
   System.Web.UI.WebControls.ListView.OnItemDataBound(ListViewItemEventArgs e) +108
   System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +716
   System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +776
   System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data) +33
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   System.Web.UI.WebControls.ListView.PerformSelect() +57
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Thanks for continuing to follow this.
0
 
LVL 16

Expert Comment

by:anoyes
ID: 22665371
Hmm I was hoping that'd work.  It must be because the control isn't on the page until you actually hit the button to insert a row.  What about adding something to the control that triggers the 'Insert' mode on the ListView?

If that doesn't work, or you need help w/ that, can you post your code.
0
 

Author Comment

by:cjackson27
ID: 22670441
@anoyes

I ended up just removing the code to insert a new record outside of the ListView and used your existing code from above.  Thanks for the help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question