关闭子页面,只刷新父页面的DataGrid

界面说明:

父页面(CangKuEdit.aspx)中,按“新增”(id=imgbtnAdd),弹出CangKuAdd.aspx子页面。

子页面(CangKuAdd.aspx )中,按“确定”(id=btnSubmit),新增数据到数据库、关闭子页面、刷新父页面DataGrid 以反映数据的异动,注意只能刷新DataGrid,其他控件里输入的值要保留!

分两种情况:

一、 DataGrid直接写在父页面中

CangKuEdit.aspx

加上一段JS:

<HEAD>

    <script language="javascript">    

        function doPostBack()

        {

            var theform;

            

            if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) 

            {

                theform = document.forms["CangKuLLEdit"];

            }

            else 

            {

                theform = document.CangKuLLEdit;

            }

            

            theform.submit();

        }

    

        function openwin(strBillNO)

        {

            window.open("CangKuAdd.aspx?returncontrol=imgbtnAdd&strBillNo="+ strBillNO,'new1','left=160,top=200,height=450,width=750,toolbar=0,scrollbars=2');

            return false;

        }

    </script>

</HEAD>

CangKuEdit.aspx.cs

在Page_Load中加上:

    imgbtnAdd.Attributes.Add("ondblclick","doPostBack();");

    imgbtnAdd.Attributes.Add("onclick","return openwin('"+txtBillNo.Text+"');");

CangKuAdd.aspx.cs

在在“确定”事件btnSubmit_Click中加上:

    Response.Write("<script>window.opener.document.all.imgbtnAdd.fireEvent('ondblclick');window.close();</script>");

二、 DataGrid是父页面调用的用户控件(CCDataGrid.ascx,“新增”(id=imgbtnAdd)也在CCDataGrid.ascx中)

因为用户控件会在主页面控件之前被解析,

所以,用户控件的事件中,无法捕获主页面上的控件(如:<asp:textbox id="txtBillNo"/>,原因是它还未被解析)。

CCDataGrid.ascx

加上一段JS:

<script language="javascript">

    function PostBack()

    {

        __doPostBack("CCDataGrid1:CCDataGrid1_imgbtnAdd", null);

    }

</script>

CCDataGrid.ascx.cs

在Page_Load中加上:

    imgbtnAdd.Attributes.Add("ondblclick","PostBack();");

在“确定”事件imgbtnAdd_Click中加上:

this.Page.RegisterHiddenField("IsOpenAddPage","true");

// 在用户控件的"新增"事件中注册一个隐藏控件: 

CangKuEdit.aspx

在主页面的最底下写:

                </tr>

            </table>

        </FORM>

        <script language="javascript">

            var obj = document.getElementById("IsOpenAddPage");

            if (obj!=null)

            {

                if (obj.value=='true')

                {

                    OpenAddPage();

                    obj.value='';

                }

            }

            

            function OpenAddPage()

            {

                var SID=document.getElementById('txtBillNo').value;

                var url='CangKuAdd.aspx?returncontrol=CCDataGrid1_imgbtnAdd&strBillNo='+SID;

                window.open(url,'new1','left=160,top=200,height=450,width=750,toolbar=0,scrollbars=2');

            }

        </script>

    </BODY>

</HTML>

CangKuAdd.aspx.cs

在“确定”事件btnSubmit_Click中加上:

    Response.Write("<script>window.opener.document.all('CCDataGrid1_imgbtnAdd').fireEvent('ondblclick');window.close();</script>");

 

 

查询到的:

首先你要知道,在客户端的你要改变的控件的ID(一般和你设计时的一样),然后   
  window.opener.document.getElementById(ID).value=你的值,   
  这个适合改变象文本框内的简单数据,并且只能该你能欲知的数据.   
    
  如果你的那个控件和后台程序绑定或函数关联,经过很复杂的运算   
  才得到的话,那就用人为回发(asp.net是自动回发的),   
  window.opener.document.form1.submit()(这个刷新整个页面).   
    
  如要只刷新你预想的控件,就比较麻烦一点:   
  创建一个按钮(隐藏),让这个按钮触发能修改你的控件的函数,然后你要用着按钮的:   
  用Button1.Page下的方法(具体方法记不清了)或的在客户端的回发"脚本",然后给你的Button1添加客户端事件,事件的参数就是你获得的"脚本",函数体内用exec执行参数的内容,最后window.opener.document.你的客户端事件名字

posted on 2010-05-19 09:54  carekee  阅读(1644)  评论(0编辑  收藏  举报