从复合 Web 控件内的控件回发

发布于 2024-12-25 23:17:40 字数 372 浏览 1 评论 0原文

假设我们有一个带有组合框和文本框的复合 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-01 23:17:41

简而言之,是的,您应该能够做到这一点。

我不知道 VB 需要什么语法,但我在 C# 中多次做过类似的事情。对于 C#,您可以将偶数处理程序的名称添加到文本框的标记中,并将文本框上的自动回发设置为 true。然后事件处理程序背后的代码会执行您需要的任何工作。

通常,我还在 Web 控件上定义自定义事件,并让文本框的事件处理程序也引发此自定义事件。这提供了让使用该控件的页面也对事件进行操作的选项。

编辑:

这是一个带有 DropDownList 的示例,它是在一组 Active Directory 域中查找用户的控件的一部分。如果用户更改了他们选择的域,我们希望它在新域上搜索之前输入的值。

标记:

<asp:DropDownList ID="ddl_Domain" runat="server" onselectedindexchanged="ddl_Domain_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>

代码隐藏:

protected void ddl_Domain_SelectedIndexChanged(object sender, EventArgs e)
{
    if (UserID != "" || LastName != "" || FirstName != "" || EmailAddress != "")
    {
        lnk_Find_Click(sender, e);
    }
}

或者在我通过代码动态添加子控件的情况下,我使用了以下语法:

DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.SelectedIndexChanged += This_SelectedValue_Changed;
ddl.AutoPostBack = true; 

正如我所说,我不确定如何使用 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:

<asp:DropDownList ID="ddl_Domain" runat="server" onselectedindexchanged="ddl_Domain_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>

Code behind:

protected void ddl_Domain_SelectedIndexChanged(object sender, EventArgs e)
{
    if (UserID != "" || LastName != "" || FirstName != "" || EmailAddress != "")
    {
        lnk_Find_Click(sender, e);
    }
}

Or in the case where I have added a child control dynamically through code I have used this syntax:

DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
ddl.DataTextField = "Text";
ddl.DataValueField = "Value";
ddl.SelectedIndexChanged += This_SelectedValue_Changed;
ddl.AutoPostBack = true; 

As I said, I am not sure how to make this work with the Handles syntax of VB but it should be possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文