如何在VB.NET中捕获HTML输入框文本更改事件

huangapple go评论72阅读模式
英文:

how to capture html input box text change event in vb.net

问题

我需要在HTML输入框文本更改事件中在vb.net中填充下拉列表。以下是我的输入框。

<input ID="txtPositionDt" runat="server" class="style247" name="txtPositionDt" size="10" tabindex="0" type="text" autocomplete="off" />
英文:

i need to populate a dropdown in vb.net on my html inputbox text change event.
Here is my input box.

&lt;input ID=&quot;txtPositionDt&quot;  runat=&quot;server&quot; class=&quot;style247&quot; name=&quot;txtPositionDt&quot; 
                                                     size=&quot;10&quot; tabindex=&quot;0&quot; type=&quot;text&quot; autocomplete=&quot;off&quot; /&gt;

答案1

得分: 1

如果您希望在用户无需按下按钮或执行任何操作的情况下自动发生这种情况,那么您可以在网页上添加一些代码(JavaScript),这可以实现。

然而,不需要编写自定义代码的情况下,您可以让asp.net自动完成所有这些操作,而无需额外努力。

不要使用HTML输入或控件,而是使用asp.net内置的控件。这些控件不仅可以轻松拖放到Web表单中,而且它们的工作方式类似于桌面控件,所有的“自动”代码都会自动完成,无需额外努力。

与桌面下拉/组合框类似,基于Web的下拉框也具有两个值的功能(隐藏值和文本值)。

让我们尝试使用asp.net下拉框。

将一个拖放到Web表单上,使用属性窗格进行设置,或者使用标记。 (再次强调,您只需在属性窗格中输入值,然后在事件选项卡中进行操作)。

我们有以下标记:

所以到目前为止,这完全是拖放操作。

所以,我们最终得到了这个:

<asp:DropDownList ID="DropDownList1" runat="server"
    AutoPostBack="true"
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    DataValueField="ID"
    DataTextField="HotelName"
>
</asp:DropDownList>
<br />
<br />

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" 
    Height="100px"
    width="260px"
>
</asp:TextBox>

好的,现在是后端代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        LoadData

    End If

End Sub

Sub LoadData()
    ' 所有页面设置和数据加载都在这里完成
    DropDownList1.DataSource = MyRst("SELECT ID, HotelName FROM tblHotelsA 
                                      ORDER BY HotelName")

    DropDownList1.DataBind()
    DropDownList1.Items.Insert(0, "Select A Hotel")

End Sub

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim sBuf As String =
        $"Drop down Text = {DropDownList1.SelectedItem.Text} {vbCrLf}" &
        $"Drop down (Hotel ID) value = {DropDownList1.SelectedItem.Value}"

    TextBox1.Text = sBuf

End Sub

现在我们看到的结果是这样的:

如何在VB.NET中捕获HTML输入框文本更改事件

正如您所看到的,这不仅容易,而且您不需要在客户端浏览器中添加和编写一些JavaScript代码。所有移动部分都已自动完成。

因此,除非您有一个真正好的理由要使用HTML控件,如上所示,请使用asp.net控件进行此操作。

在上述示例中,针对任何通用的SQL查询,我创建了一个我在通用代码中反复使用的辅助程序。MyRst函数简单地返回一个数据表。

因此,为了完整起见,MyRst例程(在简单的代码模块中 - 因此对所有代码都是全局的)如下所示:

Public Function MyRst(strSQL As String) As DataTable

    Dim rstData As New DataTable
    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
            rstData.TableName = strSQL
        End Using
    End Using
    Return rstData
End Function

编辑:仅捕获文本框更改的代码。

让我们使用文本框进行相同的操作。

因此,我们像这样插入一个asp.net文本框:

<asp:TextBox ID="txtPositionDt" runat="server"
    AutoPostBack="true"
    OnTextChanged="txtPositionDt_TextChanged"
    autocomplete="off"
>
</asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="">

</asp:Label>

后端代码如下:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


End Sub


Protected Sub txtPositionDt_TextChanged(sender As Object, e As EventArgs)

    Label1.Text = "Value of text box = " & txtPositionDt.Text

End Sub

现在的结果是这样的:

如何在VB.NET中捕获HTML输入框文本更改事件

因此,不太清楚为什么您要同时使用文本框和下拉框。

您可以使用下拉列表(组合框),或者如上所示,您可以使用常规的asp.net文本框,并为用户在该文本框中输入的内容设置更改事件。

因此,您可能只需要一个文本框,或者您可能需要一个下拉(组合)框。不太清楚您希望使用哪种。

最后但并非最不重要的是?

这里的另一个可能目标是实现自动搜索或自动完成。当从数据库驱动时,这是一个“复杂”的任务和目标,但是ajaxtool-kit提供了一个可以在用户输入时自动完成的控件,我建议如果这是您的目标,那么使用该控件。

换句话说,一个组合框(下拉列表)应该足够替代文本框,但正如前面所提到的,您可能正在寻找一个由数据库驱动的“搜索/完成”功能,如果这是您的目标,我可以发布一个工作示例。

英文:

If you want this to occur automatically without the user having to press a button or do anything, then you could add some code to the web page (JavaScript), and this could/would/can be done.

However, in place of wiring up that custom code, you can get asp.net to do this all automatic for you, and without efforts.

Don't use an HTML input or control, but use one of the asp.net built-in controls. Those controls are not only a easy drag + drop into the webform, but they tend to work like desktop controls, and all of the "automatic" code behind is done automatic for you, and without efforts.

And, like a desktop dropdown/combo box, the web based ones also have provisions for 2 values (a hidden value, and a text value).

Let's try this with a asp.net dropdown.

Drag one on to the web form, setup using the property sheet, or use markup. (Again, it's really nice that you can just enter values in the property sheet, and then on the event tab).

We have this markup:

So really so far, this has been 100% drag and drop here.

So, we wind up with this:

&lt;asp:DropDownList ID=&quot;DropDownList1&quot; runat=&quot;server&quot;
    AutoPostBack=&quot;true&quot;
    OnSelectedIndexChanged=&quot;DropDownList1_SelectedIndexChanged&quot;
    DataValueField=&quot;ID&quot;
    DataTextField=&quot;HotelName&quot;
    &gt;
&lt;/asp:DropDownList&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; TextMode=&quot;MultiLine&quot; 
    Height=&quot;100px&quot;
    width=&quot;260px&quot;
    &gt;
&lt;/asp:TextBox&gt;

Ok, so now code behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        LoadData

    End If

End Sub

Sub LoadData()
    &#39; all page setup and loading of data goes here
    DropDownList1.DataSource = MyRst(&quot;SELECT ID, HotelName FROM tblHotelsA 
                                      ORDER BY HotelName&quot;)

    DropDownList1.DataBind()
    DropDownList1.Items.Insert(0, &quot;Select A Hotel&quot;)

End Sub

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Dim sBuf As String =
        $&quot;Drop down Text = {DropDownList1.SelectedItem.Text} {vbCrLf}&quot; &amp;
        $&quot;Drop down (Hotel ID) value = {DropDownList1.SelectedItem.Value}&quot;

    TextBox1.Text = sBuf

End Sub

And now we see/get this:

如何在VB.NET中捕获HTML输入框文本更改事件

As you can see, this is not only easy, but then you not forced to add and write some JavaScript code in the client side browser. All of the moving parts are done automatic for you.

So, unless you have a really great reason to use a html control, as the above shows, use a asp.net control for this.

In above, for any general SQL query, then I created a helper routine that I use over and over in my general code. The function MyRst simple returns a data table.

So, for completeness, the routine MyRst (in a simple code module - thus global to all code) is this:

Public Function MyRst(strSQL As String) As DataTable

    Dim rstData As New DataTable
    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
            rstData.TableName = strSQL
        End Using
    End Using
    Return rstData
End Function

Edit: Just code to capture text box change.

Let's do the same with a text box.

So, we drop in a asp.net text box like this:

        &lt;asp:TextBox ID=&quot;txtPositionDt&quot; runat=&quot;server&quot;
            AutoPostBack=&quot;true&quot;
            OnTextChanged=&quot;txtPositionDt_TextChanged&quot;
            autocomplete=&quot;off&quot;
            &gt;
        &lt;/asp:TextBox&gt;
        &lt;br /&gt;
        &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;&quot;&gt;

        &lt;/asp:Label&gt;

The code behind is this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


End Sub


Protected Sub txtPositionDt_TextChanged(sender As Object, e As EventArgs)

    Label1.Text = &quot;Value of text box = &quot; &amp; txtPositionDt.Text

End Sub

And the result is now this:

如何在VB.NET中捕获HTML输入框文本更改事件

So, it is not really clear why you want a text box and also a drop down.

You either use a dropdownlist (a combo box), or as above shows you can have a regular asp.net textbox, and have a change event for what the user types into that text box.

So, you might be wanting a text box only, or maybe you want a dropdown (combo) box. It not 100% clear as to which you want here.

Last but not least?

Another possible goal here is to have a auto-search, or auto-complete as you type. When driven from a database, this is a "complex" task and goal, but there is a auto-complete as you type control from the ajaxtool-kit, and I do suggest that control be used if that is your goal.

In other words, a combo box (dropdownlist) should suffice in place of a text box, but as noted, it is possible you are looking for a database driven "search/complete" as you type, and if that is your goal, then I can post a working example.

huangapple
  • 本文由 发表于 2023年6月18日 18:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定