我正在开发AJAX网站功能,当用户单击按钮时,页面上的某些内容会更新。我遇到的问题是,该按钮位于仅在某些页面上显示的特定控件中,而我需要更新的一些信息位于Site.master文件中。以下是正在发生的事情:
单击按钮后,我希望更新的Site.master代码此代码在每个页面的标题中,但只有某些页面才能更新它。
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="Panel1" runat="server">
<ContentTemplate>
<asp:HyperLink
ID="Items" runat="server"
EnableViewState="False"
NavigateUrl="/Destination.aspx"
Text="0 items"
updatemode="Conditional" />
</ContentTemplate>
</asp:UpdatePanel>
单独控件(Items.ascx)中的按钮。这只显示在某些页面上。
<asp:UpdatePanel ID="Panel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateItems" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:Button runat="server"
OnClick="UpdateItems"
Text="Update Items"
class="update-items"
ID="UpdateItems"
name="UpdateItems"
type="submit">
</asp:Button>
</ContentTemplate>
</asp:UpdatePanel>
以及单击按钮时运行的方法(Items.ascx.cs)。单击后,我希望更新第一个代码块中的Items超链接。此代码仅在某些页面上显示。
protected void UpdateItems(object sender, EventArgs e)
{
UpdateItems.Text = "Done!";
// can't use Items.Text = "1" or similar due to this being a separate control
}
当我点击按钮时,文本成功地变为"完成!",这意味着事件启动得很好。问题是我不知道如何更新Site.master文件中的Items超链接。我搜索了许多不同的想法,但最终都一无所获。
我想注意的是,这是对现有网站的更新,因此由于这些控件如何影响布局以及它们在网页布局中的位置,这些控件的位置无法轻易移动或可能根本无法移动
首先修复Site.master
上的错误类型:将updatemode
属性从超链接移动到UpdatePanel标记。接下来,有两种方法
A.在site.master
上设置UpdateMode="Always"
。我希望超链接导航URL和文本不是硬编码的
B.在您的控制代码中
protected void UpdateItems(object sender, EventArgs e)
{
UpdateItems.Text = "Done!";
var mp = this.Page.MasterPage;
var up = mp.FindControl("Panel11");
var hl = up.FindControl("Items");
//do something with hl
up.Update(); //if updateMode="Conditional"
// can't use Items.Text = "1" or similar due to this being a separate control
}