如何解决我在使此javascript + asp.net正常工作时遇到问题
我有这个小项目,带来了一个小窗口,可以将“模态”窗口中的数据带到主窗口,但是我在上班时遇到了问题。
索引:
%@ 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,'');
}
很抱歉打扰大文本,但现在有点沮丧,谢谢!
解决方法
要达到的目标很难。我猜:
index.aspx具有一个用于加载弹出窗口的按钮(不是模式的)
该按钮将传入文本框txtIdRutPersona
Index.aspx
<input type="button" value="Abrir" onclick="Asistencia('<%= txtIdRutPersona.ClientID %>')" />
在ASP.NET生成其他ID的情况下使用客户端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"]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。