如何根据选定的下拉值自动填充文本框2 [英] How do i autofill a textbox2 based on selected dropdown value

查看:81
本文介绍了如何根据选定的下拉值自动填充文本框2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个字段的表单:

名称,帐号,日期,里程,访问次数



当用户选择他们的下拉列表中的名称名称我想让他们的帐号在Employee sqldatabase的文档框帐号中自动填充,然后再提交信息的余额。



我该怎么做?



我尝试过这个javascript但我无法做到这一点。有人可以帮我修复这个脚本吗?

I have a form with multiple fields:
Name, account number, date, mileage, visits

When a user selects their name from the dropdown list "Name" I want to get their account number to populate automatically in the textbox "Account Number" from the Employee sqldatabase before they submit the balance of the information.

How would I do this?

I tried this javascript but I can't get it right. Can someone help me fix this script please?

function EnterNum() {
            var DropdownListName = document.getElementById('<%= DropDownListName.ClientID%>').Value;
            var TextBoxVendorNum = document.getElementById('<%=TextBoxVendorNum.ClientID%>').Value;
            if ((DropdownListName !== "") && (TextboxVendorNum !== "")) {
                var newValue = ("Select APVendorNumber FROM Employee WHERE ('DropDownList.SelectedValue')");
                document.getElementById('<%=TextBoxVendorNum.ClientID%>').Value = getSelection(newValue);
            }
            
            }





我尝试过: < br $> b $ b



What I have tried:

Protected Sub DropDownListName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownListName.SelectedIndexChanged
        If Me.DropDownListName.SelectedItem.Value <> "" Then
            Dim constr As String = ConfigurationManager.ConnectionStrings("Data Source=<server>; Initial Catalog=<database>;User ID=<user>;Password=<password>;").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("SELECT APVendorNumber FROM Employee WHERE EmployeeName = @EmployeeName", con)
                    Using da As New SqlDataAdapter(cmd)
                        cmd.Parameters.AddWithValue("@EmployeeName", Me.DropDownListName.SelectedItem.Value)
                        con.Open()
                        Dim VendorNumber As Object = cmd.ExecuteScalar()
                        con.Close()
                        Me.TextBoxVendorNum.Text = VendorNumber.ToString()
                    End Using
                End Using
            End Using
        Else
            Me.TextBoxVendorNum.Text = "Please Select Name from List"
        End If
    End Sub
End Class

推荐答案

当用户从名称框中选中时,使用模糊事件捕获



.blur()| jQuery API文档 [ ^ ]



或者您可以使用keyUp事件。当blur\keyup事件触发时,使用val获取名称文本框中的内容值



。val()| jQuery API文档 [ ^ ]



现在在您的网站上调用webservice\url,接受该名称作为参数并返回帐号(如果找到)。



jQuery.ajax()| jQuery API文档 [ ^ ]
Use the blur event to capture when the user tabs out of the name box

.blur() | jQuery API Documentation[^]

Or you could use the keyUp event. When the blur\keyup event fires get the value of what's in the name textbox using val

.val() | jQuery API Documentation[^]

Now call a webservice\url on your site that accepts the name as a param and returns the account number if one is found.

jQuery.ajax() | jQuery API Documentation[^]


发现这个并且它完美地工作。双击我的下拉列表并将其添加到我后面的代码中。



Found this and it worked perfectly. Doubled click on my dropdownlist and added this to my code behind.

' build the query with the product id as paramter
myCmd.CommandText = "SELECT product_name, product_title, product_desc, product_author FROM Product WHERE product_id = @product_id"
' add the parameter so the SqlCommand can build the final query
myCmd.Parameters.Add(New SqlParameter("@product_id", (DropDownList2.Text)))

' run the query and obtain a reader to get the results
Dim reader As SqlDataReader = myCmd.ExecuteReader()

' check if there are results
If (reader.Read()) Then
     ' populate the values of the controls
     txtProductName2.Text = reader(0)
     txtProductTitle2.Text = reader(1)
     txtProductDescription2.Text = reader(2)
     txtProductAuthor2.Text = reader(3)
End If


这篇关于如何根据选定的下拉值自动填充文本框2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆