473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3119
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
2
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
1
by: Demetri | last post by:
I'm trying to determine if we want to use panels or user controls for our pages. Our primary concern is performance, page loading and posting speed. To illustrate my question, lets use the...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
3
by: Terry Olsen | last post by:
I'm trying to add a domain user to a local group using the code below: Dim LCL As New DirectoryEntry("WinNT://" + Environment.MachineName + ",computer") Dim DOM As New...
1
by: zeya_bakhtyar | last post by:
Here is the page architecture: Page loads multiple user controls (including nested user controls) dynamically based on parameters provided into place holders. Note: The page only has the logic to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.