c# - ASP.NET UpdatePanel 不适用于 Page_Load 中的 Response.Write

标签 c# asp.net

我有那个例子:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <fieldset>
                <legend>UpdatePanel</legend>
                <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br />
            </fieldset>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>

protected void Page_Load(object sender, EventArgs e)
{
    // doesn work with:
    Response.Write("eg.");    
}

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Refreshed at " + DateTime.Now.ToString();
}

这很好用。当我点击 Button Label更新而不刷新页面,但如果我有 Response.WritePage_Load事件,当我点击按钮 UpdatePanel不起作用,我需要 Page_Load .

我该如何解决我的问题?谢谢。

最佳答案

不要在 Page_Load 函数中使用 Response.Write(),因为它会破坏 UpdatePanel

事实上,使用 Response.Write() 通常不是一个好主意,因为您无法控制写入内容在 DOM 中的最终位置。相反,如果您需要一些简单的调试输出,则使用其他方法,例如 System.Diagnostics.Debug.WriteLine()。或者,如果您需要向 DOM 添加一些内容,请添加一个控件,例如 Label 并对其进行操作。

关于c# - ASP.NET UpdatePanel 不适用于 Page_Load 中的 Response.Write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374724/

相关文章:

c# - 在 View 中使用接口(interface)类型作为模型并使用真实类型属性和验证的最佳实践

c# - VS2008 : File creation fails randomly in unit testing?

c# - 从字符串中获取枚举字段

asp 中的 JavaScript :Wizard

c# - 将具有配置文件的网站转换为 Web 应用程序项目

asp.net - 需要有关选择数据访问方法的建议

c# - 尽管将 Visibility 设置为 Hidden 并将 WindowState 设置为 Minimized,WPF Window 仍然显示为可最大化的矩形

c# - 在 WPF 中创建可重用的界面元素

c# - 组合框值加载问题

asp.net - 升级到 TLS 1.2 后无法生成 Web 服务引用