动态控件和Page_Init [英] Dynamic Controls and Page_Init

查看:75
本文介绍了动态控件和Page_Init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



你好,你们所有的天才,


我在初始化阶段试图加载动态控件时遇到问题。 />

我已经阅读了文档,我认为我已经弄明白了:

< [+]> - < [+]> - 百分比抑制率数据+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < ; [+]> - < [+]> - < [+]> - < [+]>

< [+]> - < [+ ]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]> - < [+]>

.NET Framework类库


Control.Init事件[C#]


C#

公共事件EventHandler Init;


事件数据

事件处理程序接收类型为EventArgs的参数。


备注

服务器控件应执行任何初始化需要的步骤

来创建和设置实例。您不能在此活动中使用视图状态信息

;它还没有填充。在此事件期间,您不应该访问另一个

服务器控件,无论它是否是该控件的子级或

父级。其他服务器控件不一定要创建

并准备好访问。


示例

[C#]以下示例分配一个自定义事件处理程序,Control_Init,

到控件的init事件,myControl,当在包含控件的页面上调用Page_Init方法时。$ br / b >

[C#]

void Page_Init(对象发件人,EventArgs e)

{

//添加一个Init的事件处理程序。

myControl.Init + = new System.EventHandler(Control_Init);

}


void Control_Init(对象发送者,EventArgs e)

{

Response.Write("最初对象的ID:" + myControl.ID);

//更改ID属性。

myControl.ID =" TestControl";

Response.Write("< br>已更改ID:" + myControl.ID);

}


? 2001-2002微软公司。保留所有权利。


< [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - 百分比抑制率数据+ ]> - < [+]>

< [+]> - < [+]> - < [+]> - < [+]> - 百分比抑制率数据+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < ; [+]> - < [+]>


我不明白的一件事是:myControl在哪里?来自

给出的例子?


在我自己的努力中,我试图将一个文字控件附加到占位符:

< [+]> - < [+]> [< [+]> - < [+]> [< [+]> - < [+]> - < ; [+]> - < [+]> - < [+]> [< [+]> - < [+]> [< [+]>

< [+]> - < [+]> - < [+]>< [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>


void Page_Init(对象发送者,EventArgs e)

{

//为''Init'添加事件处理程序。

// myControl.Init + = new System.EventHandler(Control_Init);

PlaceHolder plc_hldr =(PlaceHolder)FindControl(" Dyn_Control_Placeholder");

plc_hldr.Init + = new System.EventHandler(plc_hldr_Control_Init);

}

/ * --------------- -------------------------------------------------- - * /

/ * ------------------------------------- ----------------------------- * /

void plc_hldr_Control_Init(object sender,EventArgs e)

{

Label lbl_msg01 =(Label)FindControl(" Message01");


Dyn_Control_Placeholder.Controls.Add

(新的LiteralControl(< br>在init< br>"上添加1));

PlaceHolder plc_hldr =(PlaceHolder)FindControl(" Dyn_Control_Placeholder");


lbl_msg01.Text ="对象的ID最初:" + plc_hldr.ID;

Response.Write("最初对象的ID:" + plc_hldr.ID);


plc_hldr.Controls。添加(新的LiteralControl(< br>在init< br>"上添加2));

}

/ * -------- -------------------------------------------------- -------- * /

/ * ------------------------------ ------------------------------------ * /


< [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> [< [+]> - < [+]>

< [+]>< [+]> - < [+]>< [+]> - < [+]> [< [+]> - 百分比抑制率数据+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> <无线电通信$>

......什么都没发生 - 文字控件不在那里。


我希望比我更聪明的人能让我知道在这个

- 我将非常感激。


我的完整代码如下:

< [+]> ; - < [+]& GT; - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>

< [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - 百分比抑制率数据+ ]> - < [+]> - < [+]> - < [+]> - < [+]>

<%@ Page Language = " C#" AutoEventWireup = QUOT;真" %>


< html>

< head>


< script runat ="服务器">


/ * -------------------------------- ---------------------------------- * /

/ * ---- -------------------------------------------------- ------------ * /


/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - * /

public class sys_obj_class

{

private ControlCollection pram_ph01_ctrl;

public ControlCollection Get_pram_ph01_ctrl

{get {return pram_ph01_ctrl; } $

private Page this_page01;

public Page Get_this_page01

{get {return this_page01; } $

私人PlaceHolder Obj_Dyn_Ctrl_Placeholder;

public PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder

{get {return Obj_Dyn_Ctrl_Placeholder; } $

private Label Obj_Dyn_Label01;

public Label Get_Obj_Dyn_Label01

{get {return Obj_Dyn_Label01; } $

public sys_obj_class

(PlaceHolder Dyn_Ctrl_Placeholder,Label Dyn_Label)

{pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;

this_page01 = Dyn_Ctrl_Placeholder.Page;

Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;

Obj_Dyn_Label01 = Dyn_Label;

}

}


/ * -------------------------------------- ---------------------------- * /

/ * ---------- -------------------------------------------------- ------ * /

void Page_Init(对象发送者,EventArgs e)

{

//添加事件处理程序' 'init''。

// myControl.Init + = new System.EventHandler(Control_Init);

PlaceHolder plc_hldr =(PlaceHolder)FindControl(" Dyn_Control_Placeholder");

plc_hldr.Init + = new System.EventHandler(plc_hldr_Control_Init);

}

/ * ---------- -------------------------------------------------- ------ * /

/ * - -------------------------------------------------- -------------- * /

void plc_hldr_Control_Init(object sender,EventArgs e)

{

标签lbl_msg01 =(标签)FindControl(" Message01");

PlaceHolder plc_hldr =(PlaceHolder)FindControl(" Dyn_Control_Placeholder");


lbl_msg01。 Text ="最初对象的ID: + plc_hldr.ID;

Response.Write("最初对象的ID:" + plc_hldr.ID);


plc_hldr.Controls。添加(新的LiteralControl("< br>在init< br>"上添加));

}

/ * --------- -------------------------------------------------- ------- * /

/ * ------------------------------- ----------------------------------- * /

protected void Page_Load(Object发件人,EventArgs e)

{

PlaceHolder plc_hldr =(PlaceHolder)FindControl(" Dyn_Control_Placeholder");

Label lbl_msg01 =(Label) FindControl(" Message01");

sys_obj_class sys_obj = new sys_obj_class(plc_hldr,lbl_msg01);


ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01(sys_obj);


ccp1.create_ctrl_pair_01();

}

/ * --------------- -------------------------------------------------- - * /

/ * ------------------------------------ ------------------------------ * /


/ * - - - - - - - - - - - - - - - - - - - - - - * /

/ * - - - - - - - - - - - - - - - - - - - - - - * /

protected class base_create_ctrl_pair

{

protected TextBox UserTextBox0x;

protected Button button0x;

protected ControlCollection ph01_ctrl;

protected PlaceHolder base_Dyn_Ctrl_Placeholder;

protected Label Dyn_Lbl_msg01;


// constructor

protected base_create_ctrl_pair(sys_obj_class sys_obj)

{ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;

base_Dyn_Ctrl_Placeholder = sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;

Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;

}


protected TextBox create_text_box_control(String text_box_id)

{//创建UserTextBox TextBox控件。

TextBox new_TextBox = new TextBox();


//配置UserTextBox TextBo x控制。

new_TextBox.ID = text_box_id;


//将UserTextBox TextBox控件添加到Controls集合

// of Dyn_Control_Placeholder PlaceHolder控件。

ph01_ctrl.Add(new_TextBox);


返回new_TextBox;

}


/ * ------------------------------------------ --------------- *

示例:以下示例创建一个Button,

将其DialogResult属性设置为DialogResult.OK,

并将其添加到表格中。

* -------------------------- ------------------------------- * /

protected void InitializeMyButton(String Btn_ID,String Btn_text )

{//创建并初始化一个按钮。

button0x = new Button();

button0x.ID = Btn_ID;

button0x.Text = Btn_text;


ph01_ctrl.Add(new LiteralControl("< br>< br>"));

ph01_ctrl.Add(button0x);

}


protected void init_single_line_text_box(TextBox new_TextBox,int col_sz)

{new_TextBox.Columns = col_sz; }


protected void create_ctrl_pair_0x

(String TB_ID,String DB_ID,String DB_txt)

{//创建UserTextBox TextBox控件。

UserTextBox0x = create_text_box_control(TB_ID);

InitializeMyButton(DB_ID,DB_txt);

}


/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - * /

private String submit_click_str01 =" The TextBox";


private String submit_click_str02

="上面的控制是动态生成的。 <峰; br>您输入了:" ;;

public void Submit_Click(Object sender,EventArgs e)

{//从

中检索UserTextBox TextBox控件Dyn_Control_Placeholder

// PlaceHolder控件。

TextBox TempTextBox =(TextBox)

base_Dyn_Ctrl_Placeholder.FindControl(" UserTextBox 1");


//显示Text属性。

Dyn_Lbl_msg01.Text = submit_click_str01 +''1''+ submit_click_str02 +

TempTextBox.Text;

}

/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - * /


}

/ * - - - - - - - - - - - - - - - - - - - - - - * /

/ * - - - - - - - - - - - - - - - - - - - - - - * /

protected class ctrl_pair_creation_01:base_create_ctrl_pair

{

private sys_obj_class lcl_sys_obj ;


//构造函数

public ctrl_pair_creation_01(sys_obj_class sys_obj):base(sys_obj)

{lcl_sys_obj = sys_obj; }


/ *。 。 。 。 。 。 。 。 。 。 。 。 。 * /

public void create_ctrl_pair_01()

{create_ctrl_pair_0x

(" UserTextBox1"," DButton01"," Submit 01") ;


button0x.Click + = new EventHandler(Submit_Click);


if(!lcl_sys_obj.Get_this_page01.IsPostBack)

init_single_line_text_box(UserTextBox0x,22);


ph01_ctrl.Add(new LiteralControl("< br>< br>"));

}

/ *。 。 。 。 。 。 。 。 。 。 。 。 。 * /

}

/ * - - - - - - - - - - - - - - - - - - - - - - * /

/ * - - - - - - - - - - - - - - - - - - - - - - * /

/ * - - - - - - - - - - - - - - - - - - - - - - * /


< / script>

< / head>

< body>


< form runat =" server">


< h3>控制初始化< / h3>


输入一些文本并单击提交按钮。 < br>< br>

< asp:PlaceHolder ID =" Dyn_Control_Placeholder" runat =" server" />


< asp:Label ID =" Message01" runat =" server" />


< / form>


< / body>

< / html>


< [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>

< [+]> - < [+]> - < [+]> - < [+] > - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>

- wASP


Hello again to all of you geniuses,

I''m having a problem trying to load dynamic controls at the initialization phase.

I''ve read the docs, and I thought I had it figured out:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
.NET Framework Class Library

Control.Init Event [C#]

C#
public event EventHandler Init;

Event Data
The event handler receives an argument of type EventArgs.

Remarks
Server controls should perform any initialization steps that are required
to create and set up an instance. You cannot use view-state information
within this event; it is not populated yet. You should not access another
server control during this event, regardless of whether it is a child or
parent to this control. Other server controls are not certain to be created
and ready for access.

Example
[C#] The following example assigns a custom event handler, Control_Init,
to the Init event of a control, myControl, when the Page_Init method is
called on the page that contains the control.

[C#]
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender,EventArgs e)
{
Response.Write("The ID of the object initially : " + myControl.ID);
// Change the ID property.
myControl.ID="TestControl";
Response.Write("<br>The changed ID : " + myControl.ID);
}

? 2001-2002 Microsoft Corporation. All rights reserved.

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

One thing I don''t understand is: Where did "myControl" come from in the
given example?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

... and nothing happened - the literal control was not there.

I''m hoping that someone smarter than me can clue me in on this
- I would be most grateful.

My complete code is as follows:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
public class sys_obj_class
{
private ControlCollection pram_ph01_ctrl;
public ControlCollection Get_pram_ph01_ctrl
{ get { return pram_ph01_ctrl; } }

private Page this_page01;
public Page Get_this_page01
{ get { return this_page01; } }

private PlaceHolder Obj_Dyn_Ctrl_Placeholder;
public PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder
{ get { return Obj_Dyn_Ctrl_Placeholder; } }

private Label Obj_Dyn_Label01;
public Label Get_Obj_Dyn_Label01
{ get { return Obj_Dyn_Label01; } }
public sys_obj_class
(PlaceHolder Dyn_Ctrl_Placeholder, Label Dyn_Label)
{ pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load (Object sender, EventArgs e)
{
PlaceHolder plc_hldr = (PlaceHolder) FindControl ("Dyn_Control_Placeholder");
Label lbl_msg01 = (Label) FindControl ("Message01");
sys_obj_class sys_obj = new sys_obj_class (plc_hldr, lbl_msg01);

ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01 (sys_obj);

ccp1.create_ctrl_pair_01 ();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;
protected PlaceHolder base_Dyn_Ctrl_Placeholder;
protected Label Dyn_Lbl_msg01;

// constructor
protected base_create_ctrl_pair (sys_obj_class sys_obj)
{ ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;
base_Dyn_Ctrl_Placeholder = sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}

protected TextBox create_text_box_control (String text_box_id)
{ // Create UserTextBox TextBox control.
TextBox new_TextBox = new TextBox ();

// Configure the UserTextBox TextBox control.
new_TextBox.ID = text_box_id;

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}

/* --------------------------------------------------------- *
Example: The following example creates a Button,
sets its DialogResult property to DialogResult.OK,
and adds it to a Form.
* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
ph01_ctrl.Add (button0x);
}

protected void init_single_line_text_box (TextBox new_TextBox, int col_sz)
{ new_TextBox.Columns = col_sz; }

protected void create_ctrl_pair_0x
(String TB_ID, String DB_ID, String DB_txt)
{ // Create UserTextBox TextBox control.
UserTextBox0x = create_text_box_control (TB_ID);
InitializeMyButton(DB_ID, DB_txt);
}

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */
private String submit_click_str01 = "The TextBox";

private String submit_click_str02
= " control above is dynamically generated. <br> You entered: ";
public void Submit_Click (Object sender, EventArgs e)
{ // Retrieve the UserTextBox TextBox control from the
Dyn_Control_Placeholder
// PlaceHolder control.
TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 1");

// Display the Text property.
Dyn_Lbl_msg01.Text = submit_click_str01 + ''1'' + submit_click_str02 +
TempTextBox.Text;
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- */

}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_01 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;

// constructor
public ctrl_pair_creation_01 (sys_obj_class sys_obj) : base (sys_obj)
{ lcl_sys_obj = sys_obj; }

/* . . . . . . . . . . . . . */
public void create_ctrl_pair_01 ()
{ create_ctrl_pair_0x
("UserTextBox1", "DButton01", "Submit 01");

button0x.Click += new EventHandler (Submit_Click);

if (!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

</script>
</head>

<body>

<form runat="server">

<h3> Control Init </h3>

Enter some text and click the Submit button. <br><br>

<asp:PlaceHolder ID="Dyn_Control_Placeholder" runat="server"/>

<asp:Label ID="Message01" runat="server"/>

</form>

</body>
</html>

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
- wASP

推荐答案

这是一个动态向控件添加事件处理程序的示例。他们

假设控件已经存在。


" wASP" < wylbur [在] EV1 [点]净>在消息中写道

news:r6 ******************************** @ 4ax.com ...
This is an example of dynamically adding an event handler to a control. They
are assuming the control is already there.

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:r6********************************@4ax.com...

大家好再次告诉我们,

我在初始化时尝试加载动态控件时遇到问题
阶段。

我已经阅读了文档,我认为我已经弄明白了:
< [+]> - < [+]> - < [ +]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+] > - < [+]> - < [+]> - < [+]>
< [+]> - < [+]> - < [+ ]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]>
.NET Framework类库

Control.Init事件[C#]

C#
公共事件EventHandler Init;

事件数据
事件处理程序接收EventArgs类型的参数。

备注
服务器控件应执行创建和设置实例所需的任何初始化步骤。您不能在此活动中使用视图状态信息;它还没有填充。在此事件期间,您不应访问另一个服务器控件,无论它是该控件的子级还是
父级。其他服务器控件不一定会被创建
并准备好访问。

示例
[C#]以下示例分配自定义事件处理程序Control_Init,<当在包含控件的页面上调用Page_Init方法时,控件myControl的Init事件。

[C#]
void Page_Init(对象) sender,EventArgs e)
//为''Init'添加事件处理程序。
myControl.Init + = new System.EventHandler(Control_Init);
} <
void Control_Init(对象发送者,EventArgs e)
; Response.Write("最初对象的ID:" + myControl.ID);
//更改ID属性。
myControl.ID =" TestControl";
Response.Write("< br>已更改的ID:" + myControl.ID);
}

? 2001-2002微软公司。保留所有权利。

< [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>
< [+]> - < [+]>< [+]> - < [+]> [< [+]> - 百分比抑制率数据+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < ; [+]>

我不明白的一件事是:myControl在哪里?来自
给出的例子?

在我自己的努力中,我试图将一个文字控件附加到占位符:
< [+]> - < [ +]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+] > - < [+]> - < [+]> - < [+]>< [+]>
< [+]> - < [+ ]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]> - < [+]>

void Page_Init(对象发件人,EventArgs e)
{
//为''Init'添加一个事件处理程序。
// myControl.Init + = new System.EventHandler(Control_Init);
PlaceHolder plc_hldr =(PlaceHolder )FindControl
(Dyn_Control_Placeholder);
plc_hldr.Init + = new System.EventHandler(plc_hldr_Control_Init);
}
/ * --------- -------------------------------------------------- ------- * /
/ * ----------------------------------- ------------------------------- * /
void plc_hldr_Control_Init(对象发送者, EventArgs e)
{
标签lbl_msg01 =(标签)FindControl(" Message01");

Dyn_Control_Placeholder.Controls.Add
(新的LiteralControl("< ; br>在init< br>")上添加1);

PlaceHolder plc_hldr =(PlaceHolder)FindControl
(Dyn_Control_Placeholder);

lbl_msg01 .Text ="最初对象的ID: + plc_hldr.ID;
Response.Write("最初对象的ID:" + plc_hldr.ID);

plc_hldr.Controls.Add(new LiteralControl("< ; br>在
init< br>")上添加了2);
}
/ * -------------------- ---------------------------------------------- * /
/ * ---------------------------------------------- -------------------- * /

< [+]> - < [+]> - < [+ ]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]>
< [+]> - < [+]> - < [+] > - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]>

......什么都没发生 - 文字控件不存在。

我希望比我更聪明的人可以在这上面了解我的意思 - 我将非常感激。

我的完整代码如下:
< [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> ; - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> < br< [+]> - < [+]> - < [+]> [< [+]> - < [+]> [< [+]> - 百分比抑制率数据+]> - < [+]> - < [+]> - < [+]> - < [+]> - < [+]> <无线电通信/><%@ Page Language =" C#" AutoEventWireup = QUOT;真" %>

< html>
< head>

< script runat =" server">

/ * ------------------------------------------------ ------------------ * /
/ * ------------------------ ------------------------------------------ * /

/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -
* /
公共类sys_obj_class
私有ControlCollection pram_ph01_ctrl;
公开ControlCollection Get_pram_ph01_ctrl
{get {return pram_ph01_ctrl;私人页面this_page01;
公共页面Get_this_page01
{get {return this_page01;私有PlaceHolder Obj_Dyn_Ctrl_Placeholder;
公共PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder
{get {return Obj_Dyn_Ctrl_Placeholder;私有标签Obj_Dyn_Label01;
公共标签Get_Obj_Dyn_Label01
{get {return Obj_Dyn_Label01; } $

public sys_obj_class
(PlaceHolder Dyn_Ctrl_Placeholder,Label Dyn_Label)
{pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/ * -------------------- ---------------------------------------------- * /
/ * ---------------------------------------------- -------------------- * /
void Page_Init(object sender,EventArgs e)
//
//添加事件处理程序对于''Init''。
// myControl.Init + = new System.EventHandler(Control_Init);
PlaceHolder plc_hldr =(PlaceHolder)FindControl
(Dyn_Control_Placeholder);
plc_hldr.Init + = new System.EventHandler(plc_hldr_Control_Init);
}
/ * ------------------------ ------------------------------------ ------ * /
/ * ------------------------------------ ------------------------------ * /
void plc_hldr_Control_Init(object sender,EventArgs e)
{
标签lbl_msg01 =(标签)FindControl(" Message01");
PlaceHolder plc_hldr =(PlaceHolder)FindControl
(" Dyn_Control_Placeholder");

lbl_msg01。 Text ="最初对象的ID: + plc_hldr.ID;
Response.Write("最初对象的ID:" + plc_hldr.ID);

plc_hldr.Controls.Add(new LiteralControl("< ; br>在init< br>")上添加;)
}
/ * ------------------------- ----------------------------------------- * /
/ * - -------------------------------------------------- --------------- * /
protected void Page_Load(Object sender,EventArgs e)
{/> PlaceHolder plc_hldr =(PlaceHolder)FindControl (" Dyn_Control_Placeholder");
标签lbl_msg01 =(标签)FindControl(" Message01");
sys_obj_class sys_obj = new sys_obj_class(plc_hldr,lbl_msg01);

ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01(sys_obj);

ccp1.create_ctrl_pair_01();
}
/ * ----------------- ------------------------------------------------- * /
/ * ------------------------------------------ ------------------------ * /

/ * - - - - - - - - - - - - - - - - - - - - - - * /
/ * - - - - - - - - - - - - - - - - - - - - - - * /
受保护的类base_create_ctrl_pair {
受保护的TextBox UserTextBox0x;
受保护的按钮button0x;
受保护的ControlCollection ph01_ctrl;
受保护的PlaceHolder base_Dyn_Ctrl_Placeholder;
受保护的标签Dyn_Lbl_msg01;

//构造函数
protected base_create_ctrl_pair(sys_obj_class sys_obj)
{ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;
base_Dyn_Ctrl_Placeholder =
sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}

受保护的TextBox create_text_box_control(String text_box_id)
{//创建UserTextBox TextBox控件。
TextBox new_TextBox = new T extBox();

//配置UserTextBox TextBox控件。
new_TextBox.ID = text_box_id;

//将UserTextBox TextBox控件添加到Controls集合中/> // Dyn_Control_Placeholder PlaceHolder控件。
ph01_ctrl.Add(new_TextBox);

返回new_TextBox;
}

/ * --- -------------------------------------------------- ---- *
示例:以下示例创建一个Button,
将其DialogResult属性设置为DialogResult.OK,
并将其添加到表单中。
* --- -------------------------------------------------- ---- * /
protected void InitializeMyButton(String Btn_ID,String
Btn_text)
{//创建并初始化一个Button。
button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

ph01_ctrl.Add(new LiteralControl("< br>< br>"));
ph01_ctrl.Add(button0x);
}

protected void init_single_line_text_box(TextBox new_TextBox,
int col_sz)
{new_TextBox.Columns = col_sz; }

protected void create_ctrl_pair_0x
(String TB_ID,String DB_ID,String DB_txt)
{//创建UserTextBox TextBox控件。
UserTextBox0x = create_text_box_control(TB_ID);
InitializeMyButton(DB_ID,DB_txt);
}

/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -
* /
private String submit_click_str01 =" The TextBox";

private String submit_click_str02
="上面的控制是动态生成的。 <峰; br>您输入了:" ;;

public void Submit_Click(Object sender,EventArgs e)
{//从Dyn_Control_Placeholder中检索UserTextBox TextBox控件 // PlaceHolder控件。
TextBox TempTextBox =(TextBox)
base_Dyn_Ctrl_Placeholder.FindControl(" UserTextBox 1");

//显示Text属性。
Dyn_Lbl_msg01.Text = submit_click_str01 +''1''+
submit_click_str02 +
TempTextBox.Text;
}

/ * - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + -
* /

}
/ * - - - - - - - - - - - - - - - - - - - - - - * /
/ * - - - - - - - - - - - - - - - - - - - - - - * /
受保护的类ctrl_pair_creation_01:base_create_ctrl_pair
{
私人sys_obj_class lcl_sys_obj;

//构造函数
pub lic ctrl_pair_creation_01(sys_obj_class sys_obj):base
(sys_obj)
{lcl_sys_obj = sys_obj; }

/ *。 。 。 。 。 。 。 。 。 。 。 。 。 * /
public void create_ctrl_pair_01()
{create_ctrl_pair_0x
(" UserTextBox1"," DButton01"," Submit 01");

button0x.Click + = new EventHandler(Submit_Click);

if(!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box(UserTextBox0x,22);

ph01_ctrl.Add(new LiteralControl (< br>< br>"));
}
/ *。 。 。 。 。 。 。 。 。 。 。 。 。 * /
}
/ * - - - - - - - - - - - - - - - - - - - - - - * /
/ * - - - - - - - - - - - - - - - - - - - - - - * /
/ * - - - - - - - - - - - - - - - - - - - - - - * /

< / script>
< / head>

< body>

< form runat =" server">

< h3>控制初始化< / h3>

输入一些文本并单击提交按钮。 < br>< br>
< asp:PlaceHolder ID =" Dyn_Control_Placeholder" runat =" server" />

< asp:Label ID =" Message01" runat =" server" />

< / form>

< / body>
< / html>

< [+]> - < [+]> - < [+]> [< [+]> - < [+]> - < [+]> - < ; [+]> - < [+]> - < [+]> [< [+]> - < [+]> [< [+]>

- wASP

Hello again to all of you geniuses,

I''m having a problem trying to load dynamic controls at the initialization
phase.

I''ve read the docs, and I thought I had it figured out:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
.NET Framework Class Library

Control.Init Event [C#]

C#
public event EventHandler Init;

Event Data
The event handler receives an argument of type EventArgs.

Remarks
Server controls should perform any initialization steps that are required
to create and set up an instance. You cannot use view-state information
within this event; it is not populated yet. You should not access another
server control during this event, regardless of whether it is a child or
parent to this control. Other server controls are not certain to be
created
and ready for access.

Example
[C#] The following example assigns a custom event handler, Control_Init,
to the Init event of a control, myControl, when the Page_Init method is
called on the page that contains the control.

[C#]
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
myControl.Init += new System.EventHandler(Control_Init);
}

void Control_Init(object sender,EventArgs e)
{
Response.Write("The ID of the object initially : " + myControl.ID);
// Change the ID property.
myControl.ID="TestControl";
Response.Write("<br>The changed ID : " + myControl.ID);
}

? 2001-2002 Microsoft Corporation. All rights reserved.

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

One thing I don''t understand is: Where did "myControl" come from in the
given example?

In my own effort, I tried to attach a literal control to a placeholder:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler (plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");

Dyn_Control_Placeholder.Controls.Add
( new LiteralControl("<br>added 1 on init<br>") );
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added 2 on
init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>

... and nothing happened - the literal control was not there.

I''m hoping that someone smarter than me can clue me in on this
- I would be most grateful.

My complete code is as follows:
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/
public class sys_obj_class
{
private ControlCollection pram_ph01_ctrl;
public ControlCollection Get_pram_ph01_ctrl
{ get { return pram_ph01_ctrl; } }

private Page this_page01;
public Page Get_this_page01
{ get { return this_page01; } }

private PlaceHolder Obj_Dyn_Ctrl_Placeholder;
public PlaceHolder Get_Obj_Dyn_Ctrl_Placeholder
{ get { return Obj_Dyn_Ctrl_Placeholder; } }

private Label Obj_Dyn_Label01;
public Label Get_Obj_Dyn_Label01
{ get { return Obj_Dyn_Label01; } }
public sys_obj_class
(PlaceHolder Dyn_Ctrl_Placeholder, Label Dyn_Label)
{ pram_ph01_ctrl = Dyn_Ctrl_Placeholder.Controls;
this_page01 = Dyn_Ctrl_Placeholder.Page;
Obj_Dyn_Ctrl_Placeholder = Dyn_Ctrl_Placeholder;
Obj_Dyn_Label01 = Dyn_Label;
}
}

/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void Page_Init(object sender,EventArgs e)
{
// Add a event Handler for ''Init''.
// myControl.Init += new System.EventHandler(Control_Init);
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
plc_hldr.Init += new System.EventHandler(plc_hldr_Control_Init);
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void plc_hldr_Control_Init (object sender,EventArgs e)
{
Label lbl_msg01 = (Label) FindControl ("Message01");
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");

lbl_msg01.Text = "The ID of the object initially : " + plc_hldr.ID;
Response.Write("The ID of the object initially : " + plc_hldr.ID);

plc_hldr.Controls.Add ( new LiteralControl("<br>added on init<br>") );
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
protected void Page_Load (Object sender, EventArgs e)
{
PlaceHolder plc_hldr = (PlaceHolder) FindControl
("Dyn_Control_Placeholder");
Label lbl_msg01 = (Label) FindControl ("Message01");
sys_obj_class sys_obj = new sys_obj_class (plc_hldr, lbl_msg01);

ctrl_pair_creation_01 ccp1 = new ctrl_pair_creation_01 (sys_obj);

ccp1.create_ctrl_pair_01 ();
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */

/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class base_create_ctrl_pair
{
protected TextBox UserTextBox0x;
protected Button button0x;
protected ControlCollection ph01_ctrl;
protected PlaceHolder base_Dyn_Ctrl_Placeholder;
protected Label Dyn_Lbl_msg01;

// constructor
protected base_create_ctrl_pair (sys_obj_class sys_obj)
{ ph01_ctrl = sys_obj.Get_pram_ph01_ctrl;
base_Dyn_Ctrl_Placeholder =
sys_obj.Get_Obj_Dyn_Ctrl_Placeholder;
Dyn_Lbl_msg01 = sys_obj.Get_Obj_Dyn_Label01;
}

protected TextBox create_text_box_control (String text_box_id)
{ // Create UserTextBox TextBox control.
TextBox new_TextBox = new TextBox ();

// Configure the UserTextBox TextBox control.
new_TextBox.ID = text_box_id;

// Add UserTextBox TextBox control to the Controls collection
// of the Dyn_Control_Placeholder PlaceHolder control.
ph01_ctrl.Add (new_TextBox);

return new_TextBox;
}

/* --------------------------------------------------------- *
Example: The following example creates a Button,
sets its DialogResult property to DialogResult.OK,
and adds it to a Form.
* --------------------------------------------------------- */
protected void InitializeMyButton (String Btn_ID, String
Btn_text)
{ // Create and initialize a Button.
button0x = new Button();
button0x.ID = Btn_ID;
button0x.Text = Btn_text;

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
ph01_ctrl.Add (button0x);
}

protected void init_single_line_text_box (TextBox new_TextBox,
int col_sz)
{ new_TextBox.Columns = col_sz; }

protected void create_ctrl_pair_0x
(String TB_ID, String DB_ID, String DB_txt)
{ // Create UserTextBox TextBox control.
UserTextBox0x = create_text_box_control (TB_ID);
InitializeMyButton(DB_ID, DB_txt);
}
/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/
private String submit_click_str01 = "The TextBox";

private String submit_click_str02
= " control above is dynamically generated. <br> You
entered: ";
public void Submit_Click (Object sender, EventArgs e)
{ // Retrieve the UserTextBox TextBox control from the
Dyn_Control_Placeholder
// PlaceHolder control.
TextBox TempTextBox = (TextBox)
base_Dyn_Ctrl_Placeholder.FindControl("UserTextBox 1");

// Display the Text property.
Dyn_Lbl_msg01.Text = submit_click_str01 + ''1'' +
submit_click_str02 +
TempTextBox.Text;
}

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/

}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
protected class ctrl_pair_creation_01 : base_create_ctrl_pair
{
private sys_obj_class lcl_sys_obj;

// constructor
public ctrl_pair_creation_01 (sys_obj_class sys_obj) : base
(sys_obj)
{ lcl_sys_obj = sys_obj; }

/* . . . . . . . . . . . . . */
public void create_ctrl_pair_01 ()
{ create_ctrl_pair_0x
("UserTextBox1", "DButton01", "Submit 01");

button0x.Click += new EventHandler (Submit_Click);

if (!lcl_sys_obj.Get_this_page01.IsPostBack)
init_single_line_text_box (UserTextBox0x, 22);

ph01_ctrl.Add ( new LiteralControl("<br><br>") );
}
/* . . . . . . . . . . . . . */
}
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - */

</script>
</head>

<body>

<form runat="server">

<h3> Control Init </h3>

Enter some text and click the Submit button. <br><br>

<asp:PlaceHolder ID="Dyn_Control_Placeholder" runat="server"/>

<asp:Label ID="Message01" runat="server"/>

</form>

</body>
</html>

<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>-<[+]>
- wASP



On Thu,2005年7月28日14:16:18 -0400,Marina <所以***** @ nospam.com>写道:
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:
这是一个向控件动态添加事件处理程序的示例。他们假设控制已经在那里。
This is an example of dynamically adding an event handler to a control. They
are assuming the control is already there.




嗨Marina,


好​​的 - 所以我''我试图将一个处理程序添加到尚未实例化的控件中吗?


你能给我一个关于如何添加动态控件的线索

在Page_Load被解雇之前占位符并且在回发时恢复了viewstate




......或者......

>
我如何保存特定控件的视图状态(动态创建)

然后在Page_Load上恢复它? ......这是一个更重要的问题。

你知道任何好的文档/书籍可能会像我一样愚弄吗?


我是新手 - 我有很多需要学习的东西。


- wASP



Hi Marina,

OK - so I''m trying to add a handler to a control that has yet to be instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool like myself?

I''m a newbie on this - and I''ve got a lot to learn.

- wASP


不,此示例中的控件已实例化。它存在,它是在.aspx页面中声明的
,并且在Init运行时它指向一个真正的

对象。


在Init或Load事件中,你将声明一个你想要的类型的变量。你可以创建一个你想要的任何对象的实例,然后将
添加到你的占位符。那么你就是这个对象的处理程序
你刚刚创建的
。如果页面上有viewstate,则控件应该只是
维持其viewstate。你必须在每次回发时都这样做。


" wASP" < wylbur [在] EV1 [点]净>在消息中写道

新闻:i8 ******************************** @ 4ax.com ...
No, the control in this example has been instantiated. It exists, it is
declared in the .aspx page, and by the time Init runs it points to a real
object.

In the Init or Load event, you would declare a variable of the type you
want. You would create an instance of whatever object it is you want, and
then add it to your placeholder. Then you would the handler to this object
you just created. If the page has viewstate on, the control should just
maintain its viewstate. You have to do this on every postback.

"wASP" <wylbur[at]ev1[dot]net> wrote in message
news:i8********************************@4ax.com...
On Thu,2005年7月28日14:16:18 -0400,Marina <所以***** @ nospam.com>写道:
On Thu, 28 Jul 2005 14:16:18 -0400, "Marina" <so*****@nospam.com> wrote:
这是一个向控件动态添加事件处理程序的示例。
他们假设控件已经存在。
This is an example of dynamically adding an event handler to a control.
They
are assuming the control is already there.

<你好Marina,

好的 - 所以我正在尝试为尚未实例化的控件添加一个处理程序?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

.. . or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.

Do you know of any good docs/books that might spell it out for a fool like
myself?

I’’m a newbie on this - and I’’ve got a lot to learn.

- wASP



Hi Marina,

OK - so I''m trying to add a handler to a control that has yet to be
instantiated?

Can you give me a clue as to how would I add a dynamic control
to a placeholder before Page_Load gets fired and the viewstate
is recovered on postback?

... or ...

How could I save the viewstate for a specific control (created
dynamically)
then recover it on Page_Load? ... THAT is a more important question.
Do you know of any good docs/books that might spell it out for a fool like
myself?

I''m a newbie on this - and I''ve got a lot to learn.

- wASP



这篇关于动态控件和Page_Init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆