来自Web服务的DropDownList绑定在asp.net中编辑时不传递数据源
Posted
技术标签:
【中文标题】来自Web服务的DropDownList绑定在asp.net中编辑时不传递数据源【英文标题】:DropDownList bind from web service Not passing data source when Edit in asp.net 【发布时间】:2018-12-13 13:04:10 【问题描述】:当我添加新数据下拉列表工作正常时,我正在使用 Web 服务绑定 DropDownList 中的数据,但是当我编辑模式时删除数据源未填充。所以问题是当我使用web服务绑定web服务时如何填充数据源。
html:
<asp:DropDownList ID="cmbFlightNo" class="form-control" runat="server" DataValueField="mFlightNo" DataTextField="mFlightNo" AppendDataBoundItems="true">
</asp:DropDownList>
网络服务
[WebMethod]
public Airline_Flights[] Loadetails(string StuID)
string Conec = ConfigurationManager.ConnectionStrings["BriskCargo"].ConnectionString;
SqlConnection con = new SqlConnection(Conec);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("Select FlightNo from AirLine_Flights where ALCode='" + StuID + "' and IsActive=1", con);
DataTable st = new DataTable();
da.Fill(st);
List<Airline_Flights> details = new List<Airline_Flights>();
foreach (DataRow dtrow in st.Rows)
Airline_Flights obj = new Airline_Flights();
obj.mFlightNo = dtrow["FlightNo"].ToString();
details.Add(obj);
javascriptSerializer ser = new JavaScriptSerializer();
return details.ToArray();
触发时更改时填充的JQuery代码。
function Load_Regno()
var StuID = document.getElementById('ContentPlaceHolder1_cmbAirlines').value;
$.ajax(
type: "POST",
contentType: "application/json;charset=utf-8",
url: "AirlinesDropDown.asmx/Loadetails",
data: JSON.stringify(StuID: StuID ),
dataType: "json",
success: function (data)
var theDropDown = document.getElementById("ContentPlaceHolder1_cmbFlightNo");
theDropDown.length = 0;
$.each(data.d, function (key, value)
$("#ContentPlaceHolder1_cmbFlightNo").append($("<option></option>").val(value.mFlightNo).html(value.mFlightNo));
);
,
error: function (XMLHttpRequest, textStatus, errorThrown)
if (XMLHttpRequest.status == 0)
alert(' Check Your Network.');
else if (XMLHttpRequest.status == 404)
alert('Requested URL not found.');
else if (XMLHttpRequest.status == 500)
alert('Internel Server Error.');
else
alert('Unknow Error.\n' + XMLHttpRequest.responseText);
);
return false;
【问题讨论】:
看来你不使用JavaScriptSerializer ser = new JavaScriptSerializer();
所以可能删除它。你签入调试器,return details.ToArray();
是否包含数据?对不起,基本问题,试图在这里找到失败点。当绑定服务器端时,Webforms 往往希望您使用服务器端方法、更新面板等。
【参考方案1】:
[WebMethod]
public Airline_Flights[] Loadetails(string StuID)
var flightsList = GetFlightListByStuID(string StuID);
return selectedFlights.Select(x => x.FlightNo).ToArray();
// could be in another data access class etc.
private List<Airline_Flights> GetFlightListByStuID(string StuID)
List<Airline_Flights> selectedFlights = new List<Airline_Flights>();
string connectionString = ConfigurationManager.ConnectionStrings["BriskCargo"].ConnectionString;
// SQL with parameter
string commandString = @"
SELECT FlightNo
FROM AirLine_Flights
WHERE ALCode = @StuID AND IsActive=1
";
// use IDisposable here so it closes and garbage collects automatically
using (SqlConnection connection = new SqlConnection(connectionString))
connection.Open();
using (SqlCommand command = new SqlCommand(commandString))
// check the length and type here...
command.Parameters.Add("@StuID",SqlDbType.NVChar, 25).Value = StuID;
command.CommandType = CommandType.Text;
using (var reader = command.ExecuteReader())
while (reader.Read())
flightsList.Add(new Airline_Flights FlightNo= reader["FlightNo"].ToString());
return selectedFlights;
JavaScript
function Load_Regno()
var StuID = document.getElementById('ContentPlaceHolder1_cmbAirlines').value;
$.ajax(
type: "POST",
contentType: "application/json",
url: "AirlinesDropDown.asmx/Loadetails",
data: StuID: StuID,
dataType: "json"
)
.done(function(data)
// verify data
alert(JSON.stringify(data));
// or
console.log(data);
var theDropDown = $("ContentPlaceHolder1_cmbFlightNo");
theDropDown.html(""); // clear old options
var options = [];
$.each(data.d, function(key, value)
var opt = ("<option></option>").val(value.FlightNo).html(value.FlightNo))
options.push(opt);
);
$.each(options,function(v)theDropDown.append(v););
)
.fail(function(XMLHttpRequest, textStatus, errorThrown)
if (XMLHttpRequest.status == 0)
alert('Check Your Network.');
else if (XMLHttpRequest.status == 404)
alert('Requested URL not found.');
else if (XMLHttpRequest.status == 500)
alert('Internal Server Error.');
else
alert('Unknown Error.\n' + XMLHttpRequest.responseText);
);
return false;
【讨论】:
以上是关于来自Web服务的DropDownList绑定在asp.net中编辑时不传递数据源的主要内容,如果未能解决你的问题,请参考以下文章
在 KendoGrid 中重新绑定 DropDownList,取决于在同一行的其他 DropDownList 中选择的值
如何在不使用ViewData的情况下将对象属性绑定到DropDownList中的数据源