创建自定义服务器控件以接受用户输入
我正在尝试构建一个服务器控件,该控件根据“QuestionTypeId”显示文本框、日期选择器或是-否单选按钮。
我的控件按照我想要的方式显示,但是当在表单上按下提交按钮时,在 RenderContents 方法中生成的文本框、日期选择器或单选按钮都为空。
我尝试将生成的控件存储在视图状态中,这阻止了它们为空,但用户输入没有被存储。
如果需要的话我会发布代码。 就问吧。
I am trying to build a server control that, depending on a "QuestionTypeId" display either a text box, date picker or Yes-No radio buttons.
I have my control displaying how I want it to, but when the submit button is pressed on the form, the text box, date picker or radio buttons that were generated in the RenderContents method are all null.
I have attempted to store the generated controls in view state, that stopped them being null, but the user inputs were not being stored.
I will post code if it is needed. Just ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您需要在 CreateChildControls。 这意味着您需要将 QuestionTypeId 的值存储在 Viewstate 或 ControlState 中(我认为 ControlState 在这种情况下适用,因为如果没有此值,您的控件将无法工作)。
I think you need to create (and add) the controls in CreateChildControls. This will mean you'll need to store the value of the QuestionTypeId in either Viewstate or ControlState (I'd argue that ControlState is applicable in this case, as your control can't work without this value).
动态添加控件时,需要确保在恢复视图状态之前重新创建它们。
我有一段时间没有这样做了,但根据记忆,我认为您应该在 OnInit 方法中重新创建控件。 这发生在加载回发数据之前以及在控件从视图状态设置其值之前。
可能值得阅读有关 ASP.NET 页面生命周期的内容:
http:// /msdn.microsoft.com/en-us/library/ms178472.aspx
When you add controls dynamically, you need to make sure they are recreated before the viewstate is restored.
I haven't done this in a while, but from memory I think you should recreate your controls in the OnInit method. This happens before postback data has been loaded and before the controls have their values set from viewstate.
It may be worth doing some reading on the asp.net page lifecycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
您可以创建用户控件并将服务器控件用于文本框、日期选择器、单选按钮。
如果您创建自定义服务器控件,则必须将发布的数据添加到控件属性中。 您可以在控件 OnInit 事件中执行此操作:
You can create a user control and use server controls for textbox, datepicker, radiobuttons.
If you create a cusom server control then you have to add the posted data to your control properties. You can do this at your control OnInit event:
一种更简单的方法是在设计时创建所有控件,并根据您的要求使控件不可见。
示例代码:
如果您确实使用动态控件,则应该按照 David 指出的那样,将值 QuestionTypeID 保存在 ViewState 或 ControlState 中,然后根据该值创建您想要的控件。
(每次页面加载时都需要创建控件,即使是在回发时也是如此,并且如果您希望保留并恢复其 ViewState,则无法在页面生命周期的后期创建它们,然后使用 Page_Load 方法)
示例代码:
PS
使用动态控件来指定 ID 始终是一个好主意。
您可以将添加的控件保存到成员变量中并在其他地方使用它们(分配后)
您可以订阅他们的事件,如果用户发布了新值,您的方法将被调用
A simpler method would be to create all the controls at design time and make the controls invisible based on you requirements.
Example code:
If you do use dynamic controls you should do as David pointed out, save the value QuestionTypeID in ViewState or ControlState and then create the control you want based on that value.
(the control needs to be created every time the page loads even on a post back and they cannot be created later in the page life cycle then the Page_Load method if you want their ViewState persisted and recovered)
Example code:
P.S.
It's always a good idea with dynamic controls to specify the ID.
You can save the added controls to member variables and use them elsewhere (after they are assigned)
You can subscribe to their events and if the user posted a new value your method will be called
我已遵循您的建议并执行了以下操作:
1)问题类型存储在我的服务器控件的视图状态中。
2) 现在,在 CreateChildControls 上创建控件的新实例并将其添加到页面上的占位符。
我现在的问题是,事情似乎以有点奇怪的顺序触发:
1)在页面初始加载时,创建子控件被触发,并且我的服务器控件的 RenderContents 方法被触发。
2)单击按钮加载新联系人,这会触发创建子控件并触发 RenderContents。
3)输入详细信息并按下保存,这会触发创建子控件,但不会触发 RenderContents,并且通过尝试访问我的控件以获取值而生成 NullReferenceException。 (如果我跳过访问控件的代码,则会调用 RenderContents 并进行渲染。
其次,另一个问题是,当我尝试设置值(onDataBind)时,我尝试访问在服务器控件中生成的文本框并获取另一个 NullReferenceException
想法?
I have followed your advice and done the following:
1) Question Type is stored in view state in my server control.
2) on CreateChildControls now creates a new instance of my control and adds it to a place holder on the page.
My problem now is that things seem to fire in bit of an odd order:
1)On initial load of page, create child controls is fired and the RenderContents method of my server control fires.
2)A button is clicked to load a new contact, this triggers create child controls and RenderContents is fired.
3)details are entered and save is pressed, this triggers Create Child Controls but RenderContents is not triggered and a NullReferenceException is generated by trying to access my control to get the value out. (If i skip the code that access my controls, RenderContents is called and renders.
Seconly, another issue is that when I try an set a value (onDataBind) I try to access the Text box that has been generated in my server control and get another NullReferanceExeption
Thoughts?