ASP CompositeControl & ScriptManager

I am really new to the world of WebControl / CompositeControl, and I have a small class of tests that I play with. This is just LinkButton, which is updated when clicked. Everything works fine when I leave it from UpdatePanel. But when I try to run it inside, I still get the full POST response. How can I make this class work inside 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 send the message correctly, but I am not getting updated text for LinkButton. He is still firing the event - when I debug, I see what it is called. What is the correct way to install this control for use in UpdatePanel?

Use, just in case:

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

ID... javascript, UpdatePanel. , .

testLinkButton.ID = "btn";
+1

Source: https://habr.com/ru/post/1704303/


All Articles