我有以下按钮,它位于UpdatePanel
之外,我想用它来更新:
<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>
代码隐藏:
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();
}
}
以下功能更新页面刷新时的消息:
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;
}
}
}
如何修改我的代码,以便在按下提交按钮时更新lblMessage
而无需刷新页面?
答案 0 :(得分:1)
您可以使用此类外部触发器
<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>
请注意Triggers
部分