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>");
Related
I have a asp button where i would like to link to a javascript method.
<ICCM:ICCMImageButton ID="btnClose" runat="server" meta:resourcekey="btnResClose" TabIndex="9" OnClick="CloseIFrame()"/>
<script language="javascript" type="text/javascript">
function CloseIFrame() {
alert("close");
}
</script>
I have also tried this function in the j query document ready, but this had no effect.
In my C# i have added to my page load:
Page.Header.DataBind();
btnClose.Attributes.Add("OnClick", "CloseIFrame();");
When i try run my project i get the error method name expected. which points me to the line my button is on.
This all the info i get, am i missing something?
OnClick in asp.net controls is for server-side methods. Try OnClientClick instead.
How about
OnClientClick="CloseIFrame()"
ICCMImageButton is a server control. This would usually mean that the OnClick handler would be expected to be a server side handler.
The typical syntax for a client side handler would be OnClientClick.
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>
I have page with other asp.net page inside iframe.
And on button click inside iframe i need to refresh main page from server side.
How can it?
Make use of javascript and you can easily do it
call the following function on your button click
<script language="javascript">
function RefreshParent()
{
window.parent.location.href = window.parent.location.href;
}
</script>
From the cs code if you are opening the aspx page in the iframe
Page.RegisterStartupScript("RefreshParent","<script
language='javascript'>RefreshParent()</script>");
Its explained very well in the following links:
link 1
link 2
Hope it helps.
For some reason the javascript function shown in earlier answers did not work for me (although the function was called). However, this worked for me:
function RefreshParent()
{
// Was: window.parent.location.href = window.parent.location.href;
parent.location.reload();
}
I had to add it near the start of my HTML (near the end of the HTML didn't work, as it was not yet rendered).
I used this C# code to call it, which is based on earlier answers but updated to use the current API which has an extra initial paramter, "type":
ClientScript.RegisterStartupScript(this.GetType(), "RefreshParent", "<script language='javascript'>RefreshParent()</script>");
i wonder if there is any way to call an asp method that returns a string or something when you are in <script></script> tags.
Like this (it does not work):
<script language="javascript" type="text/javascript">
function isValid() {
var required = "<%= callAspMethodThatReturnsSomething() %>";
}
</script>
any help with this? thx!
There is no direct way to call c# method like in example above. But you can send $.ajax request to the server, or even send postback using _doPostBack(..,..)
The issue is that you are calling a method on the master page from the content page directly.
You can't do that - you need a reference to the strongly typed master page.
Try this (asp.net 4.0):
<%:((MasterPageClassName)Page.Master).callAspMethodThatReturnsSomething()%>
Or this (pre 4.0):
<%=((MasterPageClassName)Page.Master).callAspMethodThatReturnsSomething()%>
<% callAspMethodThatReturnsSomething() %>
this method in <% %> will work before what you want to do ,it may looks like does not work
yet,you can create a new button on page ,make it unvisible and use it's click event like document.getElementById("<%=button1.ClientID%>").click()
I have a div inside asp:content :
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="slidebar" style="display:none;" >There are some pending approvals.Please approve by the 25th of this month
Click here to close <sup style="font:caption;color:#373636;">X</sup>
</div>
<script language="javascript" type="text/javascript">
function showbanner()
{
document.getElementById("slidebar").style.visibility="visible";
}
</script>
<\asp:content>
and the code behind:
ClientScript.RegisterClientScriptBlock(Page.GetType(), "displaybanner", "return showbanner();", true);
I cannot call the function showbanner from the code behind and even when I directly call the statement inside showbanner with registerclientscript ...it doesnt get called .
Please help
The properties visibility and display are not the same, change the js function to:
function showbanner()
{
document.getElementById("slidebar").style.display="block";
}
also change your code behind to
ClientScript.RegisterStartupScript(Page.GetType(), "displaybanner", "showbanner();", true);
so the script executes after the page has load or else it won't find the element.
I'm not sure why your register script isn't working but have you tried putting the javascript call directly on the page inside a script block? If that works then wrap the script block in a pannel at the bottom of the page with visible="false". In your code behind when you determine that you want it to work set the visibility to true.
If you are using Jquery I would also wrap the contents of your script block in:
$(document).ready(function() {
//javascript call here
});
Just to make sure it doesn't get called before the page can actually handle it.
This all assumes of course that your function call is good and that the issue is with register client script.
Okay one of my team mebers Mahi came up with a solution...rather than CleientScript.reg.....
we used Page.RegisterStartupScript and it works fine :))