Solved

Assign Different ValidationGroup For Each Instance of User Control

Posted on 2008-10-06
8
1,294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

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.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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