【问题标题】:Auto populated text box values from data base来自数据库的自动填充文本框值
【发布时间】:2021-11-16 16:23:37
【问题描述】:

我有 4 个文本框。 我在第一个文本框中输入了一个值。 我希望接下来的 3 个文本框根据在第一个文本框中输入的值自动从数据库中填充值。 这可以在asp.net中完成吗?

【问题讨论】:

  • 是的,可以在asp.net中完成
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: javascript sql asp.net


【解决方案1】:

好的,所以我们有一个文本框来表示某种“id”,如下所示:

    <div style="text-align:right;width:300px">
        Enter Hotel ID :<asp:TextBox ID="askHotel" runat="server" AutoPostBack="true"></asp:TextBox> <br />
        <br />

        Hotel Name :<asp:TextBox ID="txtHotel" runat="server" Width="180px"></asp:TextBox> <br />
        City  :<asp:TextBox ID="txtCity" runat="server" Width="180px"></asp:TextBox> <br />
        Active :<asp:CheckBox ID="Active" runat="server" ></asp:CheckBox>

    </div>

我们的代码是:

Protected Sub askHotel_TextChanged(sender As Object, e As EventArgs) Handles askHotel.TextChanged

    Using cmdSQL As New SqlCommand("SELECT * FROM tblHotels WHERE ID = @ID",
                    New SqlConnection(My.Settings.TEST4))

        cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = askHotel.Text
        cmdSQL.Connection.Open()
        Dim rst As New DataTable
        rst.Load(cmdSQL.ExecuteReader)

        If rst.Rows.Count > 0 Then
            With rst.Rows(0)
                txtHotel.Text = .Item("HotelName")
                txtCity.Text = .Item("City")
                Active.Checked = .Item("Active")
            End With
        End If
    End Using

End Sub

输出:

【讨论】:

    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2011-12-14
    • 2012-03-04
    相关资源
    最近更新 更多