string jsstr="<script> var ret = window.showModalDialog('EditDG_Main.aspx?id="+id+"',window,'status=no;center=yes;help=no;dialogWidth=780px;dialogHeight=580px');</script>"; this.Response.Write(jsstr); 上端代码是在服务器端asp.net按钮下运行,请问document.getElementById( "Button2" ).click()怎么放?而且这个Button2我要求是asp按钮?
你说的asp按钮是指HtmlControl中的HtmlButton吧 这个没有问题的 不管是HtmlButton还是WebControls中的Button 最后在客户端显示时都是<input type=Button...>的形式 所以这个不用担心 你只需要验证这里的Button2是你的页面在执行时看到的Html源代码中 要操作的按钮的名称即可你这段代码改成: string jsstr="<script> var ret = window.showModalDialog('EditDG_Main.aspx?id="+id+"',window,'status=no;center=yes;help=no;dialogWidth=780px;dialogHeight=580px');document.getElementById( "Button2" ).click();</script>"; this.Response.Write(jsstr); 应该就可以了 你先试试看
Response.Expires = 0;
Response.CacheControl = "no-cache";
问题二觉得是你的id赋值有问题,
你提交弹了新窗口,<input type=button value="click" onclick="window.dialogArguments.document.getElementById('Button2').click()">
肯定不行吧?得先解决第一个问题才行。
二、你可以将该值保存在ViewState中,而不用保存成静态变量;
<input type=button value="click" onclick="window.dialogArguments.document.getElementById('Button2').click()">这里的Button2应该是你需要调用的父页面中的按钮名
你检查一下,按钮是否命名相同
都说找不到对象.
把代码修改成
<input type=button value="click" onclick="alert( window.dialogArguments );"><input type=button value="click" onclick="alert( window.dialogArguments.document );"><input type=button value="click" onclick="alert( window.dialogArguments.document.getElementById('Button2') );">这样逐步判断是哪一个对象没有找到实例
window.dialogArguments.document: [object]
window.dialogArguments.document.getElementById('Button2'): null
如何解释?
把命名方式修改为id=""或者使用
window.dialogArguments.document.all['Button2']来取值
试试看如果不行,你就试试在这里捕获父窗体中某个表单控件的值
看能否捕获
比如
alert( document.getElementById('Button2') );
如果在父窗体中这样写代码都不能捕获的话
你先试试吧
比如
alert( document.getElementById('Button2') );================>>>>> 为[object]
有个简单的方法触发
比如你这里要触发一个Button.Click事件
可以把这段代码写在windos.ShowModulDialog这个方法后
然后写一句
document.getElementById( "Button2" ).click()的脚本
就可以了
当模式窗体关闭后
这个函数才完成
按照脚本的执行方法
接下来的脚本代码才会继续执行
this.Response.Write(jsstr);
上端代码是在服务器端asp.net按钮下运行,请问document.getElementById( "Button2" ).click()怎么放?而且这个Button2我要求是asp按钮?
这个没有问题的
不管是HtmlButton还是WebControls中的Button
最后在客户端显示时都是<input type=Button...>的形式
所以这个不用担心
你只需要验证这里的Button2是你的页面在执行时看到的Html源代码中
要操作的按钮的名称即可你这段代码改成:
string jsstr="<script> var ret = window.showModalDialog('EditDG_Main.aspx?id="+id+"',window,'status=no;center=yes;help=no;dialogWidth=780px;dialogHeight=580px');document.getElementById( "Button2" ).click();</script>";
this.Response.Write(jsstr);
应该就可以了
你先试试看
{
Response.Write("<script>alert('你必须先选择要修改的行号');</script>");
Response.Write("<script>parent.frames.main.document.location.href='menu_left.aspx';</script>");
}
这是Button2的事件,单独单击这按钮是有结果的.
比如
在页面中加入
<script language='javascript'>
function Test()
{
alert('你必须先选择要修改的行号');
parent.frames.main.document.location.href='menu_left.aspx';
}
</script>在.cs文件的page_Load方法中加入
Button2.Attributes.Add( "onclick","Test()" );
在onclick中showModelDialog showModel出来的要加上:<base target="_self">和清除缓存,
然后更新数据后,得到showModelDialog的返回值,可以根据返回值确定返回true或者false,如果是true自动刷新,因为这个button是服务器端的。
cs中
btnAdd..Attributes.Add("OnClick","return btnAddFunc();");
js中
function btnAddFunc()
{
var str = window.showModalDialog("aa.aspx?OP=add","","");
if (typeof(str) != "undefined")
{
if (str=="0") //不需要更新
return false;
}
return true;//return true刷新本页面,也可以写js刷新url 比如document.all.href=.... 使用js刷新要return false;
} 2.showModel的页面
<base target="_self">
<%
Response.Buffer=true;
Response.ExpiresAbsolute=System.DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl="no-cache";
%>
页面有两个按钮,确定和取消,确定做填加操作,取消关闭并返回是否要更新
//关闭
function buttonClose()
{
window.returnValue = document.all('hiddenChanged').value ;//用于标记是否需要刷新
window.close();
return false;
} 不知道这样能否达到你的要求
1.如果传递的是显示的已经有的数据 比如你的id就是第一列
从客户端得到的DataGrid其实是一个表格 从表格中的rows[i].cells(n).innerText得到
2.如果数据在DataSet或者DataTable中,而不显示
我的做法是,每行第0列作为索引列,在ItemDataBind的时候,把id值写到第0列,作为hidden隐藏起来,这样客户端可以拿到了。
另外也可以把id的值作为一个属性 给TD或者什么的,反正就是放在表格行的某个地方。
或者干脆把id,循环分割放到hidden,客户端得到后根据行得到id信息。
webdiyer(陕北吴旗娃)的办法我试验了,可以防止提交生成新窗口,不过如果打开的模态窗口需要接受QueryString就麻烦了,有时候会出现找不到查询字符串的错误。
而在<head></head>之间加入<base target="_self">我也试验了,不能防止提交生成新窗口。我也不知道怎么回事。
我的解决办法:
在模态对话框的.aspx文件的<head></head>加入js代码
<script language="javascript" type="text/javascript">
function onsubmit()
{
document.Form1.target="_self";
}
</script>上面的方法在IE6.0SP1中试验成功,而且通过这种办法,showmodeldialog()方法可以直接打开.aspx文件。
使用上面的方法注意,上面的js代码直接硬编码表单的ID,因为vs.net生成的.aspx网页的ID都是Form1,所以上面的方法必须手工指定.aspx的表单ID编号。
父窗口打开对话框代码
<script language="javascript">
function OpenDialog()
{
var url;
url ="...."; // 打开的模态对话框内.aspx文件地址,可以加查询字符串参数
var me;
me = window; // 定义引用父页面的窗体对象,准备传递
window.showModalDialog(url,me,"[末态对话框的宽高度样式定义]");
}
</script>模态对话框打开的.aspx文件刷新父页面:
<script language="javascript">
funcation Flush()
{
if(dialogArguments != null)
{
dialogArguments.location.reload(true);
}
}
</script>
<%
Response.Buffer=true;
Response.ExpiresAbsolute=System.DateTime.Now.AddSeconds(-1);
Response.Expires=0;
Response.CacheControl="no-cache";
%>
从客户端得到的DataGrid其实是一个表格 从表格中的rows[i].cells(n).innerText得到
==================》》》》》》》》》》》
请写出在javascrit客户端获取mydatagrid的当前选择行第一个单元格的文本。 TomMax(笑望人生)
===========>>>>>>>>>>>>>
一样的道理,如何获取mydatagrid的当前选择行第一个单元格的文本。
-----------------------------------------
<%@Import Namespace="System.Data"%>
<%@Page debug=true%>
<script runat="server" Language="C#">
void Page_Load(object src,EventArgs e){
if(!Page.IsPostBack){
BindData();
}
}void BindData(){
DataTable table=(DataTable)Cache["TestTable"];
if(table==null){
table=new DataTable();
DataColumn column=new DataColumn("序号",typeof(int));
column.AutoIncrement=true;
column.AutoIncrementSeed=1;
column.Unique=true;
table.Columns.Add(column);
table.Columns.Add(new DataColumn("姓名",typeof(string)));
table.Columns.Add(new DataColumn("住址",typeof(string)));
table.Columns.Add(new DataColumn("年龄",typeof(int)));
DataRow row=table.NewRow();
row["姓名"]="张三";
row["住址"]="陕西省吴旗县";
row["年龄"]=22;
table.Rows.Add(row);
row=table.NewRow();
row["姓名"]="李四";
row["住址"]="陕西省延安市";
row["年龄"]=26;
table.Rows.Add(row);
table.PrimaryKey=new DataColumn[]{column};
Cache["TestTable"]=table;
}
grid1.DataSource=table;
grid1.DataBind();
//Cache.Remove("TestTable");
}void BindScript(object src,DataGridItemEventArgs e){
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem){
e.Item.Attributes.Add("style","cursor:hand");
e.Item.Attributes.Add("ondblclick","AddOrEditUser("+grid1.DataKeys[e.Item.ItemIndex].ToString()+")");
}
}
</script>
<HTML>
<HEAD>
<TITLE> Webdiyer 制造:) (www.webdiyer.com) </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer">
</HEAD><BODY>
<form runat="server">
<table width="760" align="center" border=0 cellspacing=0><tr><td>
<asp:datagrid id="grid1" runat="server" Width="100%" HorizontalAlign="Center" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal" OnItemDataBound="BindScript" DataKeyField="序号">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
</td></tr>
<tr><td align="right"><a href="#" onclick="AddOrEditUser(null)">添加新用户</a>
</td></tr>
<tr><td>在DataGrid任意行上双击可进行编辑</td></tr></table>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function AddOrEditUser(userID){
var argument;
if(userID!=null)
argument="AddOrEditUser.aspx?type=edit&id="+userID;
else
argument="AddOrEditUser.aspx";
var retValue=window.showModalDialog("frame.htm",argument,"dialogWidth=400px;dialogHeight=160px;center=yes;status=no");
if(retValue==1)
top.location.href=top.location.href;
}
//--></SCRIPT></BODY></HTML>
-------------------------------------<%@Import Namespace="System.Data"%>
<%@Page debug=true%>
<script runat="server" Language="C#">
DataTable table;
void Page_Load(object src,EventArgs e){
if(!Page.IsPostBack){
string type=Request.QueryString["type"];
string idStr=Request.QueryString["id"];
if(type=="edit"){
if(idStr!=null&&idStr.Trim().Length>0)
{
int id=int.Parse(idStr);
table=(DataTable)Cache["TestTable"];
DataRow row=table.Rows.Find(id);
if(row!=null){
namebox.Text=(string)row["姓名"];
addrbox.Text=(string)row["住址"];
agebox.Text=row["年龄"].ToString();
ViewState["Action"]="Edit";
ViewState["UserID"]=id;
titlelbl.Text="修改用户信息";
button1.Text="更新(U)";
button1.AccessKey="U";
}
}
}
else{
titlelbl.Text="添加新用户";
button1.Text="添加(A)";
button1.AccessKey="A";
}
}
}void AddNewUser(object src,EventArgs e){
table=(DataTable)Cache["TestTable"];
if(table==null){
table=new DataTable();
DataColumn column=new DataColumn("序号",typeof(int));
column.AutoIncrement=true;
column.AutoIncrementSeed=1;
column.Unique=true;
table.Columns.Add(column);
table.Columns.Add(new DataColumn("姓名",typeof(string)));
table.Columns.Add(new DataColumn("住址",typeof(string)));
table.Columns.Add(new DataColumn("年龄",typeof(int)));
}
if((string)ViewState["Action"]=="Edit"){
foreach(DataRow row in table.Rows){
if((int)row["序号"]==(int)ViewState["UserID"]){
row["姓名"]=namebox.Text.Trim();
row["住址"]=addrbox.Text.Trim();
row["年龄"]=int.Parse(agebox.Text);
Cache["TestTable"]=table;
break;
}
}
}
else{
DataRow row=table.NewRow();
row["姓名"]=namebox.Text.Trim();
row["住址"]=addrbox.Text.Trim();
row["年龄"]=int.Parse(agebox.Text);
table.Rows.Add(row);
Cache["TestTable"]=table;
}
foreach(DataRow row in table.Rows){
Response.Write(row["姓名"]);
}
Response.Write("<script language=\'Javascript\'>window.returnValue=1;top.close();<");
Response.Write("/");
Response.Write("script>");
Response.End();
}
</script>
<HTML>
<HEAD>
<TITLE> Webdiyer 制造:) (www.webdiyer.com) </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer">
</HEAD><BODY topmargin=1 leftmargin=1 scroll=no>
<form runat="server">
<table width="100%" border=1 cellspacing=0 style="border-collapse:collapse" bgcolor="snow">
<tr><td colspan=2 bgcolor=#9999FF><asp:Label runat="server" forecolor="red" font-bold=true id="titlelbl"/></td></tr>
<tr><td width="20%">姓名</td>
<td width="80%"><asp:TextBox runat="server" id="namebox"/><asp:RequiredFieldValidator runat="server" Text="请输入" ControlToValidate="namebox" Display="dynamic"/></td></tr>
<tr><td>住址:</td>
<td><asp:TextBox runat="server" id="addrbox"/><asp:RequiredFieldValidator runat="server" Text="请输入" ControlToValidate="addrbox" Display="dynamic"/></td></tr>
<tr><td>年龄:</td>
<td><asp:TextBox runat="server" id="agebox"/><asp:RequiredFieldValidator runat="server" Text="请输入" ControlToValidate="agebox" Display="dynamic"/>
<asp:CompareValidator runat="server" Operator="DataTypeCheck" ControlToValidate="agebox" Type="integer" Display="dynamic" Text="非数字"/></td></tr>
<tr><td colspan=2 align="center">
<asp:Button Text="添加(A)" AccessKey="A" onclick="AddNewUser" runat="server" id="button1"/>
<button onclick="top.close()" accesskey="C">取消(C)</button>
</table>
</form>
</BODY>
</HTML>
---------------------------------
<HTML><HEAD><TITLE> 在模态窗口中打开的框架页 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer">
</HEAD><BODY topmargin=0 leftmargin=0 scroll=no>
<iframe width="100%" height="100%" border=0 id="frame1"></iframe>
<script Language="Javascript">
var el=document.frames.frame1;
if(el!=null){
var arg=window.dialogArguments;
if(arg!=null)
frame1.location.href=arg;
}
</script>
</BODY>
</HTML>