RE: Validators in composite control firing prematurely
- From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
- Date: Wed, 30 Nov 2005 05:32:46 GMT
Hey Brad,
Have you got any further idea on this? I've just made some further test
and seems we can do some certain trick in our composite control's code. My
current approach is just set the validator's Enabled property to false in
CreateChildControl....
Then, we override our Custom Control's OnLoad method , there when postback,
the validation hasn't take place.... , we can check the Page.Request.Form
collection to see whether the postback is caused by the button in our
composite control. If so, set validator's Enabed to true and manually call
validator's Validate() function, else, set to false....
Also, this approach need to disable clientside validation (since the
enabled state we set in Onload will be persisted take effect at clientside
). If you also need the clientside validation occurs only for our
composte control's sub buttons, we may have to register some custom
clientscript which manually call the validator's clientside validation
function. I think it is also possible, just refer to the following msdn
article which describe the asp.net 1.x validation control's clientside
behavior in detail:
#ASP.NET Validation in Depth
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/aspplusvalid.asp
Here is the my test control's code:
================================
namespace DTControlLib
{
[DefaultProperty("Text"),
ToolboxData("<{0}:ValidateCompositeControl
runat=server></{0}:ValidateCompositeControl>")]
public class ValidateCompositeControl :
System.Web.UI.WebControls.WebControl, INamingContainer
{
private string text;
private TextBox _txt;
private RequiredFieldValidator _rfv;
private Button _btn;
private DropDownList _lst;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
if(Page.IsPostBack)
{
if(Page.Request.Form[_btn.UniqueID] != null)
{
_rfv.Enabled = true;
_rfv.Validate();
}
else
{
_rfv.Enabled = false;
}
}
}
protected override void CreateChildControls()
{
Controls.Clear();
Table tb = new Table();
tb.ID = "tbContainer";
tb.BorderWidth= 1;
tb.CellPadding = tb.CellSpacing = 0;
Controls.Add(tb);
TableRow tr = null;
tr= new TableRow();
tr.Cells.Add(new TableCell());
_txt = new TextBox();
_txt.ID = "txtInput";
_rfv = new RequiredFieldValidator();
_rfv.ID = "rfvInput";
_rfv.ControlToValidate = _txt.ID;
_rfv.EnableClientScript = false;
_rfv.ErrorMessage = "Input value required.....";
_rfv.Enabled = false;
tr.Cells[0].Controls.Add(_txt);
tr.Cells[0].Controls.Add(_rfv);
tb.Rows.Add(tr);
tr= new TableRow();
tr.Cells.Add(new TableCell());
_lst = new DropDownList();
_lst.ID = "lstItems";
_lst.AutoPostBack = true;
_lst.Items.Add("Item1");
_lst.Items.Add("Item2");
_lst.Items.Add("Item3");
tr.Cells[0].Controls.Add(_lst);
tb.Rows.Add(tr);
tr= new TableRow();
tr.Cells.Add(new TableCell());
_btn = new Button();
_btn.ID = "btnSubmit";
_btn.Text = "Submit Input";
_btn.Click += new EventHandler(btn_Click);
_btn.CausesValidation = false;
tr.Cells[0].Controls.Add(_btn);
tb.Rows.Add(tr);
}
private void btn_Click(object sender, EventArgs e)
{
Page.Response.Write("<br>" + sender + " postback.");
}
}
}
==========================
Hope helps. Thanks,
Steven Cheng
Microsoft Online Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 223188659
| References: <ueZyDZI9FHA.252@xxxxxxxxxxxxxxxxxxxx>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: stcheng@xxxxxxxxxxxxxxxxxxxx (Steven Cheng[MSFT])
| Organization: Microsoft
| Date: Tue, 29 Nov 2005 09:15:15 GMT
| Subject: RE: Validators in composite control firing prematurely
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| Message-ID: <mAdwzVM9FHA.1240@xxxxxxxxxxxxxxxxxxxxx>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| Lines: 81
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:31372
| NNTP-Posting-Host: tomcatimport2.phx.gbl 10.201.218.182
|
| Hi Brad,
|
| Welcome to ASPNET control newsgroup.
| from your description, you're developing a custom composite web control
and
| there're some entry fields and validation controls in the composte
control.
| Currently you found that the validator controls will always validate the
| input in all the postback events on the page, so you're wondering any
means
| to make them invoking validation only when the postback is raised by the
| button in your custom control?
|
| If anything I misunderstand, please feel free to let me know. As for the
| ASP.NET's page validation model in 1.X , all the validation controls on
the
| page are in the same scope , so any submit button's postback on the page
| will cause the validation take place (as long as that button's set to
| causeValidation=true....). In ASP.NET 2.0, we can resolve this
problems
| through defining ValidationGroup for our custom control's validators....
|
| Currently what I can got to workaround the problem in 1.1 may be creating
a
| custom Validator control to do the validation in our custom composite
| control. We can put our code logic in the overrided validation function
to
| determine whether to do the validtion or not (only when the post back in
| invoked by the button in our composite control .....). This checking can
| be done through check the Page's Request.Forms collection to see whether
| the submit button's UniqueID is in it...
|
| If you have any other consideration or concerns, please feel free to post
| here.
|
| Thanks,
|
| Steven Cheng
| Microsoft Online Support
|
| Get Secure! www.microsoft.com/security
| (This posting is provided "AS IS", with no warranties, and confers no
| rights.)
|
|
| --------------------
| | From: "Brad" <lane@xxxxxxxxxxxxxxxx>
| | Subject: Validators in composite control firing prematurely
| | Date: Mon, 28 Nov 2005 17:43:10 -0800
| | Lines: 18
| | X-Priority: 3
| | X-MSMail-Priority: Normal
| | X-Newsreader: Microsoft Outlook Express 6.00.3790.1830
| | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
| | Message-ID: <ueZyDZI9FHA.252@xxxxxxxxxxxxxxxxxxxx>
| | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| | NNTP-Posting-Host: xllc.ris.lane.or.us 199.79.32.18
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet.webcontrols:31369
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| |
| | I have a custom composite control with some text boxes, which have
| | validators (requiredfieldvalidators + regularexpression validators).
The
| | problem is the validators are working to well and seem to be firing
even
| on
| | a postback which is set to not cause validation.....and the postback
| | correctly does not cause validation for any of the other input
validators
| on
| | the aspx page.
| | I'm using the CreateChildControls method to set the properties of the
| | composite items and then write them out in Render, interspersed with
some
| | table formatting.
| |
| | If this makes sense and you have some thoughts one what I might look
into
| to
| | correct this I'd appreciate some feedback.
| |
| | Brad
| |
| | This in .Net 1.1
| |
| |
| |
|
|
.
- References:
- Validators in composite control firing prematurely
- From: Brad
- RE: Validators in composite control firing prematurely
- From: Steven Cheng[MSFT]
- Validators in composite control firing prematurely
- Prev by Date: RE: BoundField and DataFormatString in ASP.NET 2.0 - Bug ?
- Next by Date: VS2005 DataList with Nested GridView using ObjectDataSource
- Previous by thread: RE: Validators in composite control firing prematurely
- Next by thread: Re: Dynamic user control Event Handling (VS.NET Ent 2003)
- Index(es):