OK so I have a jquery modal dialog. It accepts an input and then has an ok and and cancel button. Cancel works fine by simply closing the dialog. The ok button fires the OnClientClick when I really want it to fire the OnClick method so I can go into the server controls and log the input into the database using ASP.NET. Any ideas on how this is supposed to be done using jquery?
Side note: I'm not currently by the computer that has the code, but I'll try to update it as soon as I can.
You could try this:
Insert a ASP.NET button on your form, binded to the event that you want to trigger, then, with jQuery do $('#your_button_ID').click(); on the 'OK' event of your dialog.
Hope it helps!
In your codebehind create a static function and add the [WebMethod] attribute, so now you can fire code behind methods from Jquery.
javascript:__doPostBack('IDOfYourControlButton','')
This is the same method a button or linkbutton uses to fire the onClick event in .NET
Demo webmethod
in code behind you add this
[System.Web.Services.WebMethod]
public static string SayHi()
{
return "Hi";
}
in yr aspx file you add this javascript
<script>
function GetHi() {
PageMethods.SayHi(onComplete);
}
function onComplete(result) {
alert(result);
}
GetHi();
</script>
and dont forget to add this also in yr site.master or aspx page right under the
<form runat="server">
<asp:ScriptManager ID="scriptManager" EnablePageMethods="true" runat="server"/>
</form>
Related
I have an iframe in asp.net, using c# .
When the iframe with a story ends, I want to fire a button in parrent window via java script.
The iframe window has its own html file and js file. when the iframe come to an end of story
in the js file run
case "ClosePlayer":
trig();
break;
then I call another js file with a simple function
function trig()
{
//$('#<%= SubmitButton.ClientID %>').click();
//document.getElementById('<%= SubmitButton.ClientID %>').click();
document.getElementById("SubmitButton").click();
// alert("javascript here");
}
in the main window in asp.net I have this button
<asp:Button ID="btnProceedToNextTab" runat="server" Text="Proceed" OnClick="btnProceedToNextTab_Click" />
and I want to trigger this with another button via javascript, so the code behind of the function btnProceedToNextTab_Click(); run
<asp:Button ID="SubmitButton" runat="server" Text="Button" OnClientClick="trig();" />
Can anyone help me? I tried to use jquery, but nothing fires the button. The alert messages show ok, but the button is never clicked.
Try something like this in your parent window:
$("#iFrame").contents().find("#ButtonInIframeWindow").live('click',function(){
$('#submitButtonInParentWindow').click();
});
this is false:
document.getElementById("SubmitButton").click();
you have to use jQuery selector like this:
$("#SubmitButon").click();
Can you try this...
You can invoke your click function in javascript function..
document.getElementById('btnProceedToNextTab').click();
Please feel free to mark as answer if it satisfies....
ok found it.
from child (iframe window) call a js file with trig();
function trig()
{
var proceedButton = parent.$('[id$="btnProceedToNextTab"]');
proceedButton.trigger('click');
return false;
}
and in parent window:
<asp:Button ID="btnProceedToNextTab" runat="server" Text="Proceed" Style="display:none;" OnClick="btnProceedToNextTab_Click" />
works for me! thanks.
I have a radRotator whit an OnItemClick method which is fired when the user click on one of the items while rotating.
In the OnClick method, I get the index of the item clicked and then I make use of it.
The problem is that every time that a click is performed in the radrotator, it stops and restart all over again. I suppose this is because of the post back generated by the OnItemClick.
How can I disable the post back on the OnItemClick but still fire the command?
<telerik:RadRotator ID="RadRotator1" RotatorType="AutomaticAdvance" ScrollDirection="Up"
ScrollDuration="4000" runat="server" Width="714"
ItemWidth="695" Height="260px" ItemHeight="70" FrameDuration="1" InitialItemIndex="-1"
CssClass="rotator" OnItemClick="RadRotator1_ItemClick">
OnItemClick will do a postback. I think you will have to call server side function from javascript. Since you are using telerik controls, you can use either RadAjaxManager or RadAjaxPanel to do the server side call.
$find("<%= RadAjaxPanel1.ClientID %>").ajaxRequest();
The above line will make a call to RadAjaxPanel1 ajaxrequest event.
Updated code -
<telerik:RadRotator ID="RadRotator1" RotatorType="AutomaticAdvance" ScrollDirection="Up"
ScrollDuration="4000" runat="server" Width="714"
ItemWidth="695" Height="260px" ItemHeight="70" FrameDuration="1" InitialItemIndex="-1"
CssClass="rotator" OnClientItemClicked="itemclicked">
<script type="text/javascript">
function itemclicked(sender, args) {
// you will have to make a server side call from here
// if you have RadAjaxPanel or RadAjaxManager on this page you can call ajaxRequest to //make server side call.
$find("<%= RadAjaxPanel1.ClientID %>").ajaxRequest();
}
</script>
I'm using Telerik controls in my project and I am using RadTabStrip for my purposes
<telerik:RadTabStrip ID="tsRequisitions" Skin="" MultiPageID="mpRequisitions" runat="server" Width="980" ScrollButtonsPosition="Right" ScrollChildren="True" OnTabCreated="tabCreated">
As you can see in this template I call tabCreated method every time when new tab has created. Now I want to call some javascript function from server-side(all mentioned is in RadAjaxPanel).
I've tried to use RegisterClientScriptBlock, but it didn't help me to fire my javascript function.
if (!ClientScript.IsClientScriptBlockRegistered("tabSelected"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),
"tabSelected", "TabSelected();", true);
}
And I have this in my .aspx file
<script type="text/javascript">
function TabSelected() {
console.log("dfgdfgfdg");
}
</script>
How can I call my function from code-behind after AJAX postback?
The following code snippet do the trick
RadScriptManager.RegisterStartupScript(this,this.GetType(), "tabSelectedScript", "TabSelected();", true);
RegisterStartupScript is static method of RadScriptManager class (I'm using Telerik controls here again, but maybe it will work with asp.net standart ScriptManager too).
Have you tried this ?
Page.RegisterStartupScript("OnLoad", "<script>TabSelected();</script>");
I Have a small working application on ASP.NET and C# and I would like to add some Javascript to it.
I know that for example I can use the Confirm button and then do something on yes or no.
What I would like to do is call some of the functions I have in the code-behind, depending on the answer to the Confirm Button.
How do I go about this?
Many Thanks!
Simple.
Put a hidden div on your aspx page that contains button triggers to your methods:
<div style="display: none">
<asp:Button ID="TriggerMethod1" runat="server" />
</div>
In your javascript, in the confirm part of your code simply do:
__doPostBack('TriggerMethod1', '');
And in your code-behind, handle up that button click to call Method1.
I would create an ASHX handler file and post back to the hander using jQuery ajax methods.
See an article on this here:
Using jQuery in ASP.NET apps with httphandlers
To call a server side method on a client side event you need to do the following:
1- Create the server side method:
void DoSomething(...) { ... }
2- Implement the System.Web.UI.IPostBackEventHandler.RaisePostBackEvent which take one string argument (You can assign the name to the value of this argument).:
public void RaisePostBackEvent(string eventArgument)
{
DoSomething(...);
}
3- Write a script to trigger post back:
function TriggerPostBack(control, arg){
__doPostBack(control, arg);
}
4- Call the PostBack trigger function when needed:
<a .... onclick="TriggerPostBack('control', 'arg')" .. />
Have you tried to use the search first in the site!!
Call ASP.NET function from JavaScript?
Calling functions from an ASP.NET code file with javascript
or https://stackoverflow.com/search?q=Call+C%23+Functions+From+JavaScript
i have a popup that is getting displayed when Save button is clicked. The popup has 2 buttons. Yes and No. No should cancel the popup
and yes should take you to function in the code-behind say, btnSave_Click(object sender, Eventargs e). How is it possible. Could someone help me, i am new to Javascript.
Below is the code where i am showin the popup.
var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
if(mdlPopup)
{
mdlPopup.show();
}
To do this you will need to set your server side function as a web method like this:
Add to the top of your code behind:
using System.Web.Services;
using System.Web.Script.Services;
Then decorate your method with these attributes:
[WebMethod(), ScriptMethod()]
public static void btnSave_Click(Object sender)
{
//Stuff
}
To call this from the client side (Javascript) do this:
PageMethods.btnSave_Click(this,btnSave_Click_Finished);
You can place that in a client click event. The first argument is the sender parameter, the second is the javascript function to call when the server side method has completed.
You can't call server side code from JavaScript directly. Make a postback or fire a XHR (AJAX) request in the background.
I think it is possible for you to acess the serverside script by using javascript.
__doPostBackis a function which is behind the asp postback and this function is called when a button is clicked or dropdown value is changed.
For more details pls refer to [this.][1]
All you need is to place a <asp:Button ID="btnSave" runat="server" onClick="btnSave_Click" /> in the form and in the javascript (ie button click function) just call the __doPostBack('<%= btnSave.UniqueID %>', '');
So it will calls the serverside function.(btnSave_Click)
Notice that you need to give '<%= btnSave.UniqueID %>' as the firstargument.
The above will works as similar to a server button click
The another way is to make a post or ajax using jquery.post or jquery.ajax it is possible to send request asynchronously to the server.Here you want to pass some query string and call the appropriate function in the page_Load
This will do without any postback
One another method is to use PageMethods from clientside by defining a static WebMethod
[1]: http://msdn.microsoft.com/en-us/library/aa720099%28vs.71%29.aspx/"Call serverside function from clientside"
I hope any one of these will solve your problem
the Yes button should be an asp.net control with a server-side handler
markup
<asp:button id="yesButton" runat="server" onclick="yes_click" />
codebehind
void yes_click(object sender, EventArgs e) {
// TODO your thing here.
}
the other buttons can be standard html inputs with javascript event handlers. if you are using javascript to alter element style, the changes won't persist across a postback- when the yes button submits and the page reloads, the popup won't be visible.