ASP 复合控制与脚本管理器

发布于 2024-08-19 16:40:39 字数 2486 浏览 5 评论 0原文

我对 WebControl / CompositeControl 世界真的很陌生,并且我正在使用一个小型测试类。它只是一个点击时更新的LinkBut​​ton。当我将其排除在 UpdatePanel 之外时,效果会很好。但是当我尝试在内部运行它时,我仍然收到整页 POST 响应。如何使此类在 UpdatePanel 中工作?

类如下:

public class Test2 : CompositeControl 
{
    private static readonly object testButtonEvent = new object();

    public event EventHandler OnTestClick
    {
        add { Events.AddHandler(testButtonEvent, value); }
        remove { Events.RemoveHandler(testButtonEvent, value); }
    }

    private LinkButton testLinkButton;

    public virtual string testLinkButtonText
    {
        get
        {
            object o = ViewState["testLinkButtonText"];
            return (o == null) ? String.Empty : (string)o;
        }
        set
        {
            if (value == null)
                ViewState.Remove("testLinkButtonText");
            else
                ViewState["testLinkButtonText"] = value;
        }
    }   

    protected override void OnInit(EventArgs e)
    {
        /* This stuff makes it ajax friendly but stops the text rendering
        EnsureChildControls();

        ScriptManager ScMan = ScriptManager.GetCurrent(Page);
        if (ScMan != null)
        {
            ScMan.RegisterAsyncPostBackControl(testLinkButton);
        }            */

        base.OnInit(e);
    }

    protected override void CreateChildControls()
    {
        Controls.Clear();

        testLinkButton = new LinkButton();
        testLinkButton.Command += new CommandEventHandler(testClick);
        testLinkButtonText = "Test ViewState Text";

        Controls.Add(testLinkButton);
    }

    void testClick(object sender, CommandEventArgs e)
    {
        testLinkButtonText = "Updated Text On " + DateTime.Now.ToLongTimeString();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        RenderContents(writer);
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        EnsureChildControls();

        testLinkButton.Text = testLinkButtonText;
        testLinkButton.RenderControl(writer);            
    }

}

OnInit() 中的代码导致控件正确发布,但我没有获得 LinkBut​​ton 的更新文本。它仍在触发事件 - 当我调试时,我可以看到它被调用。设置此控件以在 UpdatePanel 中使用的正确方法是什么?

用法,以防万一:

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <cc:Test2 ID="jqTest02" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

I'm really new to the WebControl / CompositeControl world, and I have a small test class I am playing with. It's just a LinkButton that updates when clicked. Things work great when I leave it out of UpdatePanel. But when I try to run it inside I still get a full page POST response. How can I make this class work inside a UpdatePanel?

Here's the class:

public class Test2 : CompositeControl 
{
    private static readonly object testButtonEvent = new object();

    public event EventHandler OnTestClick
    {
        add { Events.AddHandler(testButtonEvent, value); }
        remove { Events.RemoveHandler(testButtonEvent, value); }
    }

    private LinkButton testLinkButton;

    public virtual string testLinkButtonText
    {
        get
        {
            object o = ViewState["testLinkButtonText"];
            return (o == null) ? String.Empty : (string)o;
        }
        set
        {
            if (value == null)
                ViewState.Remove("testLinkButtonText");
            else
                ViewState["testLinkButtonText"] = value;
        }
    }   

    protected override void OnInit(EventArgs e)
    {
        /* This stuff makes it ajax friendly but stops the text rendering
        EnsureChildControls();

        ScriptManager ScMan = ScriptManager.GetCurrent(Page);
        if (ScMan != null)
        {
            ScMan.RegisterAsyncPostBackControl(testLinkButton);
        }            */

        base.OnInit(e);
    }

    protected override void CreateChildControls()
    {
        Controls.Clear();

        testLinkButton = new LinkButton();
        testLinkButton.Command += new CommandEventHandler(testClick);
        testLinkButtonText = "Test ViewState Text";

        Controls.Add(testLinkButton);
    }

    void testClick(object sender, CommandEventArgs e)
    {
        testLinkButtonText = "Updated Text On " + DateTime.Now.ToLongTimeString();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        RenderContents(writer);
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        EnsureChildControls();

        testLinkButton.Text = testLinkButtonText;
        testLinkButton.RenderControl(writer);            
    }

}

The code in OnInit() causes the control to post correctly, but I don't get the updated text for the LinkButton. It is still firing off the event - when I debug I can see it being called. What's the proper way to set this control up for use in a UpdatePanel?

Usage, just in case:

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <cc:Test2 ID="jqTest02" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-08-26 16:40:39

您必须为按钮提供一个 ID 属性...这在驱动 UpdatePanel 的客户端 JavaScript 中使用。更具体地说,它列在要拦截并执行异步回发的控件列表中。

testLinkButton.ID = "btn";

You have to give the button an ID property...this is used in the client-side javascript that drives the UpdatePanel. More specifically, it's listed in the list of controls to intercept and do async postbacks for.

testLinkButton.ID = "btn";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文