从内容页面更新母版页控件

时间:2014-08-26 19:02:02

标签: c# asp.net updatepanel

我有UpdatePanel Label,我想在MasterPage更新。该活动正在我的内容页面上进行,以下是代码。

内容ASP.net代码:

<%@ 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#代码隐藏:

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代码:

<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>

当我更改DropDown选项时,主页中的UpdatePanel不会更新,尽管在那里放置一个断点,表明它正被击中。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

基本上,您希望从 MasterPage 公开公共属性。然后从 ContentPage 访问它。

母版页

<%@ 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; }
    }
}

内容页面

<%@ 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;
        }
    }
}