[英]How can I change another page's dropdown in aspx?
我有一個名為contact.aspx的表單,並且有一個包含用戶列表的下拉列表。
我在下面添加行以添加用戶。
<a href="#" id="userLink" onclick="insertUser()" class="addnew" runat="server">New User</a>
而insertUser的javascript如下:
<script type="text/javascript">
function insertUser() {
var win = window.open('stackoverflow.aspx?t=1', 'User Insert', 'width=800,height=600');
}
</script>
當我單擊“ New User
”時,將打開stackoverflow.aspx,我想輸入新用戶數據並單擊“保存”。 單擊“保存”按鈕后,如何關閉stackoverflow.aspx並僅刷新 contact.aspx的下拉菜單 ?
不要在其他頁面上執行此操作,因為您無法從其他頁面更新字段。 您可以按時間間隔從contact.aspx發出ajax請求,還可以檢查數據庫是否有新用戶,然后刷新它。 但這不是一個好的解決方案。 因此,我建議在iframe內或div內的contact.aspx上進行此注冊(在stackoverflow.aspx上)。 您可以隱藏每個動作中的每個內容(注冊時,隱藏其余內容僅顯示注冊字段,如果完成刷新刷新下拉列表,重置注冊字段並隱藏它們,顯示下拉列表)。通過這種方法,您可以完成注冊后,刷新下拉列表。
要刷新dropdownlist,請進行ajax調用,您可以在db中獲取新值(不同用戶同時可以提交多個新值),然后cen refresh dropdown(這是HTML中帶有選項的選擇)但是,如果執行此操作,則可能會得到一個eventvalidation錯誤,該錯誤是asp.Net的內置功能(防御xss攻擊)。 因此,解決方案可以是注冊后進行完整的回發,並通過服務器代碼刷新值,或者不使用服務器控件到下拉列表,僅使用html選擇,並將其填寫在文檔中准備就緒,然后通過javascript或jQuery異步刷新。
嗯,在用戶單擊保存后在stackoverflow.aspx上,它將發布到服務器,創建一個新用戶並獲取該對象的JSON表示形式。 然后注冊腳本以關閉窗口並將json傳遞給打開器。
private btnSave_Click(object sender, EventArgs args)
{
//Save data
...
string objectJson = GetJSON(); // {"userId": 100, "name": "John Smith"}
ClientScriptManager cs = Page.ClientScript;
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> window.opener.appendObject(" + objectJson + ") </");
cstext1.Append("script>");
cs.RegisterStartupScript(this.GetType(), "RefreshScript", cstext1.ToString());
}
在contacts.aspx上具有以下腳本:
<script type="text/javascript">
function appendObject(json) {
var obj = JSON.parse(json); //convert json to object
//Add item to the drop down list.
var x = document.getElementById("mySelect");
var option = document.createElement("option");
option.text = obj.name;
try
{
// for IE earlier than version 8
x.add(option, x.options[null]);
}
catch (e)
{
x.add(option,null);
}
}
</script>
沒有測試,但我認為它可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.