ASP.NET 自定义控件和“动态”控件 事件模型
好吧,我不确定标题是否完全准确,欢迎提出建议!
我正在创建 ASP.NET 自定义控件,这对我来说还是比较新的东西,所以请耐心等待。
我正在考虑事件模型。 由于我们没有使用 Web 控件,因此按钮不会触发任何事件,而是我使用适当的参数手动调用 __doPostBack。 然而,这显然意味着在选择选项(选择时呈现不同的效果)时会发生大量回发。
随着时间的推移,我需要使其更加支持 Ajax 和响应,因此我需要更改事件绑定以调用本地 Javascript。
所以,我想我应该能够切换控件的“模式”,它可以使用回发和处理本身,或者您可以指定要调用的 Javascript 函数名称而不是 doPostBack。
- 您对此有何看法?
- 我是否以错误的方式从控件中引发事件? (完全欢迎这里的建议!)
- 您将如何解决类似的问题?
编辑 - 澄清一下,
- 我正在创建一个自定义呈现的控件(即继承自 WebControl)。
- 我们没有使用现有的 Web 控件,因为我们想要完全控制渲染的输出。
- 据我所知,从自定义呈现控件中获取服务器端事件发生的唯一方法是从呈现元素中调用 doPostBack(如果错误,请更正!)。
- ASP.NET MVC 不是一个选项。
OK, I am not sure if the title it completely accurate, open to suggestions!
I am in the process of creating an ASP.NET custom control, this is something that is still relatively new to me, so please bear with me.
I am thinking about the event model. Since we are not using Web Controls there are no events being fired from buttons, rather I am manually calling __doPostBack with the appropriate arguments. However this can obviously mean that there are a lot of postbacks occuring when say, selecting options (which render differently when selected).
In time, I will need to make this more Ajax-y and responsive, so I will need to change the event binding to call local Javascript.
So, I was thinking I should be able to toggle the "mode" of the control, it can either use postback and handlle itself, or you can specify the Javascript function names to call instead of the doPostBack.
- What are your thoughts on this?
- Am I approaching the raising of the events from the control in the wrong way? (totally open to suggestions here!)
- How would you approach a similar problem?
Edit - To Clarify
- I am creating a custom rendered control (i.e. inherits from WebControl).
- We are not using existnig Web Controls since we want complete control over the rendered output.
- AFAIK the only way to get a server side event to occur from a custom rendered control is to call doPostBack from the rendered elements (please correct if wrong!).
- ASP.NET MVC is not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很奇怪。 您正在使用 ASP.NET 服务器控件和自定义控件,但您没有使用 Web 控件? 你手动调用 __doPostBack 吗?
你喜欢用困难的方式做事吗?
如果我仍然使用服务器控件模型而不是 MVC,我会用 ASP.NET Ajax 控件来解决这个问题,然后就到此为止了。 你所做的就像在T型车上装一个鼓风机。这可能很有趣,但当你完成所有艰苦的工作后,你真正拥有的是什么?
Very odd. You're using ASP.NET server controls and custom controls, but you're not using web controls? And you're calling __doPostBack manually?
Do you like to do things the hard way?
If I was still using the server control model rather than MVC, I would slap ASP.NET Ajax controls on that sucker and call it a day. What you're doing is like putting a blower on a model T. It may be fun and interesting, but after you're done with all the hard work, what do you really have?
我对此进行了更多的挖掘,并发现了如何在需要时将 Javascript 注入到客户端。 显然,这将在提高控件响应速度和减少与服务器的往返次数方面发挥重要作用。
例如:RegisterClientScriptBlock。
期待更多地玩这个,请随意参与进来!
I have been doing some more digging on this, and came across how to inject Javascript in to the client when required. This will obviously play a huge part in making the controls more responsive and less round-trips to the server.
For example: RegisterClientScriptBlock.
Look forward to playing with this more, feel free to get invovled people!