从复合 Web 控件内的控件回发
假设我们有一个带有组合框和文本框的复合 Web 控件。是否可以构建控制功能,以便当文本框中的文本更改时,它会发回并将值添加为组合框中的选项?
我知道我可以向文本框添加一个“onchange”处理程序并使用 Javascript 进行一些操作,但这并不是我真正想要做的。有没有办法
Protected Sub txt1_TextChanged(sender As Object, e As System.EventArgs) Handles txt1.TextChanged
combo1.items.add(txt1.Text)
End Sub
在 Web 控制代码中放入 like: 并将其连接到文本框的 TextChanged 事件?
Let's say we have a composite web control with a combobox and a textbox. Is it possible to build into the control functionality such that when the text in the textbox changes, it posts back and adds the value as an option in the combobox?
I know that I could add an "onchange" handler to the textbox and make something work with Javascript, but that's not really what I'm looking to do. Is there a way to just put like:
Protected Sub txt1_TextChanged(sender As Object, e As System.EventArgs) Handles txt1.TextChanged
combo1.items.add(txt1.Text)
End Sub
in the web control code and it connect to the TextChanged event of the textbox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,是的,您应该能够做到这一点。
我不知道 VB 需要什么语法,但我在 C# 中多次做过类似的事情。对于 C#,您可以将偶数处理程序的名称添加到文本框的标记中,并将文本框上的自动回发设置为 true。然后事件处理程序背后的代码会执行您需要的任何工作。
通常,我还在 Web 控件上定义自定义事件,并让文本框的事件处理程序也引发此自定义事件。这提供了让使用该控件的页面也对事件进行操作的选项。
编辑:
这是一个带有 DropDownList 的示例,它是在一组 Active Directory 域中查找用户的控件的一部分。如果用户更改了他们选择的域,我们希望它在新域上搜索之前输入的值。
标记:
代码隐藏:
或者在我通过代码动态添加子控件的情况下,我使用了以下语法:
正如我所说,我不确定如何使用
Handles
进行此操作VB的语法,但应该是可以的。In short yes, you should be able to do this.
I don't know what syntax you need for VB, but I have done similar things multiple times in C#. For C# you would add the name of the even handler to the markup of your text box, and set auto postback on the text box to true. Then the code behind event handler does what ever work you need it to.
As a rule I also define a custom event on the web control, and have the event handler for the textbox raise this custome event as well. This gives the option of letting the page that is using the control act on the event as well.
EDIT:
Here is an example with a DropDownList, it was part of a control to look up users within a set of Active Directory domains. If the user changed what domain they had selected we wanted it to search for the previously entered values on the new domain.
Mark-up:
Code behind:
Or in the case where I have added a child control dynamically through code I have used this syntax:
As I said, I am not sure how to make this work with the
Handles
syntax of VB but it should be possible.