[CLOSED] JS Error when Trying to Programmatically Add Check Item to CycleButton and Set Active

  1. #1

    [CLOSED] JS Error when Trying to Programmatically Add Check Item to CycleButton and Set Active

    Hi

    The following partial view (MVC) brings JS error.

    
    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" EnableViewState="false" %>
    <ext:Window runat="server">
        <Items>
            <ext:CycleButton ID="cb" runat="server" Text="CB">
                <Menu>
                    <ext:Menu runat="server">
                        <Items>
                            <ext:CheckMenuItem runat="server" Text="0" />
                        </Items>
                    </ext:Menu>
                </Menu>
            </ext:CycleButton>
        </Items>
    </ext:Window>
    <script runat="server">
        protected override void OnInit(EventArgs e)
        {
            cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1" });
            cb.ActiveItemIndex = 1;
        }
    </script>
    Last edited by Daniil; May 01, 2013 at 3:35 AM. Reason: [CLOSED]
  2. #2
    Hi @cleve,

    What is the JavaScript error?

    Could you also demonstrate how you render that partial view?
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi @cleve,

    What is the JavaScript error?

    Could you also demonstrate how you render that partial view?
    Sorry, here is the complete example:

    Controller:
    
            public ActionResult Index()
            {
                return View(@"~/test/index.aspx");
            }
    
    
            public ActionResult Test(string id)
            {
                Ext.Net.MVC.PartialViewResult r = this.PartialExtView("~/test/control2.ascx");
                r.ContainerId = "c";
                r.RenderMode = RenderMode.AddTo;
                r.ClearContainer = true;
                r.ControlId = "x";
                r.IDMode = IDMode.Client;
                return r;
            }
    Index.aspx
    
    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body id="theBody">
        <ext:ResourceManager runat="server" />
        <ext:Container runat="server" ID="c" />
        <ext:Button runat="server" Text="Inject to Upload">
            <DirectEvents>
                <Click Url="/test.aspx/test" />
            </DirectEvents>
        </ext:Button>
    </body>
    </html>
    Control2.ascx
    
    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" EnableViewState="false" %>
    <ext:Window runat="server">
        <Items>
            <ext:CycleButton ID="cb" runat="server" Text="CB">
                <Menu>
                    <ext:Menu runat="server">
                        <Items>
                            <ext:CheckMenuItem runat="server" Text="0" />
                        </Items>
                    </ext:Menu>
                </Menu>
            </ext:CycleButton>
        </Items>
    </ext:Window>
    <script runat="server">
        protected override void OnInit(EventArgs e)
        {
            cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1" });
            cb.ActiveItemIndex = 1;
        }
    </script>
    Error message screen is attached.
    Attached Thumbnails Click image for larger version. 

Name:	error.jpg 
Views:	22 
Size:	73.0 KB 
ID:	6100  
  4. #4
    Thank you.

    Please use:
    protected override void OnInit(EventArgs e)
    {
        X.ControlsScripting = false;
        cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1" });
        cb.ActiveItemIndex = 1;
        X.ControlsScripting = true;
    }
    or
    protected override void OnInit(EventArgs e)
    {
        cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1", Checked = true });
    }
    Also I would avoid rendering a Window into a Container. In any way, a Window doesn't participate in a Container's layout and rendered to a body.

    The action can look like this.
    public ActionResult Test()
    {
        return this.PartialExtView("Test");
    }
    Last edited by Daniil; Apr 25, 2013 at 8:16 AM.
  5. #5
    It works, thank you Daniil.
    Before closing this thread, can you explain the what's "X.ControlsScripting"? and In what scenario I should apply this?
  6. #6
    In this particular scenario it might be a bug. I mean the necessity to use X.ControlScripting. Though we are still investigating.

    These things
    X.ControlScripting = false;
    or
    control.SuspendScripting();
    might be helpful when you are unwilling that controls generate JavaScript on updating its properties.

    Please note that generate JavaScript on updating its properties automatically during an AJAX request (DirectEvent or DirectMethod).

    The most common scenario of using suspending of scripting is re-rendering the controls.

    For example, you have a container with some controls like:
    <ext:TextField ID="TextField1" runat="server>
    You would like to re-render that Container applying new settings (a bunch of them including the ones which doesn't support changing on the fly). As well as changing the TextField's Text.
    this.TextField1.Text = "new text";
    Without script suspending it will generate:
    App.TextField1.setValue("new text")
    but it is not required since you are rerendering the Container which contains the TextField.

    Suspending of scripting can also be helpful in some other scenarios where we (Ext.NET sources) have no chance to determine we should generate script or not. Like in your scenario.

    By the way, this looks much better for your scenario, doesn't?
    protected override void OnInit(EventArgs e)
    {
        cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1", Checked = true });
    }
  7. #7
    We committed the fix to SVN. Now your initial code should work fine.
    <script runat="server">
        protected override void OnInit(EventArgs e)
        {
            cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1" });
            cb.ActiveItemIndex = 1;
        }
    </script>
    But suspending of scripting is still worth to use in your scenario.
    X.ControlsScripting = false;
    cb.Menu[0].Items.Add(new CheckMenuItem() { Text = "1" });
    cb.ActiveItemIndex = 1;
    X.ControlsScripting = true;

Similar Threads

  1. Replies: 1
    Last Post: Feb 02, 2012, 5:56 PM
  2. [CLOSED] CycleButton active item
    By smart+ in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Apr 01, 2011, 12:20 PM
  3. Replies: 2
    Last Post: Apr 25, 2010, 2:15 AM
  4. Replies: 5
    Last Post: Mar 05, 2010, 5:08 AM
  5. [CLOSED] active item bug with multiple gridpanels
    By LeeTheGreek in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Dec 06, 2009, 1:51 PM

Posting Permissions