Solved

Assign Different ValidationGroup For Each Instance of User Control

Posted on 2008-10-06
8
1,295 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
Independent Software Vendors: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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