日期:2014-05-18  浏览次数:20557 次

使用MasterPage时,在JS里用方法取得Form名称(id)
这几天学习MasterPage,ContentPlaceHolderID

但是ContentPlaceHolderID的页面里是没有Form的,所以不知道怎么用js来取得Form的名称,特别是用window.showModalDialog弹出一个子窗时,怎么在弹出的这个子窗里用js取得父窗的Form名或父窗里某个控件的ID。

请高手好好指一下我,Tks!


------解决方案--------------------
用document.getElementById(就不需要form的id了
------解决方案--------------------
mark
------解决方案--------------------
同意慕白兄
------解决方案--------------------
1。
window.showModalDialog弹出一个子窗时,怎么在弹出的这个子窗里用js取得父窗的Form名或父窗里某个控件的ID。
=========
此问题, 似乎与是否使用 Master 没有直接关系吧

先解决本质问题

2。
Master 实际上是一个 UserControl,最终合并到 aspx 中,
Master 中的 form 最终成为 aspx 的 form
一个 aspx 上面只能由一个 form runat=server

因此,某个 aspx 不论是否应用了 master 均可以通过

HtmlForm frm = [[this.]Page.]Form;

的方式访问服务端的 form

当然假如你高兴,还可以

HtmlForm frm = [[this.]Page.]Master.Page.Form;

注:[] 表示可省略。

4。
// 关于 js 中动态访问服务器端控件 id
window.onload = function() {
alert( ' <% =Page.Form.ClientID %> .id ')
alert(document.getElementById( ' <% =Page.Form.ClientID %> .id ').name);
}

5。
注意,当前版本中(.net 2.0) master 中的 form 呈现出来的 ClientID 和 UniqueID 不会跟踪起 ID 来呈现,永远呈现为 aspnetForm 。
所以你,还是可以硬编码为 aspnetForm。
当然,不推荐!


6。
对于,问题 1
最简单的方式是传入 父窗口的 window 对象

// DEMO

// parent.aspx
<input type=text id=txtHello value=hello />

// js
var retVal = window.showModalDialog( "Child.aspx ", window, ....);


// child.aspx
// js
window.onload = function() {
var pWin = window.dialogArguments;
alert(pWin.document.forms[0]);
pWin.documnet.getElementById( "txtHello ").value += "world ";
}


Good Luck!
------解决方案--------------------
Form的名称
======
也许,你指的是 name 属性,但此属性在 xhtml 中已废弃
------解决方案--------------------
sorry,

pWin.documnet.getElementById( "txtHello ").value += "world ";

> > >

pWin.document.getElementById( "txtHello ").value += "world ";


对于访问其他的元素,标准的 DOM 方法是使用 document.getElementById(elementId);
------解决方案--------------------
我的代码看清楚没有?

父窗体得存在 <input type=text id=txtHello value=hello />

得到父窗体window对象之后,相关对象以及方法的使用与在本窗体中无两样
------解决方案--------------------
接分,楼上说的很清楚了