[CLOSED] how to get a button in the window which is created in codebehind?

  1. #1

    [CLOSED] how to get a button in the window which is created in codebehind?

     public Ext.Net.Window CreateLoginWindow()
            {
                ...
                 var btnsubmit = new Ext.Net.Button { Text = "登录", ID = "btn_submit" };
                ...
                return window;
            }
           var loginW = _userService.CreateLoginWindow();
           // X.GetCmp<Ext.Net.Button>("btn_submit").DirectEvents.Click.Event += Login;   //here how to get the button "btn_submit" in the window "loginW";
    Last edited by Daniil; Aug 22, 2014 at 11:14 AM. Reason: [CLOSED]
  2. #2
    Hello,

    The sample you provided is insufficient for us test and debug with.
    Geoffrey McGill
    Founder
  3. #3
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="info.hdsoso.com.test.WebForm3" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script runat="server">
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var loginW = CreateLoginWindow();
                    // X.GetCmp<Ext.Net.Button>("btn_submit").DirectEvents.Click.Event += Login;   //here how to get the button "btn_submit" in the window "loginW";
                    this.Form.Controls.Add(loginW);
                }
            }
    
            public Ext.Net.Window CreateLoginWindow()
            {
                var window = new Ext.Net.Window
                {
                    Title = "用户登录",
                    ID = "w_login",
                    Width = 300,
                    Modal = true,
                    Closable = false,
                    IDMode = IDMode.Static
                };
                var fp = new Ext.Net.FormPanel
                {
                    Padding = 4,
                    Layout = LayoutType.Anchor.ToString(),
                    ID = "fp_1"
                };
                var btnreset = new Ext.Net.Button { Text = "取消", ID = "btn_reset" };
                btnreset.Listeners.Click.Handler = "#{fp_1}.reset();";
                var btnsubmit = new Ext.Net.Button { Text = "登录", ID = "btn_submit" };
                //  btnsubmit.DirectEvents.Click.Event += Login;
                fp.Buttons.Add(btnreset);
                fp.Buttons.Add(btnsubmit);
                var tfName = new TextField
                {
                    ID = "tf_uname",
                    FieldLabel = "用户名",
                    AnchorHorizontal = "100%",
                    Margin = 4,
                    LabelWidth = 70
                };
                var tfPass = new TextField
                {
                    ID = "tf_pass",
                    FieldLabel = "密码",
                    AnchorHorizontal = "100%",
                    InputType = InputType.Password,
                    Margin = 4,
                    LabelWidth = 70
                };
                fp.Items.Add(tfName);
                fp.Items.Add(tfPass);
                var fc = new FieldContainer
                {
                    AnchorHorizontal = "100%",
                    Margin = 4,
                    LabelWidth = 70,
                    FieldLabel = "验证码"
                };
                fc.LayoutConfig.Add(new HBoxLayoutConfig { Align = HBoxAlign.Middle });
                var rand = new Random().Next(100000, 999999).ToString();
                var dfYzm = new Ext.Net.TextField()
                {
                    ID = "tf_yzm",
                    Text = rand,
                    Value = rand,
                    ReadOnly = true
                };
    
                fc.Items.Add(dfYzm);
                fc.Items.Add(new Component { Flex = 1 });
                var btnfresh = new Ext.Net.Button { ID = "btn_refresh", Text = "刷新" };
              //  btnfresh.DirectEvents.Click.Event += NewYzm;
                fc.Items.Add(btnfresh);
                fp.Items.Add(fc);
                fp.Items.Add(new TextField
                {
                    FieldLabel = "输入验证码",
                    AnchorHorizontal = "100%",
                    Margin = 4,
                    LabelWidth = 70,
                    ID = "tf_yzm1"
                });
                var notlogin = new FieldContainer()
                {
                    FieldLabel = "登录不上?",
                    AnchorHorizontal = "100%",
                    Margin = 4,
                    LabelWidth = 70
    
                };
                notlogin.Items.Add(new Ext.Net.HyperLink()
                {
                    NavigateUrl = "fenlei_reg.aspx",
                    Text = "注册",
                    Margin = 4
                });
                notlogin.Items.Add(new Ext.Net.HyperLink()
                {
                    NavigateUrl = "fenlei_index.aspx",
                    Text = "返回首页",
                    Margin = 4
                });
                fp.Items.Add(
                 notlogin
                );
                window.Items.Add(fp);
                return window;
            }
    
            private void Login(object sender, DirectEventArgs e)
            {
                X.Msg.Alert("info", "login").Show();
            }
    
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <ext:ResourceManager runat="server"></ext:ResourceManager>
        </form>
    </body>
    </html>
  4. #4
    Hi @hdsoso,

    I see you need a Button control to attach a DirectEvent. X.GetCmp cannot help in such the scenario.

    Actually, there is no way to get a control instance created in another request unless you recreate a Button control entirely. Recreating it each request you could use a DirectEvent.

    Though, I would suggest to use a combination of a client side Listener and a DirectMethod instead of a DirectEvent.
  5. #5
    I see you need a Button control to attach a DirectEvent. X.GetCmp cannot help in such the scenario.

    Actually, there is no way to get a control instance created in another request unless you recreate a Button control entirely. Recreating it each request you could use a DirectEvent.

    Though, I would suggest to use a combination of a client side Listener and a DirectMethod instead of a DirectEvent.
    is it in another request? when page is load , i create a window , then get the button control in the window , is it not the same request?
  6. #6
    Well, at first time it is the same request. But when you click the Button and the DirectEvent fires - it is another request. And the Exception will be thrown, because on no Button control.

    Although, to answer your exact question:
    var loginW = CreateLoginWindow();                
    this.Form.Controls.Add(loginW);
    
    var btn = Ext.Net.Utilities.ControlUtils.FindChildControl<Ext.Net.Button>(loginW, "btn_submit");
    btn.DirectEvents.Click.Event += Login;
    Last edited by Daniil; Aug 18, 2014 at 2:53 PM.

Similar Threads

  1. [CLOSED] gridpanel created by codebehind.how can get it's id?
    By CPA1158139 in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Feb 10, 2014, 1:59 PM
  2. Replies: 0
    Last Post: Aug 01, 2012, 1:20 AM
  3. Replies: 4
    Last Post: Mar 16, 2012, 11:40 AM
  4. [CLOSED] Creating ext:Window in CodeBehind.Button aligment
    By supera in forum 2.x Legacy Premium Help
    Replies: 3
    Last Post: Mar 09, 2012, 10:13 AM
  5. [CLOSED] Add button to dinamically created window toolbar
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: Oct 13, 2011, 4:07 PM

Posting Permissions