2017-03-18 54 views
0

我正在使用usercontrol在頁面左側顯示標籤文本,而另一個用戶控件顯示內容頁面,當內容頁面上的字段文本發生變化時,我需要在左側菜單usercontrol上的標籤上更新相同的狀態。當另一個用戶控件中的標籤文本發生更改時,更新另一個用戶控件中控件的文本

我用:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),"ChangeLabelText", script, false) 

更改文本左側的控制,與此得到執行,但標籤仍顯示即使在內容網頁上的文字後,更新舊的文本相關聯的JavaScript頁面刷新。但是,它會在整個頁面刷新時顯示新的更新文本。

請提出解決此問題的方法。

+1

的[從一種形式發送值爲另一種形式]可能的複製(http://stackoverflow.com/questions/1559770/send-values-from-one -form-to-another-form) –

+0

Mine是一個基於web的應用程序 – MukkuP

+0

也許你想添加一個[asp.net]標籤或其他東西 –

回答

0

如果您在頁面上有兩個用戶控件,並且希望從第一個控件的第二個控件的第二個控件中更新一個標籤,則需要使用FindControl

//find the other control in the parent 
Control control = Parent.FindControl("WebUserControl2") as Control; 

//find the label in that control 
Label label = control.FindControl("Label1") as Label; 

//update the label 
label.Text = "Updated from another control"; 

更新

如果你想用JavaScript,你只需要確保您發送正確的ID更新。

ScriptManager.RegisterStartupScript(Page, GetType(), "changeLabel", "changeLabel('WebUserControl2_Label1', 'Updated with JS')", true); 

而javscript功能

<script type="text/javascript"> 
    function changeLabel(id, text) { 
     document.getElementById(id).innerHTML = text; 
    } 
</script> 
+0

這將工作,如果頁面刷新,但在我的情況下,左側UC不會刷新,我認爲這需要通過javascript – MukkuP

+0

更新我的答案。 – VDWWD

+0

我已經採用了這種方法,但左側控件的值仍舊保留舊的值,即在我從服務器獲取新文本值之前JavaScript會被觸發,我需要在獲取新值後觸發javascript,有什麼建議嗎? – MukkuP

相關問題