简体   繁体   English

asp.net vb用户控件在调用页面上引发一个事件

[英]asp.net vb user control raising an event on the calling page

i'm trying to learn about user controls. 我正在尝试了解用户控件。 I created a user control that has a textbox and a button. 我创建了一个具有文本框和按钮的用户控件。 What i'd like to be able to do is when i click the button in the user control, populate a label in the aspx page. 我想能够做的是当我单击用户控件中的按钮时,在aspx页面中填充标签。 I understand that i could just have a button on the page that uses some properties on the user control to get that information.. but i'd like to know how to do it with the button the user control.. the reason for this is the button is just an example.. a learning tool. 我知道我可以在页面上有一个按钮,它使用用户控件上的一些属性来获取该信息..但我想知道如何使用用户控件的按钮来实现它...原因是按钮只是一个例子..一个学习工具。 if i can get this working, i'd likely be putting thing in the user control that might require this kind of passing information. 如果我可以使这个工作,我可能会把东西放在用户控件中,可能需要这种传递信息。 Anyway.. i've found some c# examples that i tried to get working as vb.. but once started getting into the delegates and events.. well.. it made me turn to this post. 无论如何..我发现了一些c#例子,我试图像vb一样工作..但是一旦开始进入代表和事件......好吧..它让我转向这篇文章。

anyway.. hopefully someone can lend a hand. 无论如何..希望有人可以伸出援助之手。

See if this tutorial can help. 看看本教程是否有帮助。 The best way to do this is definitely to wire up an event, which isn't nearly as hard as you think. 这样做的最好方法绝对是连接一个事件,这并不像你想象的那么难。 The basics are: 基础是:

1. Define an event in your control class: 1.在控件类中定义一个事件:

Public Event MyEvent as System.EventHandler

2. Raise the event in a sub or function of your control class: 2.在控件类的子函数或函数中引发事件:

RaiseEvent MyEvent(Me, New EventArgs())

3. Handle the event on the page that contains your control: 3.在包含您的控件的页面上处理事件:

Protected Sub MyControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs) Handles MyControl.MyEvent
    'Do stuff here
End Sub

I believe what you would want to do is to have your UserControl raise an event. 我相信你想要做的就是让你的UserControl引发一个事件。 This MSDN article explains how to raise events and has VB examples. 这篇MSDN文章解释了如何引发事件并有VB示例。 Then your page can subscribe to the event that your UserControl raises and set whatever values you need to set. 然后,您的页面可以订阅UserControl引发的事件,并设置您需要设置的任何值。 It might be best to expose some of your UserControl fields (ie TextBoxes, etc.) as public properties so they're accessible from the subscribed control. 最好将一些UserControl字段(即TextBoxes等)公开为公共属性,以便可以从订阅的控件中访问它们。

The problem is that if you make the usercontrol dependent on it's parent page, it's no longer portable, and you've defeated the purpose of making it into a usercontrol in the first place. 问题是,如果你让usercontrol依赖于它的父页面,它就不再是可移植的,并且你已经打败了它首先成为用户控件的目的。

This is probably why you're running across delegates and whatnot in any examples - people are trying to remove the dependency on the parent page by allowing the usercontrol to call a passed-in method, instead of directly setting the label in the parent page. 这可能是您在代理中运行的原因以及在任何示例中的诸多原因 - 人们试图通过允许usercontrol调用传入方法来删除父页面上的依赖性,而不是直接在父页面中设置标签。

If this is just for practice, you'd be better off pressing a button on the parent page to set a label in the usercontrol, rather than the other way around. 如果这只是为了练习,你最好按下父页面上的按钮在usercontrol中设置标签,而不是相反。 The way you're doing it now is not a good design and should be avoided for real applications. 你现在这样做的方式并不是一个好的设计,应该避免在实际应用中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM