在内容页中获取主页面控制值静态网页方法在c#中。

问题描述 投票:1回答:1

我可以去取 MasterPage 控制值 Content Page

但我不明白如何取用 MasterPage 控制值 Content Pagestatic webmethod

在谷歌上,我发现了许多有趣的文章,但所有的人都使用了 ajaxjquery 技术

不过 ajaxjquery 在这种情况下不适合我

有什么建议,请?

我的代码如下

主页面

public partial class MasterPage : MasterPage
{
    public string UserNamePropertyOnMasterPage
    {
        get
        {
             // Get value of control on master page
             return lblUserName.Text;
        }
        set
        {
            // Set new value for control on master page 
            lblUserName.Text = value;
        }
    }
}

    <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
            <span style="font-size: 25px; background-color: greenyellow">
                <asp:Label ID="lblUserName" runat="server" Text="Shazam"></asp:Label>
            </span>
    </form>

缺省.aspx.cs的代码后台。

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblCurrentUserName.Font.Size = 20;
            lblCurrentUserName.BackColor = Color.Yellow;
            lblCurrentUserName.Text = "Value Received in Content Page : " + Master.UserNamePropertyOnMasterPage;
        }
    }

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static void SetLabel(string UserNamePropertyOnMasterPage)
    {
        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();

        Label Hname = (Label)Master.UserNamePropertyOnMasterPage;
        lblCurrentUserName.Text = Hname;
    }
}

Default.aspx.cs的标记

<%@ Page Title="" Language="C#" MasterPageFile="MasterPage.master" 
    AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ MasterType VirtualPath="MasterPage.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="lblCurrentUserName" runat="server" Text=""></asp:Label>
</asp:Content>
c# master-pages webmethod
1个回答
0
投票

从静态Web方法中调用MasterPage方法是不可能的。 这是C#中需要理解的一个基本概念。 基本上,在web请求期间,主页面并不存在,只有web方法被调用。 只有Web方法被调用。

使用JavaScriptjQuery来更新当前页面的HTML。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.