使用存储过程级联下拉列表
本文关键字:下拉列表 级联 存储过程 | 更新日期: 2023-09-27 17:56:43
我有这个aspx
代码,我对asmx.cs
代码感到非常困惑。
.ASPX:
<div class="col-md-6">
<asp:DropDownList ID="ddlMake" runat="server" CssClass="form-control"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlMake" TargetControlID="ddlMake" PromptText="Select Make" PromptValue="" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetMakes" runat="server" Category="MakeId" LoadingText="Loading Make..." />
</div>
<div class="col-md-6">
<asp:DropDownList ID="ddlModel" runat="server" CssClass="form-control"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cdlModel" TargetControlID="ddlModel" PromptText="Select Model" PromptValue="" ServicePath="CascadingDropdown1.cs.asmx" ServiceMethod="GetModels" runat="server" Category="ModelId" ParentControlID="ddlMake" LoadingText="Loading Model..." />
</div>
我有一个名为 Search_MAKE
和 Search_MODEL
的Stored Procedure
,模型 SP 需要参数。
有人可以帮我构建asmx.cs
代码吗?
更新:
[ScriptService]
public class CascadingDropdown1 : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
{
string query = "exec spIBCInventorySearch_MAKE @IDCustomer = 253433";
List<CascadingDropDownNameValue> Makes = GetData(query);
return Makes.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
{
string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
string query = string.Format("exec spIBCInventorySearch_MODEL @VehicleMake = {0}, @IDCustomer = 253433 ", make);
List<CascadingDropDownNameValue> Models = GetData(query);
return Models.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["UID=pal;Password=123;DATABASE=ATDBSQL;"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[1].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
}
我刚刚完成了asmx
代码,但它会在conString
部分中断。 有人可以帮我调试这个问题吗?
您正在尝试以奇怪的方式从配置文件中获取连接字符串。通常它是连接字符串部分中的键。例如:
<connectionStrings>
<add name="YourConnectionString" connectionString="Server=WIN-9A0EHHSEC8L'SQLEXPRESS2014;Database=SenseAPIClient;Integrated Security = true;" providerName="System.Data.SqlClient" />
要检索数据库连接字符串,我需要使用"您的连接字符串"。像下面这样:
var conString = ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
[ScriptService]
public class CascadingDropdown1 : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetMakes(string knownCategoryValues)
{
string query = "exec spIBCInventorySearch_MAKE @IDCustomer = 253433";
List<CascadingDropDownNameValue> Makes = GetData(query);
return Makes.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] GetModels(string knownCategoryValues)
{
string make = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["MakeId"];
string query = string.Format("exec spIBCInventorySearch_MODEL @VehicleMake = {0}, @IDCustomer = 253433 ", make);
List<CascadingDropDownNameValue> Models = GetData(query);
return Models.ToArray();
}
private List<CascadingDropDownNameValue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["UID=pal;Password=123;DATABASE=ATDBSQL;"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[0].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
我只是将value=reader[0].ToString()
中的索引从 1 更改为 0 :)
因为我在表中只有 1 列。