简体   繁体   English

将客户端处理程序从内容页面添加到母版页控件

[英]Add client side handler from content page to master page control

I have a requirement for four similar pages in an ASP.Net webforms application. 我在ASP.Net Webforms应用程序中需要四个相似的页面。 All four pages will have a header with a dropdown and two buttons. 所有四个页面的标题都带有一个下拉菜单和两个按钮。 I intend to use a master page for this header and then content pages for each of the different pages I need to produce. 我打算在此页眉中使用母版页,然后在我需要生成的每个不同页面中使用内容页。 My master looks like this: 我的主人看起来像这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Processing.master.cs" Inherits="MyProject.Processing" %>

<!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">
    <link href="../../Stylesheets/Processing.css" type="text/css" rel="stylesheet" />
    <script language="javascript" src="../../Scripts/js/Processing.js" type="text/javascript"></script>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="ProcessingForm" height="100%" runat="server">
        <asp:Label ID="titleLabel" runat="server" class="bold"></asp:Label>
        <asp:Label runat="server" ID="storeLabel" Text="Store:" />
        <asp:ObjectDataSource runat="server" ID="storesDataSource" TypeName="MyProject.Processing"
            SelectMethod="GetStoresDataSource">
            <SelectParameters>
                <asp:QueryStringParameter Name="UserName" QueryStringField="eUserName" DefaultValue="" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:DropDownList runat="server" ID="storeDropdown" AutoPostBack="true" OnSelectedIndexChanged="storeDropdown_SelectedIndexChanged"
            AppendDataBoundItems="true" DataSourceID="storesDataSource" DataTextField="Display"
            DataValueField="Number">
            <asp:ListItem />
        </asp:DropDownList>
        <asp:Button ID="refreshButton" runat="server" Text="Refresh" OnClick="refreshButton_Click" />
        <%-- ************************************************************************************--%>
        <%-- This is the button that I want to add client side behaviour to from my content pages--%>
        <%-- ************************************************************************************--%>
        <asp:Button ID="processButton" runat="server" Text="Process" OnClick="processButton_Click"
            OnClientClick="DoStuff();" />
        <asp:ContentPlaceHolder ID="GridDiv" runat="server" /> 
    </form>
</body>
</html>

In the Processing.master.cs I've defined an event that the content pages can subscribe to to react when the dropdown changes: 在Processing.master.cs中,我定义了一个事件,当下拉列表更改时,内容页面可以订阅以作出反应:

public delegate void StoreChangedHandler(object sender, string selectedValue);
public event StoreChangedHandler OnStoreChanged;

protected void storeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    if (OnStoreChanged != null)
    {
        OnStoreChanged(sender, ((DropDownList)sender).SelectedValue);
    }
}

So the content page's OnLoad contains: 因此,内容页面的OnLoad包含:

protected void Page_Load(object sender, EventArgs e)
{
    ((Processing)Master).OnStoreChanged += StockRoomWithMaster_OnSomethingSelected;
}

void StockRoomWithMaster_OnSomethingSelected(object sender, string selectedValue)
{
    this.stockReceiptGridView.DataBind();
}

What is the best way to do this same strategy for the client side DoStuff() function call that occurs when the processButton control is clicked? 在单击processButton控件时发生的客户端DoStuff()函数调用执行相同策略的最佳方法是什么? I could declare a global variable in the Processing.js file and in the content page assign it to a function: 我可以在Processing.js文件中声明一个全局变量,然后在内容页面中将其分配给一个函数:

Processing.js: Processing.js:

var processButtonClickedHandler;

function DoStuff()
{
    if (processButtonClickedHandler) {
        processButtonClickedHandler();
    }
}

In content page: 在内容页面中:

$(document).ready(function() {
    processButtonClickedHandler = DoSpecificStuff;
});

function DoSpecificStuff() {
    // Custom page client side stuff
}

I think the way you are doing this is the best way. 我认为您执行此操作的方式是最好的方法。 I have created created a test bu putting an alert in DoSpecificStuff in the same way you did and it worked good. 我创建了一个测试bu,它以与您相同的方式将警报放入DoSpecificStuff中,并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM