【问题标题】:I have problems getting this javascript + asp.net to work我无法让这个 javascript + asp.net 工作
【发布时间】:2020-08-21 16:19:45
【问题描述】:

我有这个小项目,它带来了一个小窗口,可以将数据从“模态”窗口带到主窗口,但我在投入工作时遇到了问题。

索引:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebTest.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
     <script type="text/javascript" src="Ventana.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:HiddenField ID="valor1" runat="server" />
            <asp:HiddenField ID="valor2" runat="server" />
            <asp:TextBox ID="txtIdRutPersona" runat="server"></asp:TextBox>
            <asp:Button ID="btnAbrirPopup" runat="server" Text="Abrir" />
        </div>
    </form>
</body>
</html>

索引的.cs部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTest
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnAbrirPopup.Attributes.Add("onclick", "javascript:Asistencia('txtIdRutPersona');");
            }
        }
    }
}

将数据绑定到索引的页面的“角色”窗口:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Persona.aspx.cs" Inherits="WebTest.Persona" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
        <script type="text/javascript" src="Ventana.js"></script>
        <script type="text/javascript">
            function cerrar()
            {
                self.close();
            }
        </script>
</head>
<body>
    <form id="Buscar" method="post" runat="server">
        <div>
            <asp:TextBox ID="txtRut" runat="server"></asp:TextBox>
            <asp:Button ID="btnOk" runat="server" Text="Aplicar" />
            <asp:Button ID="btnCerrar" runat="server" Text="Cerrar" />
        </div>
        
    </form>
</body>
</html>

“角色”的 .cs 部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTest
{
    public partial class Persona : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["form"] = Request.QueryString["formname"];
                ViewState["txtRut"] = Request.QueryString["txtRut"];
                ViewState["postBack"] = Request.QueryString["postBack"];

                btnOk.Attributes.Add("onClick", $"window.opener.SetCodigo({ViewState["form"]},{ViewState["txtRut"]},{ViewState["postBack"]})");
                btnCerrar.Attributes.Add("onClick", "cerrar()");

            }
        }
    }
}

和js:

var VentanaOrigen;

function Asistencia(txtRut) {

    popUp = window.open('Persona.aspx?formname=' + document.forms[0].name + '&txtRut=' + txtRut, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');

}

function SetCodigo(formulario, txtIdRutPersona, IdRutPersona, IPostback) {

    eval('var theform = document.' + formulario + ';');
    VentanaOrigen.close();
    theform.elements[txtIdRutPersona].value = IdRutPersona;

    if (IPostback)
        __doPostBack(txtIdRutPersona, '');

}

很抱歉打扰了大文本,但我现在有点沮丧,谢谢!

【问题讨论】:

  • 您有什么具体问题?你有错误吗?什么没有按预期运行?
  • 它不起作用,来自“角色”窗口的数据没有进入主窗口,并且错误是在属性中抛出。添加它会将它带到带有逗号的 hltm 并在前面空白和背部
  • 函数Asistencia 会将浏览器移动到您指定的URL - 正如您所说的“模态”,这让我感到困惑。这不是你做模态的方式。您还需要从函数中返回false,否则您会得到一个 POST。有许多在 Javascript 中执行模态的简单示例,例如:w3schools.com/howto/howto_css_modals.asp

标签: c# html asp.net


【解决方案1】:

你想要达到的目标很困难。我猜:

index.aspx 有一个按钮来加载弹出窗口(不是模态的) 按钮传入文本框的IDtxtIdRutPersona

Index.aspx

<input type="button" value="Abrir" onclick="Asistencia('<%= txtIdRutPersona.ClientID %>')" />

使用客户端 ID 以防 ASP.NET 生成不同的 ID。如果您更改 ID(在以后的日期),您也应该得到错误。

<script type="text/javascript">
    function Asistencia(txtIdRutPersonaID) {
         popUp = window.open('Persona.aspx?txtIdRutPersonaID=' + txtIdRutPersonaID, '', 'width=430,height=300,left=200,top=150,resizable=yes,status=yes,scrollbars=yes');
    }
</script>

persona.aspx

<asp:TextBox ID="txtRut" runat="server"></asp:TextBox>            
<input type="button" value="Aplicar" onclick="SetCodigo('<%= TxtIdRutPersonaID %>', '<%= txtRut.ClientID %>')" />
<input type="button" value="Cerrar" onclick="cerrar()" />   

您可以使用 window.opener 和 index.aspx 中的 ID (txtIdRutPersonaID) 将数据发送到 index.aspx

<script type="text/javascript">
    function SetCodigo(txtIdRutPersonaID, txtRutID) {
        if (window.opener != null && !window.opener.closed) {
            var txtIdRutPersona = window.opener.document.getElementById(txtIdRutPersonaID);
            // txtIdRutPersona is the textbox from index.aspx
            txtIdRutPersona.value = document.getElementById(txtRutID).value;
        }
        window.close();
    }

    function cerrar()
    {
        self.close();
    }
</script>

persona.aspx.cs

public string TxtIdRutPersonaID
{
    get
    {
        return Request.QueryString["txtIdRutPersonaID"];
    }
}

或者,您可以使用 ASP.NET 代码来生成 onclick 事件(但没有充分的理由)。例如:

btnOk.Attributes.Add("onclick", $"SetCodigo('{txtIdRutPersonaID}','{txtRut.ClientID}')");

同样,没有理由使用ViewState["form"]ViewState["txtRut"]ViewState["postBack"]

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    相关资源
    最近更新 更多