服務器端功能,我想要做的步驟如下:通行證JavaScript數組與jQuery的
- 取複選框值的列表和使它們成爲在JavaScript字符串數組。
- 取出數組並將其發送到服務器端函數。
- 取數組服務器端並將其放入DataTable中。
- 取出DataTable並將其作爲TVP發送到存儲過程。
我已經從服務器端開始工作了。我遇到麻煩的地方是從JavaScript到服務器。
隨着我當前的代碼,我得到這個錯誤:
There are not enough fields in the Structured type. Structured types must have at least one field.
我如何通過JavaScript數組到一個Web方法?
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Services;
using System.Web.Services;
namespace SendTVP
{
public partial class _default : System.Web.UI.Page
{
//page variables
string filterList = string.Empty;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
using (var con = new SqlConnection(cs))
{
using (var cmd = new SqlCommand("spFilterPatientsByRace",con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter();
//required for passing a table valued parameter
param.SqlDbType = SqlDbType.Structured;
param.ParameterName = "@Races";
//adding the DataTable
param.Value = dt;
cmd.Parameters.Add(param);
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
}
}
[ScriptMethod]
[WebMethod]
//this method will take JS array as a parameter and turn it into a DataTable
public void TableFilter(string filterList)
{
DataTable filter = new DataTable();
filter.Columns.Add(new DataColumn() { ColumnName = "Races" });
dt.Columns.Add(new DataColumn() { ColumnName = "Races" });
foreach (string s in filterList.Split(','))
{
filter.Rows.Add(s);
}
dt = filter;
}
}
}
如果這是一個完整的愚蠢辦法做到這一點,修改非常歡迎:)