用户控制 [英] User Controls

查看:63
本文介绍了用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个用户控件的Web表单(UC1和UC2)。

每个控件都有一个绑定数据网格,页脚中有文本框以添加新的

排。每个页脚中还有requiredfieldvalidator。

当我尝试向UC1添加一个新行时,UC2的requiredfieldvalidators会触发
因此页面不会发布。

如果我删除一个控件,一切正常。


我有2个问题。

1.我怎样才能获得每个控件的验证器控制只对他们的

特定控件起作用?


2.在两个控件之间共享oledb连接的最佳方式是什么,

所以每个人都没有打开新连接?


感谢您的帮助。


-


Alphonse Giambrone

电子邮件:a-giam at customdatasolutions dot us

解决方案

您好Alphonse,


感谢您的帖子。我现在与您分享以下信息:

1.如何让每个控件的验证器仅用于
的特定功能控制?


如您所知,RequiredFieldValidator应绑定到指定的

控件,其他控件不会影响它。我创建了一个样本并且

正常工作(不会重现您遇到的问题)。这是

我做了什么:


a。创建一个Web应用程序。


b。添加两个Web窗体,分别包含一个Edit框和一个RequiredFieldValidator

(正确设置ControlToValidate)。


c。根据以下MSDN文章将Web窗体页面转换为用户控件:
http://msdn.microsoft.com/library/de...us/vbcon/html/

vbwlkwalkthroughconvertingwebformtousercontrol.asp


d。添加一个包含两个用户控件的新Web窗体页面,并且

RequiredFieldValidators按预期工作。


请在您身边查看。如果问题仍然存在,您是否可以发布一个

可重现的项目?

2.在2



控制,所以每个人都没有打开新连接?


您可以将OleDbConnection声明为公共成员并将其传递给您的用户

控制。


如果您有任何问题或疑虑,请随时告诉我。


祝您有愉快的一天!


问候,


HuangTM

微软在线合作伙伴支持

MCSE / MCSD


安全! - www.microsoft.com/security

发布是按原样提供的。没有保证,也没有授予任何权利。


谢谢你回复田。

我按照你的建议建造了一个项目,除了我为每个

用户控件添加了一个提交按钮并遇到同样的问题。

这就是我所做的:

在一个新的VSN2002项目中

项目>添加Web用户控件

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 = QUOT;服务器" Text =" Submit 1">< / asp:Button>< / P>

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

codebehind:WebUserControl1.ascx.vb

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

公共MustInherit类WebUserControl1


继承System.Web.UI.UserControl


受保护的WithEvents TextBox1 As System.Web.UI.WebControls.TextBox


受保护的WithEvents RequiredFieldValidator1 As < br $>
System.Web.UI.WebControls.RequiredFieldValidator


受保护的事件Button1 As System.Web.UI.WebControls.Button


#Region" Web窗体设计器生成的代码


''Web窗体设计器需要此调用。


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


End Sub


Private Sub Page_Init(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Init


''CODEGEN:Web表单设计者需要此方法调用


' '不要使用代码编辑器修改它。


InitializeComponent()


End Sub


#End Region

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load


''用户代码初始化页面


结束子


结束班


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

创建第二个与第一个相同的控件(除了我将

提交按钮标记为提交2)并将其命名为WebUserControl2。

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

在默认的Webform1.aspx上,我添加了2个控件。

<%@ Page Language =" vb" AutoEventWireup = QUOT假QUOT; 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"方法= QUOT;交" runat =" server">

< P>

< uc1:WebUserControl1 id =" WebUserControl11"

runat =" ; server">< / uc1:WebUserControl1>< / P>

< P>

< uc1:WebUserControl2 id =" WebUserControl21"

runat =" server">< / uc1:WebUserControl2>< / P>

< / form>

< /正文>

< / HTML>

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

CodeBehind:

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

公共类WebForm1


继承System.Web.UI.Page


#Region " Web窗体设计器生成的代码


''Web窗体设计器需要此调用。


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


End Sub


Private Sub Page_Init(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Init


''CODEGEN:Web表单设计者需要此方法调用


' '不要使用代码编辑器修改它。


InitializeComponent()


End Sub


#End Region

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load


''用户代码初始化页面


结束子


结束班


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


当我在浏览器中查看表单时,我看到2个文本框和2个提交

按钮。

如果我在第一个文本框中输入并单击''Submit1'',

requiredfield验证器对于第二个文本框显示其消息并且

表单未提交。

提交表单的唯一方法是在两个文本框中输入。

我希望能够点击提交1并且只需要第一个

文本框来输入或点击提交2并且只需要第二个

有一个条目的文本框。

我想发生的事情是有道理的,因为渲染的html只包含

一个表格和验证者''表格需要验证才能提交



似乎应该有办法解决这个问题。


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

至于第2项,当你说''宣布OleDbConnection为公共成员,我是

在假设您的父表格中是正确的吗?

如何将其传递给(或引用它)用户控件?

-


Alphonse Giambrone

电子邮件:a-giam at customdatasolutions dot us

" Tian Min Huang" < TI ****** @ online.microsoft.com>在消息中写道

news:vN ************** @ cpmsftngxa07.phx.gbl ...

Hello Alphonse,

感谢您的帖子。我现在与您分享以下信息:

1.如何让每个控件的验证器仅用于其特定控件?

如您所知,RequiredFieldValidator应绑定到指定的
控件,其他控件不会影响它。我创建了一个样本,它正常工作(不会重现你面临的问题)。这是我做了什么:

创建一个Web应用程序。

b。添加两个Web窗体,分别包含一个Edit框和一个RequiredFieldValidator
(正确设置ControlToValidate)。

c。根据以下MSDN文章将Web窗体页面转换为用户控件:
http://msdn.microsoft.com/library/de...us/vbcon/html/ vbwlkwalkthroughconvertingwebformtousercontrol.asp

d。添加一个包含两个用户控件的新Web窗体页面,并且
RequiredFieldValidators按预期工作。

请在您身边查看。如果问题仍然存在,您是否可以发布一个可重现的项目?
2.在2


控件之间共享oledb连接的最佳方法是什么,所以每个人都没有打开新连接?

您可以将OleDbConnection声明为公共成员并将其传递给您的用户


请随时让我知道您是否有任何问题或疑虑。

祝您有愉快的一天!

问候,

黄TMM
Microsoft在线合作伙伴支持< br> MCSE / MCSD

获得安全! - www.microsoft.com/security
此帖子已提供按原样没有保证,也没有授予任何权利。



您好Alphonse,


感谢您的代码。我正在检查它,并会根据我的调查结果更新你。


祝你有愉快的一天!


问候,

HuangTM

微软在线合作伙伴支持

MCSE / MCSD


安全! - www.microsoft.com/security

发布是按原样提供的。没有保证,也没有赋予任何权利。


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

解决方案

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.


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.



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.


这篇关于用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆