問題描述
點擊一個按鈕,我調(diào)用了一個 JavaScript 函數(shù).獲得值后,我需要從代碼隱藏中獲得的值中執(zhí)行一些操作.我應(yīng)該如何調(diào)用代碼隱藏?
On the click of a button, I call a JavaScript function. After getting the value, I need to perform some stuff from the value obtained in the code-behind. How should I call code-behind?
我的aspx:
function openWindow(page) {
var getval = window.showModalDialog(page);
document.getElementById("<%= TxtInput.ClientID %>").value = getval;
//After this I need to perform stuff 'Upload(TxtInput.value)' into database from the code-behind
}
調(diào)用函數(shù)的按鈕設(shè)置如下:
The button calling the function is set up in the following manner:
<button class="doActionButton" id="btnSelectImage" runat="server" onclick="openWindow('../rcwksheet/popups/uploader.htm')">Select Image</button>
我想要的代碼(VB):
My desired code behind (VB):
Public Sub btnSaveImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectImage.ServerClick
Dim inputFile As String = Me.TxtInput.Value
//do more stuff here
End Sub
所以:
- 有沒有辦法從 JavaScript 調(diào)用代碼隱藏?
- 我能否以某種方式使用按鈕的onclick"屬性先轉(zhuǎn)到 JavaScript,然后再轉(zhuǎn)到代碼隱藏?
- 觸發(fā) TxtInput.Value 的代碼隱藏調(diào)用onchange"?
推薦答案
是的,有辦法.
首先,在TxtInput
中設(shè)置好返回值后,可以使用javascript提交表單.
first, you can use javascript to submit the form after your return value is set in TxtInput
.
function openWindow(page) {
var getval = window.showModalDialog(page);
document.getElementById("<%= TxtInput.ClientID %>").value = getval;
document.forms[0].submit();
}
然后在你的代碼后面,你可以在頁面加載事件中處理 TxtInput
的值.
then in your code behind, you can handle TxtInput
's value in page load event.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (this.Input.Value != string.Empty)
{
this.Input.Value += "blah";
}
}
}
注意:您可能需要識別導(dǎo)致回發(fā)的控件
這篇關(guān)于從 Javascript 調(diào)用代碼隱藏的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!