I need to create a user control with 3 images: like, dislike and comment buttons. I want to like and dislike button to save some info to the database (liked user and liked object). But i want to work them without reloading the page.
Example scenario:
Like button shows like count if the post has any
User likes a blog post.
Save like process to the DB
Disable like button.
I want to implement this operation via AJAX call to the page in which the control is used, inside of the control. I don't want to implement them separately. When another developer wanted to use this, he/she must use the control just by instantiating. Also, it must be used more than once in a page.
Regards.
Put your buttons inside an update panel in the user control.
This Introduction to the UpdatePanels will get you started.
Then other developers can use you control on any other page without any trouble.
Update panels are server controls that internally make ajax calls and update their contents.
Should work perfectly for what you want.
Related
I am forced to use Web Forms in my project, and sadly, Web Forms only allow - If I may - "Strict" Websites to be created.
Whenever you need a Button you need to put it in a form, and then you need another button which has nothing to do with the previous button, and you can't have 2 forms,
And the idea of putting a DIV that fires a server side (C#) method is kind of difficult, okay it may be easy but all I have found are "tricks", not an "official" clean way.
So I have this idea of making a webpage for each action in my websites.
For Example:
Let's say I wanna click on the ARROW that raises the rating of this question, I would put something like this.
HTML
Rate Up
And Some CSS Codes to make it look like a beautiful button...
Okay now this will take me to a page called Rating.aspx with 2 parameters, the first parameter is the ID of the question that I would like to raise its rating, and the second parameter is either UP (+rating) or DOWN(-rating).
On the Page Load method of Rating.aspx, I would update the database, then redirect to the question page.
This will work perfectly, BUT, is it a good approach? is it professional? (put in mind that there will be many actions to preform like that...)
With ASP.NET you better use server controls. Better way of implementing that is using or , that actually renders your anchor tag. But you can attach OnClick event handler to this control (link button) so after clicking there would be automatic POST to server. The same page cycle for the current page will take place (this is called PostBack) and your attached event handler will fire, where you can actually make changes to the database. So you don't even need to create any other pages for tasks like this. Every server control has specific set of events like OnClick for buttons or OnSelectedIndexChanged for dropdown lists. You can even create your own controls or derive from existing ones and create your own events.
Take a look on following links for more information:
Button Click
Event Handling in ASP.NET
ASP.NET Page Life Cycle
I have an ASP.NET form that the user can make lots of changes to, each time they make a change the page PostsBack and the details are updated.
If the user hits the browser back button they go back through all the previous versions of the page.
Is it possible to stop each PostBack being treated by the browser as a new page?
So the would make any changes they like and if they hit the back button it brings them to the previous form and not the same form but a different version?
I know I could use AJAX to update values but I'm not an advanced coder so trying to keep things simple as I haven't used AJAX before.
Ajax is your only solution.
There is no way to remove a page from the browser history. Javascript is explicitly denied the capability.
Now, you could, potentially, stop them from using the back button at all. Although this might result in unhappy users and I'm not 100% certain it works in all browsers.
function body_onload(){
window.history.forward(1);
}
You could use a trick to do it.
On postback you can set a session bit to true saying they submitted that form. On your postback check to see if that value is set. If it is they are trying to do it again and you can just abort it. It wouldn't prevent the postback per se but you could control the logic and prevent it from DOING anything.
I personally would explore ajax as Jquery provides some nice ways to do it and it'd be a learning experience but I suppose this would work as you are asking. On a per session basis. If you only want 1 submission ever use a database to store the activity.
You could use UpdatePanel: http://msdn.microsoft.com/en-us/library/bb386454.aspx
I have a menu on a .aspx page, which will load a specific user control depending on which menu option is clicked. This part is working fine.
The problem I'm running into is when I instantiate a post back from the dynamically loaded user control, the .aspx page is reloaded. Now after researching a few of the other questions on here, I gathered that I have to recreate the User Control each time the post back is instantiated. However, the question I have might be quite simple, but how do I reload the specific User Control (and fire an event such as OnClick) based on what is posted back to the server?
So my question is: What exactly gets passed back to the server on post back, and is there something in the post back request that will allow me to load the specific control? If there is, how would I get to it?
To do it the manual way, you would have to recreate the control(s) before ViewState is reloaded, like during OnInit for example.
There is a control called the DynamicControlsPlaceHolder that takes care of persisting dynamic controls for you. It's a definite time saver, and it makes persisting dynamic content easy.
Here is a link to the control:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
To better understand what postbacks do, you need to understand the ASP.NET page lifecycle. Here is an MSDN article that explains it in detail:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
In my page I have a 'Button' when I click it, it loads an instance of my 'UserControl' to a 'PlaceHolder' .. I've used the 'UpdatePanel' but it was a hazard! .. I think it's because the Asp.Net 'Ajax' sends the whole page to the server and behind the scenes it actually causes a full PostBack. and it will help me if I could implement a partial PostBack so I won't need to save or access my database each time I reload or click the button to re-assign the values to my UserControl.
So I was hopping for some samples implementing a partial PostBack to load only one instance of my UserControl.
I can't post the code in my page here because it's a REAL MESS! .. but if you really need this to help me then maybe I could send this and post a clean solution here on the question.
Thanks for your time .. I hope there's some simple answers to my question because I'm really hopeless here!
The UpdatePanel always causes a postback to the server for the entire page, and only updates the UI's within the update panel. That's just how it will always work. If you only want to load a user control, consider using a JQuery approach to pull the user control from the server and render it via client-side JavaScript.
http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery
http://samuelmueller.com/2008/12/dynamicloader-plugin-dynamically-loading-asp-net-user-controls-with-jquery/
HTH.
I have being developing a form with the wizard control. The final step is a summary of the content that has been entered by the user as a confirmation step before submit the form.
These summary sections also catgorised into visual sections that corresponds with the each WizardSteps.
Each summary section is provided with an edit button that should allow user to edit the content then and there and update the content.
However the form should work without javascript so no use of AJAX. I'm not sure how this can be achieved? Is there a way to assign corresponding WizardStep to a placeholder onClick of the "EDIT" button for that summary section or is there any other way to do this?
Personally i would not bother trying to create a website in asp.net were one specification was that it should have to work without javascript. It is possible but your options are very limited. Here is a list of controls that don't work without javascript. I'm afraid wizard is one of them. If you must not use javascript you are going to have to create your own control that mimics what the wizard does but without JS.