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.
Related
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 am trying to pass parameter to one function in the code behind in C# from javascript
<script type="text/javascript">
$(document).ready(function () {
$("#some_id").click(function () {
var id = document.getElementById('HiddenField2');
var a = <%=btn_Click(id)%>;
});
});
</script>
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "item_id")%>' />
code behind
public string btn_Click(String item_id)
{
/*do some thing*/
return null;
}
But this piece of code always giving me error of context. That id is not defined in this context.
Can some please let me know what wrong i am doing?
First, var id is javascript and the code within <%= %> is C#. You can't pass a variable between the languages like that.
Second, the value of id in this case is going to be a DOM element which C# can't use anyways. If you want to get the value of HiddenField2 within the code behind you can use HiddenField2.Value.
Third, since you're using ASP.net, instead of using jQuery's .click handler you should use the onServerClick attribute to wire up the button click behavior to btn_Click.
The button click event in C# will be triggered by Postback when your browser post data back to asp.net
I do not understand why you use HiddenField here,so my suggestion do not consider about it
Solution 1:
first you can extract your code in btn_Click to a HttpHandler(*.ashx in asp.net),then use Ajax by using js framework like jQuery to send data to your HttpHandler,data returned by HttpHandler can be processed by js
sample code is here
Solution 2:
if your code in btn_Click is relevent to your page, just use ajax Get method, the data will send to your page,the data returned by your data will be processed by js too
In the end, if you are new to web, I recommend you to learn or implement asp.net MVC, in my opinion, it is more flexible than asp.net webform
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>");
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 am developing asp.net web application, I have a repeater that call a registered user control, I have in the user control a button that I want to call a javascript function that make Ajax call to do some action on server. this button doesn't call the javascript method, I don't know why? and when I view source I found the javascript function is repeated for every item in the repeater, how to eliminate this repetition specially that I read server items inside the function, and why the function is not called?
Thanks a lot!
sercontrol.ascx
<div id="divBtnEvent" runat="server">
<input type="button" id="btnAddEvent" class="ok-green" onclick="saveEvent();" />
</div>
<script type="text/javascript">
function saveEvent()
{
var eventText = document.getElementById('<%=txtEventDescription.ClientID%>').value;
// make ajax call
}
Problem 1: In view source I found the javascript function is repeated for every item in the repeater.
Solution:
Put your js function on the page on which the user control is being called. You also have to place your js files references on your page not on user control.
Problem 2: You are trying to get control's value as <%=txtEventDescription.ClientID%>.
And I think, this control is on user control.
Solution: Please check your page source code and see that the control's actual clientid is.
If still have issues in calling js function, check firefox's Error consol.
Hope this help.
OP said and when I view source I found the javascript function is repeated for every item in the repeater
to get rid of it put ur javascript as follows
<head runat="server">
//heres goes ur js
</head>
I guess that's ur problem
Replace the saveEvent function definition from user control onto the page where the control is used. All the way as I understand, you have use in this function textbox id from that page.
Actually, if you have place javascript block in user control's markup it will be rendered along with the rest control's markup. To avoid this you may register javascript from the server code and check is that code block is already registered.
By the way, as it is - only the last function definition being taked into attention as it redefined the previous one each time new control instance rendered.