以下是我的Html代码:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="BranchRegistration.aspx.cs" Inherits="BloodBank.BranchRegistration" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="settings_pagecontent">
<div class="addform">
<asp:Button ID="BtnAdd" runat="server" CssClass="button" Text="Add" />
<br />
<br />
</div>
<div class="form">
<div class="settings_border-line">
<div class="settings_label_left">
<asp:Label ID="lblbranchname" class="settings_label_inv" runat="server" Text="Branch Name"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchphonenumber" class="settings_label_inv" runat="server" Text="Phone Number"></asp:Label>
<br />
<br />
</div>
<div class="text_right">
<asp:TextBox ID="txtbranchname" class="settings_textbox_body Branch_txt" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="txtbranchphonenumber" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchemailid" class="settings_label_inv" runat="server" Text="Email ID"></asp:Label>
<asp:TextBox ID="txtbranchemailid" class="settings_textbox_body" runat="server"></asp:TextBox>
</div>
<div class="clear"></div>
</div>
<br />
<br />
<div class="border-line">
<div class="label_left" style="width: 150px">
<asp:Label ID="lblbranchaddress" class="settings_label_inv" runat="server" Text="Branch Address"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchlandmark" class="settings_label_inv" runat="server" Text="Landmark"></asp:Label>
<br />
<br />
<asp:Label ID="lblTehsil" class="settings_label_inv" runat="server" Text="Tehsil"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchstate" class="settings_label_inv" runat="server" Text="State"></asp:Label>
<br />
<br />
<asp:Label ID="lbllocationmap" class="settings_label_inv" runat="server" Text="Location"></asp:Label>
<br />
<br />
</div>
<div class="text_right" style="width: 670px">
<asp:TextBox ID="txtbranchaddress" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchstreet" class="settings_label_inv" runat="server" Text="Street"></asp:Label>
<asp:TextBox ID="txtbranchstreet" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtbranchlandmark" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchlocality" class="settings_label_inv" runat="server" Text="Locality"></asp:Label>
<asp:TextBox ID="txtbranchlocality" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txttehsil" runat="server" class="settings_textbox_body"></asp:TextBox>
<asp:Label ID="lblDistrict" class="settings_label_inv" runat="server" Text="District"></asp:Label>
<asp:TextBox ID="txtDistrict" runat="server" class="settings_textbox_body"></asp:TextBox>
<br />
<br />
<asp:DropDownList ID="ddlState" runat="server" class="settings_textbox_body"></asp:DropDownList>
<asp:Label ID="lblbranchpincode" class="settings_label_inv" runat="server" Text="Pincode"></asp:Label>
<asp:TextBox ID="txtbranchpincode" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtbranchlocationmap" class="settings_locationmap" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="invSubmit" runat="server" Text="Submit" />
<asp:Button ID="invReset" runat="server" Text="Reset" />
</div>
<div class="clear"></div>
</div>
</div>
<div class="grid">
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('<%=txttehsil.ClientID%>').autocomplete({
source: function (request, response) {
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BranchRegistration.aspx/GetNames",
data: "{'name':'" + $('<%=txttehsil.ClientID%>').val() + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (err) {
alert("Error: " + err);
}
});
}, minLength: 1
});
});
</script>
<link href="Content/site1.css" rel="stylesheet" />
<link href="Content/Site.css" rel="stylesheet" />
</asp:Content>
服务器端代码如下:
[System.Web.Services.WebMethod]
public static List<string> GetNames(string name)
{
List<string> nameList = new List<string>();
string strSqlQuery = "Select * from SubRegions where SubRegionName like '%" + name + "%'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, Common.GetConnectionString());
DataSet ds = new DataSet(); da.Fill(ds, "SubRegions");
DataTable dt = ds.Tables["SubRegions"];
DataRowCollection drc = dt.Rows;
foreach (DataRow dr in drc)
{
nameList.Add(dr["SubRegionName"].ToString());
}
return nameList;
}
此代码在没有母版页的情况下运行,但不在我的应用程序中运行。请给我正确的答案。在此页面中autocomplete
使用了文本框。它是jquery ajax application
。我在另一个项目中使用这个代码,它正确执行并给了我正确的结果。
答案 0 :(得分:0)
在服务器代码中添加“System.Web.Script.Services.ScriptMethodAttribute()”,问题就解决了。
您的服务器代码如下所示
[System.Web.Services.WebMethod ,System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetNames(string name)
{
List<string> nameList = new List<string>();
string strSqlQuery = "Select * from SubRegions where SubRegionName like '%" + name + "%'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, Common.GetConnectionString());
DataSet ds = new DataSet(); da.Fill(ds, "SubRegions");
DataTable dt = ds.Tables["SubRegions"];
DataRowCollection drc = dt.Rows;
foreach (DataRow dr in drc)
{
nameList.Add(dr["SubRegionName"].ToString());
}
return nameList;
}