Disable Validation For Dynamically Created Control

  • Thread starter Thread starter Steve Harclerode
  • Start date Start date
S

Steve Harclerode

This is a standard .Net question, although my starting point is a Commerce
Server 2007 application that I need to modify.

I have a page that includes 2 user controls: a LoginView and a custom
control that is generated dynamically which shows the line items in a
shopping cart. The client does not need to be logged in to see the shopping
cart.

My client has asked me to put a trash can button next to each line item that
can be used to delete that line item. I have everything hooked up and
working, *except* that the postback from the trash button causes validation
for the login form. This means that the validation controls
(RequiredFieldValidator controls) display their error messages after I click
the trash button.

Is there some way to "abort" processing of the validation? It looks as
though the validation is happening on the server side, since I am able to
use the debugger to step into my handler function after hitting the trash
button.

Here's the code for a text field plus the associate validator:
<asp:TextBox ID="UserName" Width="155" runat="server"
CssClass="estoretextbox" ValidationGroup="Login" />
<asp:RequiredFieldValidator ID="reqPassword"
ControlToValidate="Password" ErrorMessage="Please enter Password"
runat="server" Display="Dynamic"
ValidationGroup="Login"></asp:RequiredFieldValidator>

And here's C# code that I use to create the trash button:

ImageButton btnTrash = new ImageButton();
btnTrash.ID =
this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
btnTrash.ImageUrl = "images/trashcan.jpg";
btnTrash.Height = new Unit(15);
btnTrash.Width = new Unit(10);
// Some things I have tried:
//btnTrash.ValidationGroup = "dummy";
//btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
//btnTrash.CausesValidation = false;
btnTrash.RenderControl(writer);
btnTrash.ValidationGroup = "dummy";
btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");
 
On my first attempt at this newsgroup post, I cut & pasted some code in the
wrong order. The corrected code is below.
----------------------

This is a standard .Net question, although my starting point is a Commerce
Server 2007 application that I need to modify.

I have a page that includes 2 user controls: a LoginView and a custom
control that is generated dynamically which shows the line items in a
shopping cart. The client does not need to be logged in to see the shopping
cart.

My client has asked me to put a trash can button next to each line item that
can be used to delete that line item. I have everything hooked up and
working, *except* that the postback from the trash button causes validation
for the login form. This means that the validation controls
(RequiredFieldValidator controls) display their error messages after I click
the trash button.

Is there some way to "abort" processing of the validation? It looks as
though the validation is happening on the server side, since I am able to
use the debugger to step into my handler function after hitting the trash
button.

Here's the code for a text field plus the associate validator:
<asp:TextBox ID="UserName" Width="155" runat="server"
CssClass="estoretextbox" ValidationGroup="Login" />
<asp:RequiredFieldValidator ID="reqPassword"
ControlToValidate="Password" ErrorMessage="Please enter Password"
runat="server" Display="Dynamic"
ValidationGroup="Login"></asp:RequiredFieldValidator>

And here's C# code that I use to create the trash button:

ImageButton btnTrash = new ImageButton();
btnTrash.ID =
this.GetUniqueImageControlName(lineItem.LineItemId.ToString());
btnTrash.ImageUrl = "images/trashcan.jpg";
btnTrash.Height = new Unit(15);
btnTrash.Width = new Unit(10);
// Some things I have tried:
//btnTrash.ValidationGroup = "dummy";
//btnTrash.Attributes.Add("onclick", "Page_ValidationActive=false");
//btnTrash.CausesValidation = false;
btnTrash.Attributes.Add("OnServerClick", "TrashButton_Click");

btnTrash.RenderControl(writer);
 
Hi Steve,

Looking at your code it seems that you did try setting "CausesValidation"
Property (of trash button) to false (though i must say it is not a good idea
if the button in question is not a "Reset" or "Clear" button), so was curious
to know did it not work for you?

Can you also paste the code that is executed when a PostBack is triggered?

regards,
Joy
 
Hi Joy,

Thanks. Here's the method signature of the callback function (plus a few
lines of code). I'm not 100% sure how LoadPostData gets wired up, but
setting breakpoints here in the debugger makes it clear that it is in fact
being called:
public bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
if (postCollection == null)
{
throw new ArgumentNullException("postCollection");
}

IEnumerable<LineItem> lineItems = this.Helper.GetAllLineItems();
// first check to see if trash icon was clicked, and if so set
the quantity to 0 for the item
foreach (LineItem lineItem in lineItems)
{
string value =
postCollection[this.GetUniqueImageControlName(lineItem.LineItemId.ToString())
+ ".x"];
if (!String.IsNullOrEmpty(value))
{
lineItem.Quantity = 0;
}
}
....

Here's the method that I'm overriding in order to create the WebControl
contents. It occurs to me the rendering might be happening after the
validation is done for the control that contains the LoginView. Not sure
what I can do about that, without re-writing the entire WebControl to be a
bit less dynamic:
protected override void RenderContents(HtmlTextWriter writer)
{
// rendering stuff here
}

Here's the class signature for the WebControl:
public class BasketDetail : WebControl, IPostBackDataHandler,
IPostBackEventHandler
 
Hi Steve,

Thanks for pasting the code. However i wanted to see if you were calling
"Validate" method on any of the controls and also wanted to see if you are
checking for "Page.IsValid" somewhere in your postback method.

Could you please confirm the following things:

1. Is Validate methode being called?
2. Is Page.IsValid being called?
3. Did setting "CausesValidation=false" for the Trash button help?

regards,
Joy

Steve Harclerode said:
Hi Joy,

Thanks. Here's the method signature of the callback function (plus a few
lines of code). I'm not 100% sure how LoadPostData gets wired up, but
setting breakpoints here in the debugger makes it clear that it is in fact
being called:
public bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
if (postCollection == null)
{
throw new ArgumentNullException("postCollection");
}

IEnumerable<LineItem> lineItems = this.Helper.GetAllLineItems();
// first check to see if trash icon was clicked, and if so set
the quantity to 0 for the item
foreach (LineItem lineItem in lineItems)
{
string value =
postCollection[this.GetUniqueImageControlName(lineItem.LineItemId.ToString())
+ ".x"];
if (!String.IsNullOrEmpty(value))
{
lineItem.Quantity = 0;
}
}
....

Here's the method that I'm overriding in order to create the WebControl
contents. It occurs to me the rendering might be happening after the
validation is done for the control that contains the LoginView. Not sure
what I can do about that, without re-writing the entire WebControl to be a
bit less dynamic:
protected override void RenderContents(HtmlTextWriter writer)
{
// rendering stuff here
}

Here's the class signature for the WebControl:
public class BasketDetail : WebControl, IPostBackDataHandler,
IPostBackEventHandler

Joy said:
Hi Steve,

Looking at your code it seems that you did try setting "CausesValidation"
Property (of trash button) to false (though i must say it is not a good
idea
if the button in question is not a "Reset" or "Clear" button), so was
curious
to know did it not work for you?

Can you also paste the code that is executed when a PostBack is triggered?

regards,
Joy


:
 
Hi Joy,

I have put my answers inline below.

When I can find time, I'll created a stripped down version of the issue and
post it. I don't want to put too much code into a text newsgroup, most of
which isn't relevant to the issue.

regards,
Steve

Joy said:
1. Is Validate methode being called?
No. I checked all controls, the ASPX file, and the .master page.
2. Is Page.IsValid being called?
No, I checked all the relevant files.
3. Did setting "CausesValidation=false" for the Trash button help?
Nope. This was what I tried first.
 
Back
Top