Update MasterPage control from Content page

╄→尐↘猪︶ㄣ 提交于 2020-01-14 05:30:10

问题


I have an UpdatePanel with a Label which I would like to update in my MasterPage. The event is taking place from my Content page and here are the codes.

Content ASP.net code:

<%@ MasterType TypeName="OnBoarding.Pages.Site" %>
<asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged">
    <asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand="QUERY"></asp:SqlDataSource>

C# code-behind:

protected void ddlTaskName_onSelectIndexChanged(object sender, EventArgs e)
{
    FilterMessages();
}

public void FilterMessages()
{
        DataTable msgTable = HttpContext.Current.Session["MessageTable"] as DataTable;
        string query = "";
        Stack msgStack = new Stack();

        if (ddlClient.SelectedIndex > 0)
        {
            query += "Client = '" + ddlClient.SelectedItem.Text + "' OR Client is NULL";
        }
        if (ddlSite.SelectedIndex > 0 && query == "")
        {
            query += "Site = '" + ddlSite.SelectedItem.Text + "' OR Site is NULL";
        }
        else if (ddlSite.SelectedIndex > 0)
        {
            query += " AND Site = '" + ddlSite.SelectedItem.Text + "' OR Site is NULL";
        }

        if (ddlProvider.SelectedIndex > 0 && query == "")
        {
            query += "Provider = '" + ddlProvider.SelectedItem.Text + "' OR Provider is NULL";
        }
        else if (ddlProvider.SelectedIndex > 0)
        {
            query += " AND Provider = '" + ddlProvider.SelectedItem.Text + "' OR Provider is NULL";
        }
        UpdatePanel upMsg = (UpdatePanel)Master.FindControl("upMessage");
        if (query != "")
        {
            DataRow[] result = msgTable.Select(query);
            System.Web.UI.WebControls.Label lblMsg = (System.Web.UI.WebControls.Label)Master.FindControl("lblMessage");
            if (lblMsg != null)
            {
                lblMsg.Text = "";
            }
            //lblMessage.Text = "";
            foreach (DataRow row in result)
            {
                if (row["Active"].ToString() == "True")
                {
                    string[] myStrings = new string[] { row["Created"].ToString().Split(' ')[0], string.IsNullOrEmpty(row["Client"].ToString()) ? null : row["Client"].ToString(), string.IsNullOrEmpty(row["Site"].ToString()) ? null : row["Site"].ToString(), row["Message"].ToString() };
                    string strResult = string.Join(" ", myStrings.Where(str => !string.IsNullOrEmpty(str)));
                    msgStack.Push(strResult);
                }
            }
            foreach (string message in msgStack)
            {
                lblMsg.Text += message + "</br>";
                //lblMessage.Text += message + "</br>";
            }
        }
        else
        {
            PopulateMessageGV();
        }
    if (upMsg != null)
    {
        upMsg.Update();
    }
    //upMessage.Update();
}
protected void PopulateMessageGV()
{
    UpdatePanel upMsg = (UpdatePanel)Master.FindControl("upMessage");
    System.Web.UI.WebControls.Label lblMsg = (System.Web.UI.WebControls.Label)Master.FindControl("lblMessage");
    if (lblMsg != null)
    {
        lblMsg.Text = "";
    }
    //lblMessage.Text = "";
    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Created", typeof(string));
    dt.Columns.Add("Message", typeof(string));
    dt.Columns.Add("Client", typeof(string));
    dt.Columns.Add("Site", typeof(string));
    dt.Columns.Add("Provider", typeof(string));
    dt.Columns.Add("Active", typeof(bool));
    dt.Columns.Add("CreatedBy", typeof(string));


    using (var context = new ClientContext(hostWeb))
    {
        Stack msgStack = new Stack();

        var hostSite = context.Web;
        context.Load(hostSite, s => s.Title);
        context.ExecuteQuery();

        ListCollection allLists = hostSite.Lists;
        Microsoft.SharePoint.Client.List messageList = allLists.GetByTitle("AdminMessage");
        context.Load(messageList);
        context.ExecuteQuery();

        try
        {
            var query = CamlQuery.CreateAllItemsQuery();
            Microsoft.SharePoint.Client.ListItemCollection allItems = messageList.GetItems(query);
            context.Load(allItems);
            context.ExecuteQuery();

            if (allItems.Count > 0)
            {
                foreach (Microsoft.SharePoint.Client.ListItem item in allItems)
                {
                    DataRow dr = dt.NewRow();
                    dr["ID"] = item["ID"];
                    dr["Created"] = item["Created"];
                    dr["Message"] = item["Message"];
                    dr["Client"] = item["Client"];
                    dr["Site"] = item["Site"];
                    dr["Provider"] = item["Provider"];
                    dr["Active"] = item["Active"];
                    FieldUserValue test = (FieldUserValue)item["Author"];
                    dr["CreatedBy"] = test.LookupValue.ToString();
                    dt.Rows.Add(dr);

                    if (item["Active"].ToString() == "True")
                    {
                        string[] myStrings = new string[] { item["Created"].ToString().Split(' ')[0], string.IsNullOrEmpty(dr["Client"].ToString()) ? null : item["Client"].ToString(), string.IsNullOrEmpty(dr["Site"].ToString()) ? null : item["Site"].ToString(), item["Message"].ToString() };
                        string result = string.Join(" ", myStrings.Where(str => !string.IsNullOrEmpty(str)));
                        msgStack.Push(result);
                    }
                }
            }
            HttpContext.Current.Session["MessageTable"] = dt;
            foreach (string message in msgStack)
            {
                lblMsg.Text += message + "</br>";
                //lblMessage.Text += message + "</br>";
            }

            if (upMsg != null)
            {
                upMsg.Update();
            }
            //upMessage.Update();
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
    }
}

MasterPage ASP.net code:

<asp:UpdatePanel runat="server" ID="upMessage" ClientIDMode="Static" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblMessage" Font-Size="x-small" runat="server" Text="" ClientIDMode="Static"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

When I change the DropDown option the UpdatePanel in the masterpage doesn't update although putting a breakpoint there, shows that it is being hit.

How do I resolve the issue?


回答1:


Basically, you want to expose Public Property from MasterPage. Then access it from ContentPage.

Master Page

<%@ Master Language="C#" AutoEventWireup="true"
    CodeBehind="Site.Master.cs" Inherits="DemoWebForm.Site" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <form id="form1" runat="server">
        <asp:Label runat="server" ID="MessageLabel" />
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </form>
</body>
</html>

public partial class Site : System.Web.UI.MasterPage
{
    public string MessageLabelText
    {
        get { return MessageLabel.Text; }
        set { MessageLabel.Text = value; }
    }
}

Content Page

<%@ Page Title="Home Page" Language="C#" 
    MasterPageFile="~/Site.Master" 
    AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="DemoWebForm._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:DropDownList ID="TaskNameDropDownList" 
        runat="server" 
        AutoPostBack="True"
        OnSelectedIndexChanged="TaskNameDropDownList_SelectedIndexChanged">
        <asp:ListItem Text="One" Value="1" />
        <asp:ListItem Text="Two" Value="2" />
    </asp:DropDownList>
</asp:Content>

public partial class _Default : Page
{
    protected void TaskNameDropDownList_SelectedIndexChanged(
      object sender, EventArgs e)
    {
        var master = Master as Site;
        if (master != null)
        {
            master.MessageLabelText = TaskNameDropDownList.SelectedValue;
        }
    }
}


来源:https://stackoverflow.com/questions/25513337/update-masterpage-control-from-content-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!