Solved

Assign Different ValidationGroup For Each Instance of User Control

Posted on 2008-10-06
8
1,282 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

709 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

13 Experts available now in Live!

Get 1:1 Help Now