如何使用 jQuery 与数据库交互
我有一个烦人的问题。我在用 C# 编写的 Web 表单应用程序中使用 ASP.NET 提供的自动完成扩展程序。自动完成功能运行良好,没有问题。
我将自动完成功能与序列号文本字段一起使用。如果我想根据我选择的序列号填写产品名称文本字段,我需要与数据库交互。 这是因为向自动完成提供数据的Web 方法必须具有特定的签名,并且其输出参数是字符串数组。
因此,一旦选择了序列号,我就需要调用一个 jQuery 函数,该函数根据所选的序列号从数据库 (SQL Server) 获取相应的产品名称。
有什么建议吗?谢谢
更新:
Web 方法提供自动完成功能
[System.Web.Services.WebMethod]
public static string[] GetProductId(string prefixText, int count)
{
string selectSQL = "SELECT srnum FROM demo_product WHERE srnum LIKE'" + prefixText + "' + '%'";
DataTable dtProdId = dbUtil.dbGetDataTable("EMPLOYEE", selectSQL);
List<string> listProdId = new List<string>();
foreach (DataRow row in dtProdId.Rows)
{
listProdId.Add((string)row["SRNUM"]);
}
return listProdId.ToArray();
}
和 aspx 文件中的标记
<asp:TextBox runat="server" ID="txtSRNum" BackColor="#FFFF66" AutoComplete="On"></asp:TextBox>
<!-- Autocomplete extender for product serial number -->
<ajax:AutoCompleteExtender ID="txtSRNum_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath="" TargetControlID="txtSRNum"
MinimumPrefixLength="1" ServiceMethod="GetProductId" CompletionSetCount="5"
CompletionInterval="50" >
</ajax:AutoCompleteExtender>
I have an annoying problem. I use the autocomplete extender provided by ASP.NET in my Web Forms application written in C#. The autocomplete works good, no problem.
I use the autocomplete with the serial_number textfield. If I want to fill in the product_name textfield based on what serial_number I have chosen I need to interact with the database.
That is because the Web Method that provides data to the autocomplete must have a specific signature and its output parameters is an array of strings.
Therefore once the serial_number is selected, I need to call a jQuery function that given the selected serial_number gets the corresponging product_name from the database (SQL Server).
Any suggestion? Thanks
UPDATE:
The Web Method feeding the autocomplete
[System.Web.Services.WebMethod]
public static string[] GetProductId(string prefixText, int count)
{
string selectSQL = "SELECT srnum FROM demo_product WHERE srnum LIKE'" + prefixText + "' + '%'";
DataTable dtProdId = dbUtil.dbGetDataTable("EMPLOYEE", selectSQL);
List<string> listProdId = new List<string>();
foreach (DataRow row in dtProdId.Rows)
{
listProdId.Add((string)row["SRNUM"]);
}
return listProdId.ToArray();
}
And the markup in aspx file
<asp:TextBox runat="server" ID="txtSRNum" BackColor="#FFFF66" AutoComplete="On"></asp:TextBox>
<!-- Autocomplete extender for product serial number -->
<ajax:AutoCompleteExtender ID="txtSRNum_AutoCompleteExtender" runat="server"
DelimiterCharacters="" Enabled="True" ServicePath="" TargetControlID="txtSRNum"
MinimumPrefixLength="1" ServiceMethod="GetProductId" CompletionSetCount="5"
CompletionInterval="50" >
</ajax:AutoCompleteExtender>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你尝试了什么?根据您的服务器端配置,您的 jQuery 可能看起来像这样简单:
如果不了解具体情况,很难给出更具体的示例。
What have you tried? Depending on your server-side configuration, your jQuery could look as simple as this:
It's hard to give a more concrete example without knowing specifics.