Can button click event goes to controller instead in javascript - asp.net-mvc-3

The click event is in javascipt. Can I make it to be in controller? I want to write in C#, not in Javascript. Here is the code for my button:
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Add"
Width="112px" />

If I understand right your question, you have to use a Form tag in your page or via JQuery.onClick() send an ajax, post or get, to the controller and from it return a JSON with your data

Related

passing input text's value to server side on click of a button in JSF

I have a text box which takes a search value, and i want to send this string to the server side on click of a button. Not by a form submit, by an ajax call.
I had added an actionListener to the input tag itself, which is called on blur. But what i really want is for the user to click the button to trigger the search function.
I got an idea from this question, and implemented it this way:
<h:inputText id="likeMaterial" value="#{createBookingForm.searchText}"></h:inputText>
<a4j:jsFunction name="setParameterAndRerender" actionListener="{bean.searchMaterials}" reRender="searchResult">
<a4j:actionparam name="param1" assignTo="#{createBookingForm.searchText}"/>
</a4j:jsFunction>
<h:commandButton value="Search" onclick="setParameterAndRerender('mySearchText');return false;"></h:commandButton>
The value received at server side is of course, "mySearchText". How do i pass what the user enters? Or how do i bind #{createBookingForm.searchText} before the button's action listener is called?
Im open to any other approach to. I have limitations though : Im working on enhancing a legacy application, built using JSF 1.1. I cant upgrade, not without a fight at least!
Edit : I tried doing it this way, but i get "undefined" on the server side.
Why not use a4j:commandButton instead of h:commandButton? It will execute ajax request and render what you want. No form submit will happen. Loks like what you need.
h:commandButton by default submit the form when clicked. So no need to send specially using a4j:jsFunction or in any other way. You can completely remove your js function unless if you have something else to do. If you want to test that add an action method and print the value of searchText variale in createBookingForm bean.
Hope this helps!!
This is the whole solution
<h:inputText id="likeMaterial" value="#{createBookingForm.searchText}"></h:inputText>
<a4j:commandButton reRender="searchResult" actionListener="#{createBookingForm.searchMaterials}">
<a4j:actionparam name="param1" noEscape="true" value="document.getElementById('likeMaterial').value" assignTo="#{createBookingForm.searchText}" />
</a4j:commandButton>

JSF + Seam + a4j Triggering one component ajax from a different component event

I wanted to ask just what title says:
say we have a inputtext and a button. Well, I wanted to submit an ajax request in the inputtext when button is clicked.
for example, from the inputtext perspective, I want to achive something like this:
<h:inputtext>
<a4j:support event="button.onclick"/>
<h:inputtext>
<h:button id="button">
or, from the button perspective:
<h:inputtext id="input"/>
<h:button id="button">
<a4j:support event="onclick" action="input.submit"/>
</h:button>
Don't know if there exist an easy way to get this done.
thanks in advance!!
If you just want a simple form submit, I think both approach are wrong, as long as you put your inputtext and button properly in a form, the form will automatically submit whatever you have in this form including user's input in inputtext when you click the button. There is no need to do something like input.submit yourself. (and it's not correct, either)
For the following case, you will see the setInputValue() of managed bean being invoked when you click your button.
<h:inputtext id="input" value=#{bean.inputValue}/>
public void setInputValue(String inputValue){
this.inputValue = inputValue;
}
Try using
Richfaces Region component to limit your request to diffrent regions.
http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/a4j_region.html
I finally get this done by means of <a4j:jsFunction>
a4j:jsFunction

apex:actionFunction and Database.Update() unintended page refresh

So, I've been working on an "edit item" modal for a visualforce page that will allow the user to edit a child component of an object, and refresh the page. As it stands, there is a single URL parameter that contains the parent objects ID. The code is structured very similar to this:
<apex:form id="edit-modal">
<!-- Modal Content-->
<apex:actionFunction action="{!updateModalObject}" name="updateModalObject">
</apex:actionFunction>
</apex:form>
When the user preses a "save" button in the modal, the aforementioned actionfunction is called. The class in the controller looks like this:
public PageReference updateModalObject(){
database.update(modalObject);
return null;
}
When this action completes, the page is refreshing, also dropping the URL parameters and causing the whole thing to sort of.. gum up. I'm not sure which portion of the code is causing the refresh, if it's the actionfunction or if it's the update in the controller.
You said:
When the user preses a "save" button...
I think this is causing the page refresh. Do you use the command button without reRender tag? The solution might look like this:
Just try this trick out - add return false after executing your JavaScript function:
<apex:commandButton value="Save" onclick="updateModalObject(); return false;"/>
Other way to avoid page reload is to add a dummy reRender tag:
<apex:commandButton value="Save" onclick="updateModalObject()" reRender="none" />
or
<apex:actionFunction action="{!updateModalObject}" name="updateModalObject" reRender="none">
You must to re-render "something" to avoid the page reload. Otherwise the command button will reload the whole page. In our case we will re-renden "nothing".

UpdatePanel does not fire on first click of link button

I have an update panel on an .aspx page. Within this aspx page I have a few ascx controls. One of the controls has my link button. When that button is pressed I'd expect to get the results I intend to get, but I expect to not see a page flicker or see the page postback. It does both. After I change some search information and click the link button again the page does an async postback (which is what I expected on the first click). After running this sequence through fiddler I see that the first time I click the button I am missing some information that is included in all subsequent requests.
ctl00$ScriptManager1=ctl00$cplContents$updatePanelOrderSearch|ctl00$cplContents$ucOrderSearchControl$btnRange&EVENTTARGET=ctl00%24cplContents%24ucOrderSearchControl%24btnRange&.....
Above is what I get on clicks 2, 3, etc.. This is not in the request when I click on the link button the very first time. Im wondering if this is why I am get a full, non async postback the first time.
Any help would be appreciated. My update panel code is below.
<asp:UpdatePanel ID="updatePanelOrderSearch" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<uc:control1 "this control has the link button" />
<hr />
<div id="SearchResults">
<div id="SearchResultsMessage">
<asp:Literal ID="lblMessage" Text="No orders found" Visible="false" runat="server" />
</div>
<uc:contorl 2 />
</div>
<uc: control3 />
</ContentTemplate>
</asp:UpdatePanel>
Thanks
this was an issue from months ago and we wound up not using this solution

send data from fbml dialog window to another page

how do i submit data from a form inside fbml modal dialog window
You can do it by multiple ways [Mock ajax , Send data by URL], but still i need to test that normal action mechanism is working or not ,
Surely using Mock ajax you can accomplish your task
Here is an example (Directly from FB wiki , if you need more info please notify me )
<fb:dialog id="my_dialog">
<fb:dialog-title>My Little Dialog</fb:dialog-title>
<fb:dialog-content><form id="my_form">Do you like my little dialog?</form> </fb:dialog-content>
<fb:dialog-button type="submit" value="Yes" clickrewriteurl="http://www.someurl.com/response.php" clickrewriteid="my_dialog" clickrewriteform="my_form" />
</fb:dialog>
I don't know what link to put, how about this one? The contents of http://www.someurl.com/response.php should print out FBML wrapped in fb:dialogresponse.
<fb:dialogresponse> <fb:dialog-title>My Little Dialog Part II</fb:dialog-title>
<fb:dialog-content>I'm glad you like my dialog</fb:dialog-content> <fb:dialog-button type="button" value="Close" close_dialog=1 />
</fb:dialogresponse>

Resources