How to sort dynamically created GridViews in code-behind - c#

I have a dynamic number of Gridviews created based on user-entered data. Each of the Gridviews has its own header row, and I want the user to be able to sort individual GridViews by clicking on a particular column's header. I've been having trouble writing an OnClick_sort method to reference the particular GridView and that data. Also, I want this to be done on Client-side instead of being passed back to the server, so I need to write the Sort method in a JavaScript, correct? Each GridView has a unique ID, generated when adding the GridView to the control.

You can do that by sing Jquery. Refer This...
http://www.aspsnippets.com/Articles/Scrollable-GridView-with-Fixed-Headers-and-Client-Side-Sorting-using-jQuery-in-ASP.Net.aspx
http://www.aspsnippets.com/Articles/Filter-GridView-Records-using-DropDownList-in-HeaderTemplate-Header-Row-in-ASPNet.aspx

You should keep in memory the elements of the grid and sort it using your code (handling events and etc). But, the magic is: you should put your grid in a updatePanel, so your code will be translated to a script that will execute on the cliente side. If your code is too complex to run on the cliente, it will comunicate with the server without your intervension.
Is that? ;)

Related

Call code behind c# function from javascript

I know this has been asked, but none of the numerous answers fit with my situation. So I will humbly ask if someone can walk me through this again gently.
Environment: .Net 4 web app using c#, javascript, jquery, sql server, and entity framework.
Question: I need to be able to allow a user to "quick add" a value into a table that is bound to a drop down box without losing values a user has already entered onto current page. I don't care if I have to do a postback or use jquery or use ajax, etc. I need the functionality first - prettiness later ... the scenario is described below if you care to keep reading. With all that being said, I would love for this to be smooth and not "clunky" for the user.
Scenario:
I have a page where the user is able to enter many pieces of information into textboxes, static drop down boxes, and drop down boxes that are bound to other tables that will all get combined and inserted into one table. Well, it never fails, a user could be filling in the page and when they get to one of the table bound drop down boxes - a value is missing or they haven't added something item yet. I would like to add a button or link or something next to that drop down box that would allow the user to "quick add" an item to the table that fills that drop down box and then refresh the drop down box so they can choose that new value from the list ... all while not losing the other data on the screen they already entered.
What I've tried:
-Tried WebMethod option. Currently, this web page calls some custom "bind" methods in the code behind to fill drop down boxes so they are not filled directly from objects from entity framework. So that means using a static method will not work for a solution because part of the static function would need to call the bind method to refresh the drop down box with the newly inserted values.
-Tried PageMethod option. Some of the other samples used Page Method settings, but my site uses master and content pages and the posts seem to state that the page method route was not going to work.
Make sense?
Add an UpdatePanel to the page. The update panel should wrap the drop down to be added, as well as the textbox/button for adding a new entry to that drop down. You would want to have a different UpdatePanel for each dropdown/button pair, if applicable.
When they click the button inside of the update panel you can read the textbox, add an extra item to the drop down, and send an update to the database. (I'd manually add the new value to the drop down rather than updating the database and re-binding, if possible, but it may not be.)
The magic of UpdatePanels will make sure that this is all asynchronous and so doesn't disturb the user working on the page.

How to repeat textboxes and check box dynamically using repeaters

I have requirement in my application where I am struck . These is my scenario:
Initially I need to show up two text boxes with a check box and a button. upon clicking the button we need to generate one more row. Like wise we need to create multiple rows dynamically.
Initially I tried with Table Control but after struggling a lot, I came to know that we can create only one row dynamically using Table Control. So now I am planning to do in repeater control.
In this kind of scenario, it would be better to use the GridView Control Instead, take a look here some working examples:
http://www.revenmerchantservices.com/page/gridview-add-new-row.aspx
http://amitpatriwala.wordpress.com/2008/04/18/inserting-new-row-in-gridview-in-aspnet-20/

Display Data Based on Drop Down List

How would I go about using c# and ASP.NET (or Javascript if need be) to have my drop down lists change something like a div on the page. Basically I want it to populate a div with certain data.Either from a SQL or XML Data Source. I have places I wanted listed, so I'm assuming I'm going to need to put each place in with the tags that it may contain.
I would use jQuery, or some other javascript library, to hook up a listener to your dropdown and when the value changes I would make a request to some services or where you get your data from.
If you're using asp.net, then it's really straight-forward.
1) put a <asp:label> or <asp:literal> in your div.
2) in your designer, double-click your dropdownlist -- this will take you to the code-behind for the dropdown's onselecteditemchanged event.
3) Call your data, and assign it to the label. Ex.: ...me.myLabel.text = myreader("myfield"), or some more complicated html you've assembled, or what have you.

How do I perform an action on a gridview column only if it exists?

I have a gridview which is in a usercontrol on a page. The gridview displays data based on whatever the calling page tells it to, which may or may not include certain columns.
I want to tell the gridview how to format the columns if they're present on the page, but I also need it to ignore all the formatting if the column doesn't exist.. I already know how to format them, so I guess I just need a method to determine if they're there, and where they are in the gridview.
It sounds like you need to be creating everything dynamically in your code-behind to achieve this kind of granularity. This should help.

Is there a way to asynchronously filter an IList?

Ok, so there has to be a way to do this... no? If not I'd love some ideas.
I have two repeaters and an image inside an update panel along with some AJAX dropdowns with link buttons to the left. I want to update the data inside the update panel as fast as possible as values are selected from the dropdowns.
What do you think would be the best way to update the data? The repeaters are populated by objects, so if I could just filter the objects by some properties I could end up with the correct data. No new data from the server is needed.
Anyone have some ideas?
As far as I know, it is not easy to get just Data and data-bind the repeater on the client side. But, you might want to check this out.
Wrap only the repeater you want to rebind with an update panel of its own. The only viewstate transferred when doing this is the portion inside the update panel. You may have to play around with the triggers and update mode of the panels to get everything to play nicely.
Another option is instead of using repeaters, serialize your objects into XML and then write a page method that returns an html string of your transformed data using xsl. Then client side call your path method and update the DOM as appropriate.
A third option is to use use a service reference/page method to return JSON objects and update the DOM manually.
http://www.asp.net/AJAX/Documentation/Live/tutorials/ASPNETAJAXWebServicesTutorials.aspx
Good luck! I have done all 3,
If your data is already rendered to the screen, you can access the dom and manipulate the dom and hide/remove the ones you want. I've done this with jquery, but the same should be possible with ASP.NET Ajax library.

Categories