问题
I have a dropdwnlist control in my parent page - ASP.NET C#, on click of button there is a popup opening to add new value to the control.
[+]
In PopUp page - Save button , I have the following code :
Response.Write("<script>opener.loadOptionLandlord('" + stid + "','" + strLandlorconn_dbame + "');</script>");
Response.Write("<script>window.close();</script>");
where loadOptionLandlord is a function in my parent page :
function loadOptionLandlord(val,txt)
{
var opt = document.createElement('<option value="'+ val +'">');
opt.innerText = txt;
var sCtrl = document.getElementById('<%= ddlLandlord.ClientID %>');
sCtrl.options[sCtrl.options.length] = new Option(txt, val, false, true);
}
The value is being saved to the database and the popup window is closing , but the newly added data is not refreshing in the dropdownlist. This is working for IE, but not for Chrome.
Please help ..
回答1:
Change your loadOptionLandlord
as below
function loadOptionLandlord(val,txt)
{
var opt = document.createElement("option");
var sCtrl = document.getElementById('<%= ddlLandlord.ClientID %>').options.add(opt);
opt.text = txt;
opt.value = val;
}
回答2:
can you check this function ?
function loadOptionLandlord(val,txt)
{
// Those create element not needed and its a wrong
// because you directly add option to select
//var opt = document.createElement('<option value="'+ val +'">');
// opt.innerText = txt;
var sCtrl = document.getElementById('<%= ddlLandlord.ClientID %>');
sCtrl.options[sCtrl.options.length] = new Option(txt, val, false, true);
}
here is sample js to add option on drop-down JS BIN
来源:https://stackoverflow.com/questions/26623479/refresh-parent-dropdownlist-from-popup-in-asp-net-c-sharp