从 Javascript 调用代码隐藏

Calling Code-behind from Javascript(从 Javascript 调用代码隐藏)
本文介绍了从 Javascript 调用代码隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击一个按钮,我调用了一个 JavaScript 函数.获得值后,我需要从代码隐藏中获得的值中执行一些操作.我应该如何调用代码隐藏?

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
}

调用函数的按钮设置如下:

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

所以:

  1. 有没有办法从 JavaScript 调用代码隐藏?
  2. 我能否以某种方式使用按钮的onclick"属性先转到 JavaScript,然后再转到代码隐藏?
  3. 触发 TxtInput.Value 的代码隐藏调用onchange"?

推荐答案

是的,有办法.

首先,在TxtInput中设置好返回值后,可以使用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";
        }
    }
}

注意:您可能需要识别导致回发的控件

这篇关于从 Javascript 调用代码隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)