如何解决ASP webforms:如何使用下拉菜单异步显示文本框中的相关数据?
我正在使用 SQL 服务器后端在 asp.net webforms 中开发应用程序。我有一个包含“机构名称”的下拉列表。当我从下拉列表中选择该机构时,它会使用地址、电话号码等相关数据填充其他 7 个只读文本框。当为下拉列表触发 SelectedIndexChanged 事件时,这当前通过使用 ADO 记录集起作用,但它还将页面发回并重新加载所有内容,这不是我想要的。
我知道 ASP 网络表单中有 ajax 控件,例如 updatepanel 和触发器,它们应该可以帮助我设置异步事件以在不重新加载页面的情况下提取此数据。我该如何实施?下拉列表是使用 aspx HTML 文件中的 SQL 数据源标记填充的。
这是代码隐藏文件中的当前代码:
Protected Sub cboRprtngAgncy_SelectedIndexChanged(sender As Object,e As System.EventArgs) Handles cboRprtngAgncy.SelectedIndexChanged
If cboRprtngAgncy.SelectedValue <> "" Then
'Fill in the agency details
Dim Cnn As SqlConnection
Dim Cmd As SqlCommand
Dim Rdr As SqlDataReader
Dim strRprtdPhn As String
Dim strAltPhn As String
'Set the objects and parameters up
Cnn = New SqlConnection
'Connection string deleted for security purposes.
Cmd = New SqlCommand
Cmd.CommandType = Data.CommandType.StoredProcedure
'Populate all the fields from the decedent table
Cmd.CommandText = "spAgncyDtls_Slct"
Cmd.Parameters.Add("@AgncyNmID",Data.SqlDbType.Int).Value = IIf(Trim(cboRprtngAgncy.SelectedValue) = "",System.DBNull.Value,Trim(cboRprtngAgncy.SelectedValue))
'Execute the objects and retrieve the decedent data
Cnn.Open()
Cmd.Connection = Cnn
Rdr = Cmd.ExecuteReader
Rdr.Read()
strRprtdPhn = Rdr("AgncyPhnMn").ToString
If strRprtdPhn <> "" Then strRprtdPhn = Left(strRprtdPhn,3) + "-" + Mid(strRprtdPhn,4,3) + "-" + Right(strRprtdPhn,4)
strAltPhn = Rdr("AgncyPhnAlt").ToString
If strAltPhn <> "" Then strAltPhn = Left(strAltPhn,3) + "-" + Mid(strAltPhn,3) + "-" + Right(strAltPhn,4)
txtRprtdPhnNmbr.Text = strRprtdPhn
txtRprtdAltPhn.Text = strAltPhn
txtRprtngAddrss.Text = Rdr("AgncyStrt").ToString
txtRprtngZp.Text = Rdr("AgncyZp").ToString
txtRprtngCty.Text = Rdr("AgncyCty").ToString
txtRprtngStt.Text = Rdr("AgncyStt").ToString
txtAgncyNmbr.Text = Rdr("AgncyFx").ToString
Rdr.Close()
If Cnn.State = Data.ConnectionState.Open Then Cnn.Close()
Cnn.Dispose()
Cmd.Dispose()
End If
End Sub
这里是 aspx HTMLfile 中的代码:
<label>Reporting Agency</label><br />
<asp:DropDownList ID="cboRprtngAgncy" runat="server" DataSourceID="sqldsAgncyNms" DataTextField="AgncyNm" DataValueField="AgncyNmID" Width="350px" Font-Names="Arial" Font-Size="16px" Font-Bold="True" TabIndex="25" AutoPostBack="True" Enabled="true" /><br />
<label>Agency #</label><br />
<asp:TextBox ID="txtAgncyNmbr" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="26" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" CssClass="border-field" /><br />
</div>
<div class="col-7">
<label>Reported By</label><br />
<asp:TextBox ID="txtRprtdBy" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="27" ReadOnly="False" BackColor="#ffffff" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#000000" /><br />
<label>Reported Phone</label><br />
<asp:TextBox ID="txtRprtdPhnNmbr" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="28" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" /><br />
<label>Alternate Phone</label><br />
<asp:TextBox ID="txtRprtdAltPhn" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="29" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" CssClass="border-field" /><br />
</div>
<br />
</div>
<br />
<div class="row">
<div class="col-7">
<label>Reporting Address</label><br />
<asp:TextBox ID="txtRprtngAddrss" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="500px" TabIndex="30" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" /><br />
<label>City</label><br />
<asp:TextBox ID="txtRprtngCty" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="31" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" /><br />
<label>State</label><br />
<asp:TextBox ID="txtRprtngStt" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="32" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" /><br />
<label>Zip Code</label><br />
<asp:TextBox ID="txtRprtngZp" runat="server" Font-Names="Arial" Font-Size="12px" Font-Bold="True" Width="150px" TabIndex="33" ReadOnly="true" BackColor="#E8EDEE" BorderStyle="Solid" BorderWidth="1px" BorderColor="#000000" ForeColor="#727372" />
</div>
我想我需要使用 javascript 来设置一个异步事件来获取数据并将其放入这 7 个文本框中。我对 Javascript 很熟悉,但还是新手,谁能告诉我如何设置?
提前致谢。
解决方法
我最终使用了三种不同的方法来运行异步事件:用于 ASP 网络表单的内置控件的 UpdatePanel、使用 API 的 javascript 事件以及使用页面代码隐藏文件中公开的方法的 javascript 事件。我将在不久的将来尝试为每种方法发布示例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。