I have this Create Event form (asp.net mvc), and i have to change some parts of the form, depending on user's choice. When the user clicks radio buttons, different user controls (ascx files) should be injected inside the form hopefully using Ajax. Any suggestions to do this?
If you use jQuery you can easily update your form with an ascx or partial html.
The following link could help you further
Wrap your buttons inside forms that are submitted via AJAX (Ajax.BeginForm...). Have the click event submit the form. Using Ajax.BeginForm you can specify the container to be updated with content (UpdateTargetId in the AjaxOptions). Have the form submit to a controller than returns a PartialViewResult based on the form parameters submitted (button values).
If you could have the functionality triggered by links, this is even easier. Just use Ajax.ActionLink and specify the route values necessary to get the proper content. You could, of course, style the links to look like buttons, even radio buttons though that would require some graphics, probably.
Related
Let's say I have a bunch of controls that I want to "repeat" on the page. The user can click "Add" to add another set of controls.
I still want the input to be kept (that the user has put in) after the postback that will be triggered when the user clicks "Add".
What is the best way to do this? I would like to use a repeater but then you have to (as far as I know) store the information in ViewState and then re-populate all the controls with the old data.
I am using ASP Web Forms.
Se picture for example: https://i.stack.imgur.com/rpmnG.png
I have jquery buttons in my website,but some of these buttons doesnt have to clickable by every user so there must be permissions, all buttons make ajax calls so how can i control that buttons, for example if I have 3 buttons like:
button1_click()
some ajaxcalls
button2_click()
some other ajax calls
button3_click()
another calls
so admin can call this events, but simple user only have to call button3's
what kind of approach I need?
-Do i have to control every button using another ajax call like:
button1_click()
an ajax call (permission.aspx) read session,admin or not,return true or false
if(true)
{
someajaxcall()
}
-Or I have to hide or show buttons according to session? but i dont know how can i do that. I m using jqgrid asp.net components so that grid has custombuttons but that buttons doesnt have id so i cant reach it from serverside and also that buttons doesnt have visible or disable properties.
Im using asp.net and jquery
Normally I prefer to not show buttons that people can't use, so since you are using asp.net on the back end, you can hide the button when the page is created after checking the user's permissions, i.e:
btnOne.Visible = UserIsAdministrator; //you need to define thisvariable based on your own rules.
etc
If you want to show them anyway, you could do btnOne.Enabled = UserIsAdministrator instead, which will show the buttons, but disable them as appropriate;
you can use a hiddenfield on that page which will hold the user roles. then inside the DOM Ready() function iterate through those buttons to make them enabled/disabled.
However, you should also check the roles in the server side after an ajax call is made in case someone manually changes the value of the hiddenfield to enable the buttons.
I'm working on developing a custom control to select items from a predefined list. This is accomplished via 2 ASP.NET ListBox controls, with a few buttons to trigger the movement of ListItems from one ListBox to the other (lets call these ListBoxes lstSelected and lstDeselected).
This is easy enough to do in ASP.NET or JavaScript independently: I have both working. However, if modifications are made via JavaScript, ASP.NET retains no knowledge of this. Is there any way to register the creation of of options in a select tag without AJAX?
You could also do this with traditional postbacks, it doesn't have to be ajax. The postbacks would be triggered by clicking your buttons which change which items are in which listboxes.
You could have a couple of hidden fields, say hdnHasSelectedChanged and hdnHasDeselectedChanged, and set those fields in your javascript code. Then, when a postback really happens, your code-behind can read those hidden fields to detect if changes occurred.
Ok, this is a bit different scenario. I guess I would have to think about doing it this way sort of with MVC anyway if we were actually using MVC...but we're not at the moment.
So I've got and .aspx page. In that .aspx page is a user control (.ascx). And in that user control is a custom control (.cs).
The custom control has a repeater in it. So I'm showing a list of items on that .aspx through the .ascx's custom control. For each item in the repeater is a button. It's just a hyperlink, just a regular on my page
When you click that button, it redirects to whatever page you're on. Since the custom control never knows what your parent .aspx page is, I'm doing a redirect to the self .aspx by doing a Response.Redirect(Request.Path). So that way it always redirects to whatever .aspx is using that user control and custom control.
So after it redirects to self, I check the querystring in the page_load of whatever .aspx is using it. If the value is true, then I handle it however the .aspx wants to. In this case when it's true, I call a method in the code behind of my .aspx that handles the action for the button. For example lets say that button was "Add to Shopping Cart", the .aspx handles that action and calls a AddToCart method in the .aspx.cs.
I'm not using an ASP.NET control for the actual hyperlink and button because I just don't need it and in my particular case I'm using a user control and a custom server control. For this instance, I had some issue where I didn't wnat to use an ASP.NET control...I forget why but the point is, no this is what it is.
So with that, I'm trying to figure out how I can apply some AJAX here call to call that method instead. I still need to somehow redirect again back to the same page like I'm doing...I'm doing the redirect in that method after all the logic at the end. I am redirecting again back to the same page, because I need my Page_Load methods in my .aspx and also in an .ascx to still fire off after that method is completed.
So I am not sure where to start on this. Let me go through this once again:
Custom control has a repeater in it and in the repeater, each item has a standard HTML hyperlink (non ASP.NET control) which wraps a standard image tag (image is a button)
User control contains the custom control
The .aspx page contains the user control
User clicks the button and hyperlink redirects them to the parent .aspx page that is using this custom control...so it calls Response.Redirect(Request.Path)
In the code-behind of this .aspx, in my page_load I check a querystring flag to see if I performed that action..meaning user clicked that button. For example one of the querystring params is "AddItem" and another querystring param is "itemID". If movedItem is true, then I fire off a method called MoveItem(int itemID)
Method MoveItem is called
Method MoveItem redirects again back to this same .aspx using Response.Redirect(Request.Path).. this is so that the page load is hit again as well as my .ascx page load is hit. Because in both those page loads, I rebind a repeater so I can show the latest state of the lists. I call a method in my .aspx page_load which rebinds a grid and then page_load in my .ascx also calls another method which rebinds some other list
You can use $.get() to pass the variables to a server-side method that performs any server-side functions you need. You don't need to run page_load or have a code behind.
If you only need to update the HTML in the client's browser then you can use jQuery to add/remove them from the lists in the HTML. You can use the html() function in jQuery to append the item to the list.
You should look at DataTables.net as you can build a client side, editable grid that will perform Ajax updates as well. You would be able to keep your repeater control, but eliminate all the back and forth to the server and the deciphering of clicks and coordinating between Page_Load and etc. Here is a good post from Dave Ward(Encosia) that covers jQuery, Page_Methods and repeater controls.
If you want to preserve your work so far, why not try Ajax Update Panels around the region that you do not want to visible "refresh"?
Remember that the first A of AJAX is asynchronous. You'll want to avoid having your button reload the page, of course. Something like:
Figure out the data you need to send to your "add to shopping cart" handler page -- part number (SKU, etc.), colors, sizes, quantity, etc.
Ensure this data is on every applicable page, in an identical fashion: perhaps one or more hidden input elements, or even from the page URL.
Replace the custom control hyperlink/button href with a [client side] onclick handler instead, which will get the data from step 2 and send it via $.ajax() (or $.post or $.get), specifying the response handlers. You probably want to disable the button or give other visual feedback to avoid duplicate clicks.
In the response handlers, update the shopping cart section of the page with the number of items or indicate success or failure.
What is the best way to implement mutliple Default Buttons on a ASP.NET Webform?
I have what I think is a pretty standard page. There is a login area with user/pass field and a login button. Then elsewhere on the same page there is a single search field with a search button.
asp:Panel has a property named DefaultButton. You just need to encapsulate your markup portions with appropriate panels and set the default buttons for each.
Capture the enter key press for each area of the screen and then fire the corresponding button's click even.
Use a helper function like this one to tie the textboxes to their associated buttons.