SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to update an UpdatePanel with a trigger control from outside of it

I have the following button which is outside of the UpdatePanel that I would like use to update:

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnExport" ClientIDMode="Static" UseSubmitBehavior="false" OnClick="SubmitAdminMessage" />

<asp:UpdatePanel ID="upMessages" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblMessage" runat="server" Text="" ClientIDMode="Static"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

The code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    upMessages.Triggers.Add(new AsyncPostBackTrigger()
    {
        ControlID = btnSubmit.UniqueID,
    });

    PopulateMessageGV(); //this displays the message on refresh of the page
}

protected void SubmitAdminMessage(object sender, EventArgs e)
{
    var hostWeb = Page.Request["SPHostUrl"];

    using (var context = new ClientContext(hostWeb))
    {
        var hostSite = context.Web;
        context.Load(hostSite, s => s.Title);
        context.ExecuteQuery();

        var allLists = hostSite.Lists;
        var messageList = allLists.GetByTitle("AdminMessage");
        var itemCreationInformation = new ListItemCreationInformation();
        var newMessage = messageList.AddItem(itemCreationInformation);
        newMessage["Message"] = tbMessage.Text;
        newMessage["Active"] = cbIsActive.Checked.ToString();
        newMessage.Update();

        context.ExecuteQuery();
    }
}

The following function updates the messages on page refresh:

protected void PopulateMessageGV()
{
    Microsoft.SharePoint.Client.ListItemCollection filteredItems = null;
    var hostWeb = Page.Request["SPHostUrl"];

    using (var context = new ClientContext(hostWeb))
    {
        var hostSite = context.Web;
        context.Load(hostSite, s => s.Title);
        context.ExecuteQuery();

        var allLists = hostSite.Lists;
        var messageList = allLists.GetByTitle("AdminMessage");
        context.Load(messageList);
        context.ExecuteQuery();
        MessageListCountLabel.Text = messageList.ItemCount.ToString();
        ListUrlHyperLink.NavigateUrl = hostWeb + "/Lists/AdminMessage";
        try
        {
            var query = CamlQuery.CreateAllItemsQuery();
            var allItems = messageList.GetItems(query);
            context.Load(allItems);
            context.ExecuteQuery();
            foreach (var item in allItems)
            {
                DataTable dt;
                if (item["Active"].ToString() == "True")
                {
                    msItem.Add(item["Created"].ToString() + " " + item["Message"].ToString());
                }
            }
            msItem.ToArray();
            foreach (var list in msItem)
            {
                lblMessage.Text += list + "<br />";
            }
            upMessages.Update();
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }

        try
        {
            var query = new CamlQuery();
            var camlViewXml = string.Format(@"<View><Query><Where><Eq>
                <FieldRef Name='Active'/><Value Type='Boolean'>
                {0}</Value></Eq></Where>
                </Query></View>", "False");
            query.ViewXml = camlViewXml;
            filteredItems = messageList.GetItems(query);
            context.Load(filteredItems, items => items.Include(
            item => item["ID"], item => item["Created"],
            item => item["Message"]));
            context.ExecuteQuery();
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
    }
}

How do I modify my code so that when the submit button is pressed the lblMessage is updated without having to refresh the page?

Upvotes: 0

Views: 939

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68440

You can use external triggers like this

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnExport" ClientIDMode="Static" UseSubmitBehavior="false" OnClick="SubmitAdminMessage" />

<asp:UpdatePanel ID="upMessages" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="lblMessage" runat="server" Text="" ClientIDMode="Static"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

Notice the Triggers section

Upvotes: 1

Related Questions