PDA

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



hdsoso
Aug 17, 2014, 4:32 AM
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";

geoffrey.mcgill
Aug 17, 2014, 5:48 AM
Hello,

The sample you provided is insufficient for us test and debug with.

hdsoso
Aug 17, 2014, 8:18 AM
<%@ 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>

Daniil
Aug 18, 2014, 1:56 PM
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.

hdsoso
Aug 18, 2014, 2:35 PM
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?

Daniil
Aug 18, 2014, 2:50 PM
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;