不能得到的OnInit事件值 [英] Cannot get values in OnInit event

查看:111
本文介绍了不能得到的OnInit事件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,事件与页面生命周期发生的顺序,但它不符合我的情况帮助。我有一个由填充表单目录填充一个的CheckBoxList。当我检查旁边的表格名称的盒子,我想它动态地创建向导步骤,并插入表单。

I understand the order the events occur with page life cycle but it is not helping with my situation. I have a checkboxlist that is populated by a directory filled with forms. When I check a box next to the name of the form I would like it to dynamically create a wizard step and insert the form.

事件顺序:
是OnInit:
GatherForms() - 检查目录,并加载所有表单名称为复选框
LoadForms() - 检查中收集的选定会话和负载形式

Order of events: OnInit: GatherForms() - Checks directory and loads all form names into checkbox LoadForms() - Checks "Selected" Session and loads forms that were collected

的CheckBoxList:的SelectedIndexChanged #AutoPost =真#
PopulateForms() - 遍历checkboxs并将它​​们添加到会话状态

CheckBoxList:SelectedIndexChanged #AutoPost = true# PopulateForms() - Loops through the checkboxs and adds them to session state

当用户点击它回传,并击中其从会话拉OnInit中的复选框。问题是,PopulateForms(),因此它填充,即使它被检查什么也没有跑出。如果我点击其他项目会回传并出现。我不能似乎能,这意味着我不能看到形式立即显示在刷新前拉任何有用的信息从复选框。我也曾尝试循环的复选框,但遗憾的是处理不当视图状态尚未公布。叹了口气。

When the user clicks the checkbox it does a postback and hits the OnInit which pulls from the session. The problem is that PopulateForms() was not ran yet so it populated nothing even though it is checked. If I click another item it will postback and appear. I cannot seem to be able to pull any kind of useful information from the checkbox before the refresh which means I cannot see the forms appear immediately. I have also tried looping the checkbox but unfortunately viewstate hasnt posted yet. sigh.

有什么建议?

谢谢!

P.S:我不能使用的Request.Form []因为我已经把所有的选定项目出来的复选框。也许我可以,但我不能找到一种方法:/

P.S: I cannot use Request.Form[] because I have to get all the selected items out of the checkbox. maybe i can but i cannot find a way :/

推荐答案

这是一个常见的​​问题与我,因为我在ASP.NET得到更好的搏斗。

This is a common problem I wrestle with as I get better at ASP.NET.

使用动态控件你有他们的OnInit的过程中没有实际存在的问题。

With dynamic controls you have the problem of them not actually existing during OnInit.

你可以做的是创造的所有的OnInit的过程中,用户可以看到的控件,并隐藏用户不会在code看到的内容。该页面将加载,所有可能的控件将被实例化(在code - 不要担心,这出现在你的HTML和胃胀的话),那么该事件处理程序会火,然后就可以处理设置的知名度您向导。

What you can do is create all of the controls the user may see during OnInit, and hide the elements the user won't see in code. The page will load, all possible controls will be instantiated (in code - don't worry this appearing in your HTML and bloating it), then the event handler will fire, and then you can deal with setting the visibility of your wizard.

例如:

public void OnInit(object sender, EventArgs e)
{
    GatherForms();
    CreateWizardForm(); // creates a wizard and adds controls it will need
}

private void Checkbox_Checked(object sender, EventArgs e)
{
    var checkBox = (CheckBox)sender;
    // append checkBox.SelectedValue to session state object of checkboxes
}

protected override void OnPreRender(object sender, EventArgs e)
{
    if (/* Session checkboxes contain values */)
    {
        this.WizardForm.Visible = true;
        this.CheckboxList.Visible = false;
    }
}

这工作只要你提前知道哪些控制将在向导的形式时间。你可以改变这些控件的值在上preRender事件,拨动自己的知名度等,但你不能去,并添加新的控件(如Controls.Add被(新按钮())) - 这已是在OnInit事件完成。

This works provided you know ahead of time which controls will be in the wizard form. You can change the values of those controls in the OnPreRender event, toggle their visibility, etc, but you can't go and add new controls (e.g. Controls.Add(new Button())) - that has to be done in the OnInit event.

这篇关于不能得到的OnInit事件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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