Solved

Assign Different ValidationGroup For Each Instance of User Control

Posted on 2008-10-06
8
1,285 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now