无法获取 OnInit 事件中的值
我了解事件在页面生命周期中发生的顺序,但这对我的情况没有帮助。我有一个复选框列表,其中填充了一个充满表单的目录。当我选中表单名称旁边的复选框时,我希望它动态创建向导步骤并插入表单。
事件顺序: 初始化时: GatherForms() - 检查目录并将所有表单名称加载到复选框中 LoadForms() - 检查“选定”会话并加载收集的表单
CheckBoxList:SelectedIndexChanged #AutoPost = true# PopulateForms() - 循环遍历复选框并将它们添加到会话状态
当用户单击复选框时,它会执行回发并点击从会话中提取的 OnInit。问题是 PopulateForms() 尚未运行,因此即使已检查,它也不会填充任何内容。如果我单击另一个项目,它将回发并出现。在刷新之前,我似乎无法从复选框中提取任何有用的信息,这意味着我无法看到表单立即出现。我也尝试过循环复选框,但不幸的是 viewstate 尚未发布。叹。
有什么建议吗?
谢谢!
PS:我无法使用Request.Form[],因为我必须将所有选定的项目从复选框中取出。也许我可以,但我找不到办法:/
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.
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 = true#
PopulateForms() - Loops through the checkboxs and adds them to session state
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.
Any suggestions?
Thanks!
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 :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在 ASP.NET 上不断进步的过程中遇到的一个常见问题。
对于动态控件,您会遇到 OnInit 期间它们实际上并不存在的问题。
您可以做的是创建用户在 OnInit 期间可能看到的所有控件,并隐藏用户在代码中看不到的元素。页面将加载,所有可能的控件将被实例化(在代码中 - 不要担心它出现在 HTML 中并使其膨胀),然后事件处理程序将触发,然后您可以处理设置向导的可见性。
例如:
如果您提前知道哪些控件将出现在向导表单中,则此操作有效。您可以在 OnPreRender 事件中更改这些控件的值、切换它们的可见性等,但您不能添加新控件(例如 Controls.Add(new Button())) - 这必须在 OnInit 中完成事件。
This is a common problem I wrestle with as I get better at ASP.NET.
With dynamic controls you have the problem of them not actually existing during OnInit.
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.
For example:
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 中添加动态控件。在页面加载之前不要进行任何填充/处理。您将通过这种方式保留您的价值观。
Only add your dynamic controls in OnInit. Don't do any population/processing until PageLoad. You will retain your values this way.
动态控制的一个经典陷阱。
老实说,最好的解决方案是仅使用
Request
值数组来查找您需要的信息,并忽略这种特定情况的生命周期内容。在
OnInit()
中,发布的值就在那里,只是 ViewState 尚未处理它们。因此,您可以执行if (Request["myCheckBoxName"] == xxx)
来处理各个复选框,或者如果您可以使用Request["__EVENTTARGET"]
来获取引起回发的控件的名称。请参阅我的旧答案此处进行更彻底的讨论的问题。
A classic pitfall with dynamic controls.
Honestly the best solution is to just wield the
Request
values array to look for the information you need and ignore the lifecycle stuff for this particular situation.In
OnInit()
, the posted values are there, they just haven't been dealt with by ViewState yet. So you can just doif (Request["myCheckBoxName"] == xxx)
to deal with individual checkboxes, or if you can useRequest["__EVENTTARGET"]
to get the name of the control that caused the postback.See my old answer here for a more thorough discussion of the issue.