How to call different JQuery functions from a dropdownlist? - c#

How do you bind each item in a dropdownlist to a different JQuery function?
For example I have a dropdown with different report names, I want each time you select the drop down to trigger a new report to display.
I accomplished this before with just straight text links with Report 1 which called $("#btnReport1").bind("click", function () {...}
I want to call the same function above from each of the different report items in the dropdownlist. There will be 5 or so I'll need to call. Is there a way to embed a link in each item? Do I use "change" somehow over "click" ?
I'm using Razor and MVC.Net so I could do something like #Html.DropdownList(...) with a SelectList object and put code in the controller file if need be. But I prefer to handle this on the HTML though.

sounds like you're trying to bind to a select dropdown. the best way to accomplish this is the onChange() handler (see the docs for details).
from there, you would simply determine which option had been selected based on the current value (probably obtained via the .val() method) and perform an action based on that value, using an series of if statements or another method.

Just call a single function on the click event and have it look at the value and decide what specific report to load from there.

Related

How to sort dynamically created GridViews in code-behind

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? ;)

Multiple selection in Dropdown list

I am wondering that if an ASP DropDownList Control support Multiple Selection or not?
If Yes, then how can i enabled multiple selection in DropDownList ???
No, but there's lots of free stuff on the web. Here's a full sourcecode, tutorial and example for just what you need:
Link
By native dropdown never allow multiple selection, but you can use list box instead of that, this have option to set mutli / single item selection. Other wise if you want customized one you have to use some jquery plugin or some other js frame work plugins
you can try this sample link :)
jQuery Dropdown Check List
If I got you right, then you want to enable the multiple selection in your drop down list.
Here are some references where custom controls can solve your problem:
Check here
Or Check here

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.

Filtering Pane, ASP MVC 3

My plan is to create a a two-pane page using ASP MVC 3. The left pane should be a small filter pane and the right the main content, showing a list of objects (say products).
Initially all products will be shown since no filter is applied. When selecting "only red", only red products are shown in the list. When further selecting a price range, only products in that price range will be shown.
Functionally the plan is to implement the filtering pane as a treeview with checkboxes (to be able to drill down to more and more specific filtering options), graphically it will probably be enhanced in some way to improve usability.
What is the best way to implement the coupling between the filter pane and the main list? Everything should work server side, but should of course use javascript (jQuery) when possible for direct feedback.
The simplest way is probably to make it closely coupled solution, calling a specific Asp MVC action using a custom-built javascript (with fallback to a form post). Doable enough, sure, but how to make the solution reusable? Also it would be nice to not loose all filtering data when navigating forward and back, i suppose GET arguments is the only decent way to do that?
Are there any best practices, any guidelines or anything to base this on to make a nice modular structure for filtering.
Victor, I recently had to solved this same problem. I'm not promising it's the best way but it's pretty clear and should even work well in case JavaScript is disabled (who even does that anymore?).
Create a that calls the action with all the field-selectable search options like "only red".
To that same form, add empty, hidden value for the things not directly as fields (paging, sorting...)
Setup your form with the excellent and very easy to use JQuery.Forms (http://www.malsup.com/jquery/form/) to make you form submit via JQuery (all your form values will be passed as JSON on form submit).
Make your back/next/paging/sorting links pass their individual values via query (no-JS fallback) and use JQuery to capture their click events. The JQuery click events on those links should assign the value of the value passed by the link (page number, sort column name...) to the corresponding hidden field in the form and call submit (with thanks to Jquery.Forms will submit via AJAX).
When you configure JQuery.Forms, you can define a callback method. In the callback method take the result (the HTML returned by your action that should contained your filtered+sorted+paged result) and replace the document (or DIV if you used a partial action + view) with that.
The result is a JQuery solution that works when JS is off. You also have very minimal JS code to write other than wiring the event handlers.
I'm not sure if it will be helpful but in MVC 3 you have access to a property called IsAjax from the view so you can do specific things to server a slightly different view when called from AJAX.
Cheers

AJAX CascadingDropdown - Setting the selected index

I have a CascadingDropDown on an ASP.NET page.
Now, the prompt text is "Select State". (list of states).
However, on a different version of this page (ie querystring), i might want to set the selected index to "California" for example.
How can i do this?
The web service used by the ajax control (ie GetStates) gets invoked at the same time the jquery document.ready function is triggered (ie asynchronously).
So when i try and set the selected index in jquery, the items are not yet bound.
Is there a way to attach a handler to the ajax dropdown so that i can set the selected index once the webservice call has completed, and the items are bound?
So instead of trying to get tricky, i ended up replacing the CDDL with regular client-side dropdownlists and extending them with jQuery/AJAX.

Categories