本文介绍了如何访问Top Master Page按钮单击第3个子页面中的Event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我在主页之间有一些情况沟通..

这是我的情景

我有一个主人页面(顶部)名为MainMasterPage,其中有一个按钮和一个文本框。

然后是名为CategoryMasterPage的第二个母版页,它是主要的子项。

然后有一个页面SearchResults.aspx,其中有一个转发器控件。



现在我想要的是当我点击按钮(它在mainMasterPage),它应该在searchResults.aspx中触发它的事件而不是它自己的主页的cs文件。

这样我就可以访问searchResults.aspx的控件来轻松附加数据。

换句话说,当点击按钮时,我想在searchResults页面中显示文本框数据。



我也按照这个链接 []



但它仅在与一个主人和一个孩子一起工作时有效。我因嵌套母版页而遇到问题..



谢谢。

Hi.
I have a bit of Situation communicating in between Master Pages..
Here is my Scenario
I have a Master Page(Top) named "MainMasterPage" which have a button and a text box in it.
Then a Second Master Page named "CategoryMasterPage" which is Child of the main.
Then there is a page "SearchResults.aspx" which have a repeater control in it.

now all I want is that when i click the button (which is in mainMasterPage), it should trigger its event in searchResults.aspx instead of the cs file of it own masterpage.
so that i can access searchResults.aspx's control to append data easily.
in other words I want text box data in searchResults page when the button is clicked.

I've followed this link too Master Page, Child Pages: Dynamic Communication Flexibility[^]

but its only valid while working with one master and one child. I am having problem due to nested master pages..

Thanks.

推荐答案

MasterPageMain.master




<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageMain.master.cs"

    Inherits="MasterPageMain" %>

<!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>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
                </td>
                <td>
                    <asp:Button runat="server" ID="btnSearch" Text="Search" />
                </td>
            </tr>
        </table>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html



>




>

MasterPageMain.master.cs




using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPageMain : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public Button refbtnSearch
    {
        get { return btnSearch; }
    }

    public TextBox reftxtSearch
    {
        get { return txtSearch; }
    }
}







MasterPageCategory.master




<%@ Master Language="C#" MasterPageFile="~/MasterPageMain.master" AutoEventWireup="true"

    CodeFile="MasterPageCategory.master.cs" Inherits="MasterPageCategory" %>
<%@ MasterType VirtualPath="~/MasterPageMain.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content



>




>

MasterPageCategory.master.cs




using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPageCategory : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public Button refbtnSearch
    {
        get { return Master.refbtnSearch; }
    }

    public TextBox reftxtSearch
    {
        get { return Master.reftxtSearch; }
    }
}










SearchResults.aspx




<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageCategory.master" AutoEventWireup="true"

    CodeFile="SearchResults.aspx.cs" Inherits="SearchResults" %>
    <%@ MasterType VirtualPath="~/MasterPageCategory.master" %>
<%-- Add content controls here --%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table>
        <tr>
            <td>
                <asp:Repeater ID="Repeater1" runat="server">
                    <HeaderTemplate>
                        <table border="1" width="100%">
                            <tr>
                                <th>
                                    ID
                                </th>
                                <th>
                                    Name
                                </th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# Eval("Id") %>
                            </td>
                            <td>
                                <%# Eval("Name")%>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
            </td>
        </tr>
    </table>
</asp:Content



>




>

SearchResults.aspx.cs




using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SearchResults : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = getData();
            Repeater1.DataBind();
        }

        Master.refbtnSearch.Click += new EventHandler(doSearchMaster);
    }

    private DataTable getData()
    {
        DataTable dt = new DataTable();
        if (ViewState["dt"] != null)
            dt = (DataTable)ViewState["dt"];
        else
        {
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            for (int i = 0; i < 20; i++)
            {
                DataRow dr = dt.NewRow();
                dr["Id"] = i;
                dr["Name"] = "Name-" + i.ToString();
                dt.Rows.Add(dr);
            }
            ViewState["dt"] = dt;
        }
        return dt;
    }

    public void doSearchMaster(object sender, EventArgs e)
    {
        DataTable dt = getData();
        DataView dv = new DataView(dt);
        dv.RowFilter = "Name LIKE '%" + Master.reftxtSearch.Text.Trim() + "%'";
        Repeater1.DataSource = dv.ToTable();
        Repeater1.DataBind();
    }
}


这篇关于如何访问Top Master Page按钮单击第3个子页面中的Event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:02