469,947 Members | 2,285 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,947 developers. It's quick & easy.

User Controls

I have a web form with 2 user controls on it (UC1 and UC2).
Each control has a bound datagrid with textboxes in the footer to add a new
row. There are also requiredfieldvalidators in each footer.
When I try to add a new row to UC1 the requiredfieldvalidators for UC2 fire
and therefore the page is not posted.
If I remove one control, everything works fine.

I have 2 questions.
1. How can I get the validators for each control to only function for their
particular control?

2. What is the best way to share an oledb connection between the 2 controls,
so each one does not open a new connection?

Thanks for any help.

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us

Jul 21 '05 #1
10 2858
Hello Alphonse,

Thanks for your post. I now share the following inforamtion with you:
1. How can I get the validators for each control to only function for their particular control?

As you know, a RequiredFieldValidator should be bound to a designated
control and other controls will not affect it. I created a sample and it
works properly (does not reproduce the problem you are facing). Here is
what I did:

a. Create a Web app.

b. Add two Web Forms containing a Edit box and a RequiredFieldValidator
(set ControlToValidate properly) respectively.

c. Converting the Web Forms page to user controls per MSDN article below:
http://msdn.microsoft.com/library/de...us/vbcon/html/
vbwlkwalkthroughconvertingwebformtousercontrol.asp

d. Add a new Web Forms page containing two user controls, and
RequiredFieldValidators work as expected.

Please check it on your side. If the problem persists, could you post a
reproducible project?
2. What is the best way to share an oledb connection between the 2

controls, so each one does not open a new connection?

You can declare OleDbConnection as public member and pass it to your user
controls.

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #2
Thanks for the reply Tian.
I built a project as you suggested except I added a submit button to each
user control and have the same problem.
Here is what I did:
In a new VSN2002 project
Project > Add Web User Control
WebUserControl1.ascx
==================================
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.vb" Inherits="Validator.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ErrorMessage="Entry Required"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator></P>
<P>
<asp:Button id="Button1" runat="server" Text="Submit 1"></asp:Button></P>
======================================
codebehind: WebUserControl1.ascx.vb
====================================
Public MustInherit Class WebUserControl1

Inherits System.Web.UI.UserControl

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected WithEvents RequiredFieldValidator1 As
System.Web.UI.WebControls.RequiredFieldValidator

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

End Sub

End Class

================================================== ===================

Created a second control identical to the first (except that I labeled the
submit button as Submit 2) and named it WebUserControl2.
================================================== ==
On the default Webform1.aspx I added the 2 controls.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="Validator.WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl2"
Src="WebUserControl2.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1></P>
<P>
<uc1:WebUserControl2 id="WebUserControl21"
runat="server"></uc1:WebUserControl2></P>
</form>
</body>
</HTML>
================================
CodeBehind:
================================
Public Class WebForm1

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

End Sub

End Class

================================================== =========

When I view the form in a browser, I then see 2 text boxes and 2 submit
buttons.
If I make an entry in the first textbox and click 'Submit1', the
requiredfield validator for the second textbox displays its message and the
form is not submitted.
The only way to submit the form is to make an entry in both textboxes.
I would like to be able to click 'Submit 1' and only require the first
textbox to have an entry or click 'Submit 2' and only require the second
text box to have an entry.
I guess what is happening makes sense since the rendered html contains only
one form and the validators' validation is required for the form to be
submitted.
It seems that there should be a way around this.

==========================

As for item 2, when you say 'declare OleDbConnection as public member", am I
correct in assuming that you mean in the parent form?
How then does one pass it to (or reference it in) the user controls?
--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:vN**************@cpmsftngxa07.phx.gbl...
Hello Alphonse,

Thanks for your post. I now share the following inforamtion with you:
1. How can I get the validators for each control to only function for their particular control?

As you know, a RequiredFieldValidator should be bound to a designated
control and other controls will not affect it. I created a sample and it
works properly (does not reproduce the problem you are facing). Here is
what I did:

a. Create a Web app.

b. Add two Web Forms containing a Edit box and a RequiredFieldValidator
(set ControlToValidate properly) respectively.

c. Converting the Web Forms page to user controls per MSDN article below:
http://msdn.microsoft.com/library/de...us/vbcon/html/ vbwlkwalkthroughconvertingwebformtousercontrol.asp

d. Add a new Web Forms page containing two user controls, and
RequiredFieldValidators work as expected.

Please check it on your side. If the problem persists, could you post a
reproducible project?
2. What is the best way to share an oledb connection between the 2

controls, so each one does not open a new connection?

You can declare OleDbConnection as public member and pass it to your user
controls.

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #3
Hi Alphonse,

Thanks for your code. I am checking it and will update you with my findings.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #4
Hi Alphonse,

Thanks for your code. I am checking it and will update you with my findings.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #5
Tian,

Thanks for the reply.
I have done a LOT of searching on the validator issue and found the
following article:
http://msdn.microsoft.com/library/de...pplusvalid.asp

According to it and the little other info I was able to find, the way to
accomplish what I want it to call ValidatorEnable on the client side to
disable the validator.
As a test, I am calling a javascript function on page load which contains
the following code:

strFocus="valrUserLoginF";
ValidatorEnable(strFocus, true);

With this, I receive a runtime error: 'style' is null or not an object.
So, the new #1 question is, how do I get the ValidatorEnable function to
work?

Still need expansion on the connection issue, per my last post.

Thanks,

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:1S**************@cpmsftngxa07.phx.gbl...
Hi Alphonse,

Thanks for your code. I am checking it and will update you with my findings.
Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #6
Tian,

Thanks for the reply.
I have done a LOT of searching on the validator issue and found the
following article:
http://msdn.microsoft.com/library/de...pplusvalid.asp

According to it and the little other info I was able to find, the way to
accomplish what I want it to call ValidatorEnable on the client side to
disable the validator.
As a test, I am calling a javascript function on page load which contains
the following code:

strFocus="valrUserLoginF";
ValidatorEnable(strFocus, true);

With this, I receive a runtime error: 'style' is null or not an object.
So, the new #1 question is, how do I get the ValidatorEnable function to
work?

Still need expansion on the connection issue, per my last post.

Thanks,

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:1S**************@cpmsftngxa07.phx.gbl...
Hi Alphonse,

Thanks for your code. I am checking it and will update you with my findings.
Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #7
Hello Alphonse,

Thanks a lot for your information. I reviewed your code and description
carefully, and now I'd like to share the following information with you:

1. As you know, when we click any button in a Web form, the whole form will
be postback and need validating. That's the reason why we cannot submit the
form unless neiher of the textboxes is empty. In addition, please kindly
note that the RequiredFieldValidator in user control works properly. That
is, RequiredFieldValidator in WebUserControl1 will display error text only
if the textbox in WebUserControl1 is empty. It's the same to
RequiredFieldValidator in WebUserControl2.

As I understand, you need not to validate both user control. Please correct
me if there is any misunderstanding. To work around this problem, I suggest
that you can add a button in each user control to dynamically
enable/disable RequiredFieldValidator1. You can perform the following steps:

a. Set the RequiredFieldValidator1's property "Enable" default to false.
b. Add a button, and enable/disable the validator in its event handler:

if... then
RequiredFieldValidator1.Enabled = true
else
RequiredFieldValidator1.Enabled = false

2. >> when you say 'declare OleDbConnection as public member", am I correct
in assuming that you mean in the parent form? How then does one pass it to
(or reference it in) the user controls?

You should declare it in the parent form as well as the user controls. In
the Page_Load method, we can instantiate/initialize the OleDbConnection of
parent form and then assign it to the user controls.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #8
Thanks Tian,

Ok, the shared connection works, so that is out of the way.

I understand what you are saying about the validator, but what you are
suggesting (if I understand correctly) is server side and would require an
additional round trip.
Perhaps you missed my last post. I had found some documentation on
validation which states that I should be able to enable/disable validators
on the client side.

http://msdn.microsoft.com/library/de...pplusvalid.asp

According to it and the little other info I was able to find, the way to
accomplish what I want it to call ValidatorEnable on the client side to
enable/disable the validator.
As a test, I disabled the validator as the default and I am calling a
javascript function on page load which contains the following code:

strFocus="valrUserLoginF";
ValidatorEnable(strFocus, true);

With this, I receive a runtime error: 'style' is null or not an object.
valrUserLoginF is the declared id of the validator. I have also tried using
the clientid for it and get the same result.
So now,the question is, how do I get the ValidatorEnable function to work?

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:wX*************@cpmsftngxa07.phx.gbl...
Hello Alphonse,

Thanks a lot for your information. I reviewed your code and description
carefully, and now I'd like to share the following information with you:

1. As you know, when we click any button in a Web form, the whole form will be postback and need validating. That's the reason why we cannot submit the form unless neiher of the textboxes is empty. In addition, please kindly
note that the RequiredFieldValidator in user control works properly. That
is, RequiredFieldValidator in WebUserControl1 will display error text only
if the textbox in WebUserControl1 is empty. It's the same to
RequiredFieldValidator in WebUserControl2.

As I understand, you need not to validate both user control. Please correct me if there is any misunderstanding. To work around this problem, I suggest that you can add a button in each user control to dynamically
enable/disable RequiredFieldValidator1. You can perform the following steps:
a. Set the RequiredFieldValidator1's property "Enable" default to false.
b. Add a button, and enable/disable the validator in its event handler:

if... then
RequiredFieldValidator1.Enabled = true
else
RequiredFieldValidator1.Enabled = false

2. >> when you say 'declare OleDbConnection as public member", am I correct in assuming that you mean in the parent form? How then does one pass it to
(or reference it in) the user controls?

You should declare it in the parent form as well as the user controls. In
the Page_Load method, we can instantiate/initialize the OleDbConnection of
parent form and then assign it to the user controls.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #9
Hello Alphonse,

Thanks for your feedback. I noticed that you input a string instead of a
validator when calling ValidatorEnable. Would you please call it like the
follow and see if it works:

ValidatorEnable(MyUserControl.MyValidator, true);

I look forward to your result.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #10
Tian,

This works using the clientID of the validator.

Thanks for all your help!

--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:%2****************@cpmsftngxa07.phx.gbl...
Hello Alphonse,

Thanks for your feedback. I noticed that you input a string instead of a
validator when calling ValidatorEnable. Would you please call it like the
follow and see if it works:

ValidatorEnable(MyUserControl.MyValidator, true);

I look forward to your result.

Regards,

HuangTMIf I use ValidatorEnable(U_Users1.valrUserLoginF, true);
the error is: U_Users1 is undefined.

If I use
ValidatorEnable(valrUserLoginF, true);
the error is: valrUserLoginF is undefined.

If I use:
ValidatorEnable(document.Form1.U_Users1.valrUserLo ginF, true);
the error is: document.Form1.U_Users1.valrUserLoginF is null or not an
object

If I use:
ValidatorEnable(document.all(document.Form1.valrUs erLoginF), true);
There is no error, but the validator is not enabled.

If I use:
ValidatorEnable(document.all(document.Form1.U_User s1_dgrdUsers__ctl7_valrUse
rLoginF), true);
There is also no error, but the validator is not enabled.

Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Jul 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Robert Howells | last post: by
4 posts views Thread by Tim::.. | last post: by
2 posts views Thread by Tim::.. | last post: by
reply views Thread by Tim::.. | last post: by
3 posts views Thread by Tim::.. | last post: by
1 post views Thread by Demetri | last post: by
8 posts views Thread by mark.norgate | last post: by
3 posts views Thread by Terry Olsen | last post: by
1 post views Thread by zeya_bakhtyar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.