cjackson27
asked on
Assign Different ValidationGroup For Each Instance of User Control
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?
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.
ASKER
@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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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?
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?
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
ASKER
@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_CustomerContactLi stView.Cus tomerConta ctsBilling ListView_I temDataBou nd(Object sender, ListViewItemEventArgs e) in C:\Sites\Amp360\Controls\C ustomerCon tactListVi ew.ascx.vb :24
System.Web.UI.WebControls. ListView.O nItemDataB ound(ListV iewItemEve ntArgs e) +108
System.Web.UI.WebControls. ListView.C reateItems WithoutGro ups(ListVi ewPagedDat aSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray) +716
System.Web.UI.WebControls. ListView.C reateChild Controls(I Enumerable dataSource, Boolean dataBinding) +776
System.Web.UI.WebControls. ListView.P erformData Binding(IE numerable data) +33
System.Web.UI.WebControls. DataBoundC ontrol.OnD ataSourceV iewSelectC allback(IE numerable data) +114
System.Web.UI.DataSourceVi ew.Select( DataSource SelectArgu ments arguments, DataSourceViewSelectCallba ck callback) +31
System.Web.UI.WebControls. DataBoundC ontrol.Per formSelect () +142
System.Web.UI.WebControls. ListView.P erformSele ct() +57
System.Web.UI.WebControls. BaseDataBo undControl .DataBind( ) +73
System.Web.UI.WebControls. BaseDataBo undControl .EnsureDat aBound() +82
System.Web.UI.WebControls. BaseDataBo undControl .OnPreRend er(EventAr gs e) +22
System.Web.UI.Control.PreR enderRecur siveIntern al() +80
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Control.PreR enderRecur siveIntern al() +171
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +842
Thanks for continuing to follow this.
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_CustomerContactLi
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.DataSourceVi
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Page.Process
Thanks for continuing to follow this.
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.
If that doesn't work, or you need help w/ that, can you post your code.
ASKER
@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!
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!