代码改变世界

内容页与母板页 少许归纳

2013-07-31 14:48  糯米粥  阅读(323)  评论(0编辑  收藏  举报

1:继承了母板页后。遍历内容页中的控件

  a:内容页继承了母板页。可以这样遍历内容页中的控件

    

            foreach (Control cp in Page.Controls)
            {
                foreach (System.Web.UI.Control ct in cp.Controls)
                {
                    if (ct is HtmlForm)
                    {
                        foreach (Control con in ct.Controls)
                        {
                            foreach (Control c in con.Controls)
                            {
                                //string typeName = c.GetType().Name; //获取 控件类型

                                if (c is TextBox)
                                {
                                    string tempId = (c as TextBox).ID;
                                    //排除配置文件中的三个文本框
                                    if (tempId == "tableName" || tempId == "QuerySift" || tempId == "QueryType")
                                        continue;
                                    if (!string.IsNullOrEmpty((c as TextBox).Text))
                                    {
                                        if ((c as TextBox).ID == "add")
                                        {
                                            string value = (c as TextBox).Text;
                                            search.Add("add", value.Trim());
                                            searchAdd.Append(string.Format("(add1 like {0} or add2 like {1} or add3 like {2})", "@add1", "@add2", "@add3"));

                                        }
                                        //如果手机/电话输入框有值
                                        else if ((c as TextBox).ID == "phonetell")
                                        {
                                            string value = (c as TextBox).Text;
                                            search.Add("phone", value.Trim());
                                            searchPhone.Append(string.Format("(tel1 like {0} or mphone like {1})", "@tel1", "@mphone"));
                                        }
                                        else
                                            //筛选信息以 K-V的方式保存
                                            search.Add((c as TextBox).ID.Trim(), (c as TextBox).Text.Trim());
                                    }
                                }
                                else if (c is DropDownList)
                                {
                                    if ((c as DropDownList).SelectedValue != "请选择")
                                        search.Add((c as DropDownList).ID.Trim(), (c as DropDownList).SelectedValue.Trim());
                                }
                                else if (c is RadioButton)
                                {
                                    // RadioButton d = new RadioButton();

                                }
                                else if (c is RadioButtonList)
                                {
                                    //多选就拼接string.Format("");
                                }

                            }
                        }
                    }
                }
            }
View Code

b:但上面的方法代码量太多。我可以这样解决。把内容中的控件放在<UpdatePanel> </asp:UpdatePanel>遍历起来不仅代码少。还达到了局部刷新的效果

 foreach (Control c in UpdatePanel1.Controls[0].Controls)
            {
                if (c is TextBox)
                {
                    string tempId = (c as TextBox).ID;
                    //排除配置文件中的三个文本框
                    if (tempId == "tableName" || tempId == "QuerySift" || tempId == "QueryType")
                        continue;
                    if (!string.IsNullOrEmpty((c as TextBox).Text))
                    {
                        if ((c as TextBox).ID == "add")
                        {
                            string value = (c as TextBox).Text;
                            search.Add("add", value.Trim());
                            searchAdd.Append(string.Format("(add1 like {0} or add2 like {1} or add3 like {2})", "@add1", "@add2", "@add3"));

                        }
                        //如果手机/电话输入框有值
                        else if ((c as TextBox).ID == "phonetell")
                        {
                            string value = (c as TextBox).Text;
                            search.Add("phone", value.Trim());
                            searchPhone.Append(string.Format("(tel1 like {0} or mphone like {1})", "@tel1", "@mphone"));
                        }
                        else
                            //筛选信息以 K-V的方式保存
                            search.Add((c as TextBox).ID.Trim(), (c as TextBox).Text.Trim());
                    }
                }
                else if (c is DropDownList)
                {
                    if ((c as DropDownList).SelectedValue != "请选择")
                        search.Add((c as DropDownList).ID.Trim(), (c as DropDownList).SelectedValue.Trim());
                }
                else if (c is RadioButton)
                {
                    // RadioButton d = new RadioButton();

                }
                else if (c is RadioButtonList)
                {
                    //多选就拼接string.Format("");
                }
            }  
View Code

2:内容页中获取母板页中的控件

     ASPxGridView gdv = Master.FindControl("aspxGdv") as ASPxGridView;

3:母板页中获取内容页中的控件

上面的方法也能获取到。但如果是在Content里面就不一样。比如内容页中如下代码

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">

    <asp:TextBox ID="QueryType" runat="server" Visible="false" Text="C"></asp:TextBox>

</asp:Content>

则在母板页中如何获取呢?

 string QueryType = ((TextBox)this.ContentPlaceHolder3.FindControl("QueryType")).Text;   原理很简单。先找到ContentPlaceHolder3在找到里面的TextBox(都是根据ID来找)

4:页面事件的执行顺序

  我们指定一个页面继承了母板页。母板页中有控件的话。会先执行Init方法。如果你在内容页中的Page_Load里面给母板页中的变量或方法赋值。然后在母板页中取值。如果在母板页中Init时候就要取值

显然值是null,因为先执行的母板页中的init。在执行内容页中的Page_Load(后执行母板页中的Page_Load) 那我们可以用Page_PreInit事件。因为他在init事件前。我们在内容页中的Page_PreInit事件的时候给

母板页中的遍历或控件赋值。或者给事件添加方法。(我这里就是给事件添加方法) 在母板页init的时候回调内容页中的控件取值。方法有很多。之间灵活运用即可

内容页中:

 protected void Page_PreInit(object sender, EventArgs e)
        {
            //页面加载时候。给母板页的事件添加一个回调方法
            Master.showDataHander += new master.DataBase.showDataDelegeat(Master_showDataHander);
        }

        void Master_showDataHander()
        {

            Master.tablename = "cli";
        }

母板页中:

        public DataTable dt;  //要显示表的数据

        public delegate void showDataDelegeat(); //查询数据委托

        public event showDataDelegeat showDataHander; //事件

 public void aspxGdv_Init(object sender, EventArgs e)
        {

            showDataHander(); //回调父页面方法 重新加载数据

当然。你在内容页中要加上这个 至于加与不加自己试试就知道。或者 你取母板页中的值为nul的时候。你要想到是否是这句指令的问题 。其实就是一个谁先加载先执行的问题。还没执行前你去取值。显然是null

<%@ MasterType VirtualPath="~/master/DataBase.Master" %>

 

5:内容页中用js或者style

当我们继承一个母板页。在源码中会有这个东东 ,样式也js脚本都写在里面就可以了。 但母板页中就不能这样。母板页中的跟平常一样。

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

 

6:修改母板页中的控件

 如果继承了母板页。想修改母板页中控件。那么母板页中的控件要放在 

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>

这样内容页中可以任意修改

 

7:iframe的使用

父页面调用ifrmae中的js函数

   function sho() {
            var show = document.getElementById("id").contentWindow;
            show.so();
            window.frames["id"].contentWindow.so();
        }

来自网络:

父页面调用iframe里的js函数:

document.getElementById('iframedemo').contentWindow.demofunction(); //与下边一句等价
window.frames['iframedemo'].contentWindow.demofunction();
其中iframedemo是iframe的id,demofunction是iframe里的js函数名

父页面调用iframe里的dom元素:

document.getElementById('iframedemo').contentDocument.getElementById("INPUT_Text").value; //与下边一句等价
window.frames['iframedemo'].contentDocument.getElementById("INPUT_Text").value;
其中iframedemo是iframe的id

注意事项:

contentWindow 兼容各个浏览器,可取得子窗口的 window 对象。
contentDocument Firefox 支持,> ie8 的ie支持。可取得子窗口的 document 对象。

题外话:

那么从子页面(iframe)调用父页面,就用window.parent;
如果从页面A中,执行了window.open(url);打开的页面B, 那么页面B访问页面A,就用window.opener;
如果跨域访问父页面或者子页面的js函数或dom,会有拒绝访问的情况,一般要保证同域,document.domain="camnpr.com";

 jQuery-JS在iframe中获取父页面的值 

1.jQuery

        var parentVal=$("#hid_RID",parent.document).val();

2.JS

        var parentVal=parent.document.getElementById("hid_RID").value;

$("#objid",document.frames('iframename').document)

注:其中的hid_RID为父页面的控件ID。