[1.0] XRender - Creating controls during DirectEvent

  1. #1

    [1.0] XRender - Creating controls during DirectEvent

    Hi,

    Thank you for the hard work you did on v1.0. Looks great :)

    I'm trying to create a control during DirectEvent and I want to postback using the created control.

    My code:

     protected void Page_Load(object sender, EventArgs e)
        {
            var b = new Ext.Net.Button("Click Me");
            b.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(c_Event);
            this.Controls.Add(b);
            b.Render();
    
        }
    
        void c_Event(object sender, DirectEventArgs e)
        {
           
        }
    but it fails:

    HttpException (0x80004005): The control with ID 'id0ac2c6e68e284660b7766ed399a76908' not found] Ext.Net.ResourceManager.RaisePostBackEvent(String eventArgument) +1083 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    Am I missing something fundamental? Can this be done at all?

    Thanks, Omer.
  2. #2

    RE: [1.0] XRender - Creating controls during DirectEvent

    Hi,

    1. Do not call Render during initial page load (call it during ajax event only)
    2. Please set ID if you want to reuse control on server side (handle events, submitted data). Also you have to recreate dynamic control on each request (but without Render calling, Render should be called once only)
  3. #3

    RE: [1.0] XRender - Creating controls during DirectEvent

    Thank you very much Vlad.

    I altered the code to the following:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ext:ResourceManager runat="server" ID="ExtNetResourceManager">
        </ext:ResourceManager>
        
                <ext:Button runat="server" Text="one">
                    <DirectEvents>
                        <Click OnEvent="btnClick">
                        </Click>
                    </DirectEvents>
                </ext:Button>
        
    
        </form>
    
        <script runat="server">
            protected void btnClick(object sender, DirectEventArgs e)
            {
                var b = new Ext.Net.Button("Click Me");
                b.ID = "myID";
                b.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(c_Event);
                this.Controls.Add(b);
                b.Render();
            }
    
            protected void c_Event(object sender, DirectEventArgs e)
            {
    
            }
        </script>
    
    </body>
    </html>
    And I still get the same error:

    [HttpException (0x80004005): The control with ID 'myID' not found]
    
       Ext.Net.ResourceManager.RaisePostBackEvent(String eventArgument) +1083
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    Can you please advise?
  4. #4

    RE: [1.0] XRender - Creating controls during DirectEvent

    Hi,

    As I mentioned before
    you have to recreate dynamic control on each request (but without Render calling, Render should be called once only)
  5. #5

    RE: [1.0] XRender - Creating controls during DirectEvent

    Thanks again, I missed that part :)

    I've altered the previous server code to the following:

    protected void Page_Load(object sender, EventArgs e)
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["created"]!=null &amp;&amp; (bool)Session["created"])
                {
                    var b = new Ext.Net.Button("Click Me");
                    b.ID = "myID";
                    b.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(c_Event);
                    this.Controls.Add(b);
                }
            }
    
            protected void btnClick(object sender, DirectEventArgs e)
            {
                var b = new Ext.Net.Button("Click Me");
                b.ID = "myID";
                b.DirectEvents.Click.Event += new ComponentDirectEvent.DirectEventHandler(c_Event);
                this.Controls.Add(b);
                b.Render();
                Session["created"] = true;
            }
    And it works. Brilliant!

    So I just want to understand - I have to manage this for each control I create dynamically in order to add it on each request?

  6. #6

    RE: [1.0] XRender - Creating controls during DirectEvent

    Hi,

    Here is another variation for dynamic Button component creation during a DirectEvent.

    Example

    <%@ Page Language="C#" %>
    
    <%@ Import Namespace="Button=Ext.Net.Button" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        Button btn;
        
        protected override void OnInit(EventArgs e)
        {
            btn = new Button
            {
                ID = "Button2",
                Text = "Button 2"
            };
            
            if (X.IsAjaxRequest)
            {
                btn.DirectEvents.Click.Event += Button2_Click;
                this.Controls.Add(btn);
            }
            
            base.OnInit(e);
        }
        
        protected void Button1_Click(object sender, DirectEventArgs e)
        {
            btn.Render();
        }
    
        protected void Button2_Click(object sender, DirectEventArgs e)
        {
            X.Msg.Notify("Message", "Server Time: " + DateTime.Now.ToLongTimeString()).Show();
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
        
            <ext:Button ID="Button1" runat="server" Text="Button 1" OnDirectClick="Button1_Click" />
        </form>
    </body>
    </html>
    Hope this helps.

    Geoffrey McGill
    Founder
  7. #7

    RE: [1.0] XRender - Creating controls during DirectEvent

    What I like in that code sample is that you don't need to manage the rendering.


    Button2 is added to the controls collection of the form on each ajax request.
    If button1 has been clicked, then it renders button2 therefor button2 is visible on page, and will stay there for each ajax request.
    If button1 has not been clicked, and I perform an ajax request, Button2 will still be added to the form's controls collection, but will not be rendered until button1 will be pressed.


    Very nice!!!


    Thank you.


    Omer

Similar Threads

  1. [CLOSED] DirectEvent throwing error for controls creating at runtime
    By rnachman in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: May 23, 2011, 6:34 PM
  2. [CLOSED] Creating DirectEvent from codebehind
    By krzak in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 01, 2011, 10:17 AM
  3. [1.0] XRender with child controls
    By lukasw in forum 1.x Help
    Replies: 9
    Last Post: Apr 21, 2010, 6:16 AM
  4. [CLOSED] Creating dynamic controls
    By antonyn in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jan 29, 2009, 1:23 PM

Posting Permissions