ASP.NET(网络表单):使用 MINIMAL 服务器控件并用 JQUERY 代替?
我目前正在使用 ASP.NET,设计表单的人已将所有服务器控件用于文本框和下拉列表等,而实际上他们不提供回发。某些下拉列表和文本框是我需要的值仅在jQuery中,据我所知,将这些控件转换为标准 html控件而不是ASP.NET服务器控件没有任何缺点?
我想我需要继续将 GetDataGrid 按钮作为服务器控件,因为我需要它回发(并接收 PageLoad 事件等 - 所有 asp.net 事件)来更新 GridView ?或者是否可以从 Webmethod 使用 GridView(ASP.NET 服务器控件)并通过 Jquery 调用它?
当然,在我的 webmethod 中,我需要 gridview 的实例来添加数据源 - 但我不知道如果不在 ASP.NET 事件中这怎么可能 - 或者也许我错了?
我想到的另一件事是将 GetGridView 按钮更改为标准 HTML 并从客户端单击事件调用 javascript 回发?这样它就会进行真正的回发,我最终会进入Page_load。
使一切生效,我不想更改 GridView asp.net 控件,因为它的功能与 asp.net 服务器控件一样好,但我不确定如何执行此操作。
我记得有一个文档说“如何在没有服务器控件的情况下使用 asp.net webforms”,但我似乎找不到它。我想使用像 asp.net MVC 这样的网络表单 - 但我无法将项目更改为 MVC - 它超出了我的控制范围。
我很想听到一些关于如何执行此操作或评论等的反馈。
我发现 ASP.NET Webforms 向页面中注入了很多代码味道 - 我正在使用 .NET 3.5,所以很多输出都是表格等...
I am currently working with ASP.NET and the person who designed the form has used all Server Controls for things like TextBoxes and Dropdowns etc when really they are not providing postbacks.. Some of the dropdowns and textboxes are values that I need only in jQuery so as far as I can see there are no drawbacks to coverting these controls to standard html controls rather than ASP.NET server controls?
I suppose I will need to continue to have my GetDataGrid button as a server control because I will need it to postback (and receive PageLoad events etc - all asp.net events) to update the GridView? Or would it be possible to use the GridView (ASP.NET server control) from a Webmethod and call it via Jquery?
Of course in my webmethod I would need to the instance of the gridview to add the datasource - but I don't see how this would be possible without being in the ASP.NET events - or maybe I wrong?
The other thing I thought of was changing the GetGridView button to a standard HTML and calling the javascript postback from the client click event?? This way it would do a real postback and I would end up in Page_load.
Taking everything into effect i don't want to the change the GridView asp.net control as it funcions well as an asp.net server control but i am unsure how i would do this.
I remember a document being available that said "how to use asp.net webforms without server controls" but i can't seem to find it. I suppose using webforms like asp.net MVC - but i can't change the project to MVC - its out of my control.
I would love to hear some feedback with regards to how to do this or comments etc.
I find ASP.NET webforms to inject a lot of code smell into pages - I am using .NET 3.5 so a lot of the output is with tables etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 请求。 Form["..."] 然后你就可以获得标准html输入字段中填写的信息。
我建议您不要继续使用 GridView 控件,而是看看 jqGrid 或新的 微软为 jQuery 部署的模板系统(目前是一个插件,但预计从 1.5 版开始将成为 jQuery 核心的一部分)。这些可以绑定到 json,可以从 webmethod 或 pagemethod 调用中检索,以用数据填充模板。
If you use Request.Form["..."] then you can get the information which was filled in in standard html input fields.
Instead of keep on using the GridView control I suggest you take a look at either jqGrid or the new templating system that Microsoft put into place for jQuery (currently a plugin but expected to be part of core jQuery from version 1.5 on). These can bound to json which can be retrieved from a webmethod or pagemethod call to fill up the template with data.
使用网络方法。
在 html 控件上设置客户端事件(如“onchange”),然后在事件触发时调用的 javascript 函数中,您可以使用 PageMethods 将数据发送到后面的代码。
Use webmethods.
Set a client event (like 'onchange') on the html control and then in javascript function called when the event is fired you can use PageMethods to send your data to the code behind.
一些想法...
GridView 无法在 WebMethod 中创建,即使有办法让它工作,您最好还是使用真正的客户端网格。由于这不是一个选择,我认为尝试对现有页面进行任何重大更改没有太多意义。
ViewState
将文本框、按钮等更改为 HTML 版本,可以稍微减小 Viewstate 的大小,但会增加处理与页面交互的方式的复杂性。您可以将
runat="server"
添加到 HTML 控件,这将使您能够控制呈现的内容,并且仍然可以访问服务器端的控件。.Net 4 为您提供了对视图状态的更多控制,但不幸的是在 3.5 中它并不那么容易。
GridView
您可以将 GridView 包装在 更新面板。这是向页面添加一些交互性的“廉价”方法,尽管您不会在性能方面获得任何好处。
仍然可以在客户端使用 jQuery 操作 Gridview。互联网上有很多教程、博客文章等解释如何做到这一点。
MVC 与 Webforms
也可以在同一个网站中混合 ASP.Net MVC 与 Webforms。听起来您似乎熟悉 MVC,因此您可能需要为任何新页面考虑这种方法。 这里一篇博客文章解释了如何做这个。
更新:
这里由 Scott Hanselman 撰写的最新文章,介绍如何将 MVC 与现有 Webforms 应用程序结合使用。
Some thoughts...
The GridView can't be created in a WebMethod and even if there was a way to get that to work, you'd be better off going with a genuine client side grid. As that's not an option, I don't think there is too much point in trying to make any major changes to your existing pages.
ViewState
Changing the textboxes, buttons etc to HTML versions, would gain you a little bit in reduced Viewstate size but add a bit of complexity in how you handle interactions with the page. You can add
runat="server"
to HTML controls which will give you control over what is rendered and still have access to the control on the server side..Net 4 gives you far more control over viewstate but unfortunately in 3.5 its not as easy.
The GridViews
You could wrap the GridViews in UpdatePanels. That's a 'cheap' way to add some interactivity to your pages although you won't be gaining anything in terms of performance.
It's also still possible to manipulate the Gridview using jQuery on the client-side. There a lots of tutorials, blog posts etc explaining how to do this on the Internet.
MVC with Webforms
Its also possible to mix ASP.Net MVC with Webforms in the same website. As it sounds like you are familiar weith MVC, you might want to consider this approach for any new pages. Here's a blog post explaining how to do this.
Update:
Here's a more recent article by Scott Hanselman on how to use MVC with an existing Webforms application.