WebControl 的触发器方法未触发
这是我的场景:
我有一个带有占位符的页面。 该页面动态地向此占位符添加了不同类型的控件(我们不想显示一些数据 - 通过查询字符串或回发获取 id,因为我们还有一棵树)。
添加的控件或多或少都包含一个文本框(显示元素的名称)、复选框(显示元素的活动状态)和一个保存按钮,该按钮触发此 Web 控件内的方法。
现在我的问题非常明显: 当我动态添加控件时(并且对于每个条件:!回发和回发),如此添加的控件内的保存方法不会触发 - 无论我做什么......
我只是太愚蠢了诀窍:)
一些幕后信息(工作流程):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.SelectedElement = SomeMagicMethod();
}
}
protected void NodeSelected(object sender, TreeViewNodeEventArgs e)
{
this.SelectedElement = SomeOtherMagicMethod();
}
protected override void OnLoadComplete(EventArgs e)
{
// we have to take this life-cycle!
if (this.SelectedElement!= null)
{
this.DisplayElement();
}
}
private void DisplayElement()
{
var UC = this.LoadControl(UCPath) as DataTypeUC;
if (UC == null)
{
return;
}
UC.ID = EditCampaignFolderUCID;
UC.SetData(this.SelectedElement);
UC.DataBind();
this.phContent.Controls.Add(UC);
}
this is my scenario:
i have a page with a placeholder. the page adds dynimcally different kinds of controls (we wan't to display some data - getting the id via querystring or postback, as we also have a tree) to this placeholder.
the added controls all, more or less, contain a textbox (name of the displaying element), checkbox (active-state of the displaying element) and a save-button which fires a method inside this webcontrol.
now my problem is really obvious:
as i'm adding the control dynamically (and for every condition: !Postback and Postback), the save-method inside the so added control, won't fire - regardless what i do ...
i'm simply to stupid to get the trick :)
some behind-the-scene-infos (workflow):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
this.SelectedElement = SomeMagicMethod();
}
}
protected void NodeSelected(object sender, TreeViewNodeEventArgs e)
{
this.SelectedElement = SomeOtherMagicMethod();
}
protected override void OnLoadComplete(EventArgs e)
{
// we have to take this life-cycle!
if (this.SelectedElement!= null)
{
this.DisplayElement();
}
}
private void DisplayElement()
{
var UC = this.LoadControl(UCPath) as DataTypeUC;
if (UC == null)
{
return;
}
UC.ID = EditCampaignFolderUCID;
UC.SetData(this.SelectedElement);
UC.DataBind();
this.phContent.Controls.Add(UC);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态添加控件时,您还必须每次都连接保存按钮的事件(即,如果保存按钮是动态生成的)。
While adding controls dynamically, you'll also have to wire up the event of the save button everytime (that is, if the save button is being generated dynamically).
技巧是:恢复 oninit 中的所有内容,并按照上次请求时保存视图状态的顺序......棘手棘手
the trick is: reinstatiate everything in oninit, and in the order as the viewstate was saved at the last request ... tricky tricky