使用 CodeBehind 从不同控件的点击事件更新 Site.master 控件
Updating Site.master control using CodeBehind from a different control's click event
我正在研究 AJAX 站点功能,当用户单击按钮时,页面上的某些内容会更新。我遇到的问题 运行 是该按钮位于仅在某些页面上显示的特定控件中,我需要更新的一些信息位于 Site.master 文件中。这是正在发生的事情的想法:
我希望在单击按钮时更新 Site.master 代码。 此代码在每个页面的 header 中,但只有某些页面应该能够更新它。
<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)。 单击时我希望在第一个中更新项目超链接代码块。此代码仅在某些页面上显示。
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
}
当我单击按钮时,文本成功更改为 "Done!",这意味着事件正在正常触发。问题是我不确定如何更新 Site.master 文件中的 Items 超链接。我搜索了许多不同的想法,但最终一无所获。
我想指出,这是对现有网站的更新,因此这些控件的位置无法轻松移动或可能根本无法移动,因为它们会影响布局和位置它们在网页的布局中。
首先修复 Site.master
上的错误输入:将 updatemode
属性从超链接移动到 UpdatePanel 标记。接下来,有两种做法。
A. 在 site.master
上设置 UpdateMode="Always"
。我希望超链接 navigateUrl 和文本不是硬编码的。
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
}
我正在研究 AJAX 站点功能,当用户单击按钮时,页面上的某些内容会更新。我遇到的问题 运行 是该按钮位于仅在某些页面上显示的特定控件中,我需要更新的一些信息位于 Site.master 文件中。这是正在发生的事情的想法:
我希望在单击按钮时更新 Site.master 代码。 此代码在每个页面的 header 中,但只有某些页面应该能够更新它。
<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)。 单击时我希望在第一个中更新项目超链接代码块。此代码仅在某些页面上显示。
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
}
当我单击按钮时,文本成功更改为 "Done!",这意味着事件正在正常触发。问题是我不确定如何更新 Site.master 文件中的 Items 超链接。我搜索了许多不同的想法,但最终一无所获。
我想指出,这是对现有网站的更新,因此这些控件的位置无法轻松移动或可能根本无法移动,因为它们会影响布局和位置它们在网页的布局中。
首先修复 Site.master
上的错误输入:将 updatemode
属性从超链接移动到 UpdatePanel 标记。接下来,有两种做法。
A. 在 site.master
上设置 UpdateMode="Always"
。我希望超链接 navigateUrl 和文本不是硬编码的。
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
}