单击按钮时,我会调用 JavaScript 函数。获取值后,我需要根据在代码隐藏中获取的值执行一些操作。我应该如何调用代码隐藏?
我的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
}
调用函数的按钮设置如下:
<button class="doActionButton" id="btnSelectImage" runat="server" onclick="openWindow('../rcwksheet/popups/uploader.htm')">Select Image</button>
我想要的代码(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 调用代码隐藏?
- 我能否以某种方式使用按钮的“onclick”属性先转到 JavaScript,然后再转到代码隐藏?
- 触发 TxtInput.Value 的代码隐藏调用“onchange”?
最佳答案
是的,有办法。
首先,在TxtInput
中设置返回值后,您可以使用javascript提交表单。
function openWindow(page) {
var getval = window.showModalDialog(page);
document.getElementById("<%= TxtInput.ClientID %>").value = getval;
document.forms[0].submit();
}
然后在后面的代码中,您可以在页面加载事件中处理 TxtInput
的值。
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (this.Input.Value != string.Empty)
{
this.Input.Value += "blah";
}
}
}
关于javascript - 从 Javascript 调用代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10662592/