当前位置: 代码迷 >> ASP.NET >> 后台aspx.CS 中取得前台由 javascript脚本赋值的 TextBox值的有关问题
  详细解决方案

后台aspx.CS 中取得前台由 javascript脚本赋值的 TextBox值的有关问题

热度:8320   发布时间:2013-02-25 00:00:00.0
后台aspx.CS 中取得前台由 javascript脚本赋值的 TextBox值的问题
我由 Father.aspx 模态弹出子窗 Child.aspx ,然后,把由 父窗传过来的值,赋与 子窗中的一个 TextBox

,但是在子窗的 aspx.CS 中却取不到空上 TextBox 的值。
请看

Child.aspx

HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PopKeywords.aspx.cs" Inherits="WebUI.BusinessCenter.PopKeywords" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>关键字选择</title>    <link href="../css/css.css" rel="stylesheet" type="text/css" />    <base target="_self" /></head><body>    <form id="form1" runat="server">    <table border="0" cellpadding="0" cellspacing="0" align="center">        <tr>            <td height="40" align="center">              <asp:TextBox ID="txbChild" runat="server" Width="400px"></asp:TextBox>             </td>        </tr>    </table></form></body></html><script type="text/javascript" language="javascript"> <!--    var k = window.dialogArguments;    //获得父窗口传递来的值     if (k != null) {        document.getElementById("txbChild").value = k.document.getElementById("txbFather").value;    }    //--> </script> 





然后我在 Child.aspx.CS 后台中取 txbChild 的值

C# code
using System;using System.Data;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebUI.BusinessCenter{    public partial class PopKeywords : System.Web.UI.Page    protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                string getFatherValue = this.txbChild_getFather.Text.ToString().Trim();                //实际上,getFatherValue  取得值是空的            }        }}



我分析原因 是这样的:

因为后台是当一加载页面是,程序是由上往下执行,当 后台读到页面中 txbChild 时, javascript 还未给这个 TextBox 赋值,
因为 javascript 是在程序的最后执行的,就是说,程序结束的时,
才执行的这段 js

HTML code
<script type="text/javascript" language="javascript"> <!--    var k = window.dialogArguments;    //获得父窗口传递来的值     if (k != null) {        document.getElementById("txbChild").value = k.document.getElementById("txbFather").value;    }    //--> </script> 


但是这段 js 还只能放在程序最下面,如果放上面,又会导致其它问题,

目前这种情况,怎么才能在 aspx.cs 后台取到前台的这个 txbChild 的值呢 !?

请各位大侠帮忙 !不胜感激!





------解决方案--------------------------------------------------------
把你要传递的值放入url,而不是argument
然后
string getFatherValue = this.Request[key].toString();
------解决方案--------------------------------------------------------
方法有很多种,你要是在服务器端的到,可以这样
Father.aspx

HTML code
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <title></title>  <script type="text/javascript">    function openChild(txt) {      k = window.showModalDialog("Child1.aspx?txt=" + encodeURIComponent(txt.value));    }  </script></head><body>  <form id="form1" runat="server">  <table>    <tr>      <td height="30" bgcolor="#CAD2F4" align="left">        &nbsp;&nbsp;关键字选择      </td>      <td colspan="5" bgcolor="#FFFFFF" align="left">        &nbsp;&nbsp;        <asp:TextBox ID="txbFather" class="inputtext" runat="server" Width="430px" onclick="openChild(this)"></asp:TextBox>      </td>    </tr>  </table>  </form></body></html>
  相关解决方案