2

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...

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
Martin
  • 23,844
  • 55
  • 201
  • 327
  • Also i don't think its possible from asp.net (code behind) to receive values of an html control without it having runat=server. The only difference between a textbox HTML control with runat server and a textbox from asp.net is that i suppose the html control with runat server wouldn't contain Viewstate? SO i suppose thats one advantage - a little less bulky and of course i can control what is output to html as its a pure html control... I woudl love to hear ideas on this. thanks in advance – Martin Nov 26 '10 at 11:00

3 Answers3

1

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.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
  • Thanks for the comment. Problem is i can't change the Gridview for 1 from jquery - although i would love to :-) There are too many gridview in use and i jsut don't have the time. My manager hasn't approved this :-( but of course changing other controls is ok... but the girdview i don't think i can pickup in a webmethod as its static. – Martin Nov 26 '10 at 11:29
  • Then I suggest you talk with your manager first. He has to approve your hours and you need to convince him of the benefits of changing it. – Kris van der Mast Nov 26 '10 at 11:32
1

Also i don't think its possible from asp.net (code behind) to receive values of an html >control without it having runat=server.

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.

franz976
  • 333
  • 3
  • 14
0

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.

Geoff Appleford
  • 18,538
  • 4
  • 62
  • 85