Calling JavaScript Function From CodeBehind
You may try this :
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction","MyFunction()",true);
How to call a codebehind function from javascript in asp.net?
in JavaScript:
document.getElementById("btnSample").click();
Server side control:
<asp:Button runat="server" ID="btnSample" ClientIDMode="Static" Text="" style="display:none;" OnClick="btnSample_Click" />
C#
protected void btnSample_Click(object sender, EventArgs e)
{
}
It is easy way though...
How to call javascript function from code-behind
This is a way to invoke one or more JavaScript methods from the code behind.
By using Script Manager we can call the methods in sequence. Consider the below code for example.
ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg",
"$(document).ready(function(){EnableControls();
alert('Overrides successfully Updated.');
DisableControls();});",
true);
In this first method EnableControls() is invoked.
Next the alert will be displayed.
Next the DisableControls() method will be invoked.
How do I call a JavaScript function from Asp.Net Code Behind?
By default, the script is written to the output but without the <script>
tags. You probably would have noticed this if you were using your browser's JavaScript console or looking at the resulting HTML on the client. Make sure you familiarize yourself with those tools.
You can have it add the script tags for you with a slightly different overload of the method.
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "WantToSave();", true);
Or you can add them yourself:
Page.ClientScript.RegisterStartupScript(this.GetType(), "MyKey", "<script>WantToSave();</script>");
The combination of the key string and the type the control was registered with serve to uniquely identify the registered script, in case you later want to unregister it or replace it with a different script. So the key doesn't have to be anything specific, just something unique.
Calling javascript function from C# code behind
Suppose You're assign the value from code behind to Hidden field
countryValue.Value = "India";
stateValue.Value = "Maharashtra";
cityValue.Value = "Mumbai";
ScriptManager.RegisterStartupScript(this, this.GetType(), "com", "CallJavaScriptFun();", true);
and then calling the javascript method...the above code is set from code behind...
function CallJavaScriptFun() {
var countryValue = document.getElementById('<% =countryValue.ClientID %>').value;//here you will get the value of hidden field and assign to client side id....in our case it should be "India" as Value
var country = document.getElementById('countryId');
country = countryValue;
var districtName = document.getElementById('<% =stateValue.ClientID %>').value;
var district = document.getElementById("stateId")
district.value = districtName;
var cityName = document.getElementById('<% =cityValue.ClientID %>').value;
var city = document.getElementById("cityId")
city.value = cityName;
}
All the hidden field countryValue,stateValue,cityValue are stored in
respectively variable...and then assign to client side id....
and Follow this answer also....
How do I programmatically set the value of a select box element using JavaScript?
After Edit-
You have to put this javascript function into head part....check out this image...i have already tested it out...it's working fine...
Call JS function from code behind C#
Change your js function like this
function RowClick(filterId) {
popUpObj = window.open("voucher.aspx?param=" + filterId + "",
"ModalPopUp",
"toolbar=no," +
"scrollbars=no," +
"location=no," +
"statusbar=no," +
"menubar=no," +
"resizable=0," +
"width=530," +
"height=500," +
"left = 450," +
"top=130"
);
popUpObj.focus();
LoadModalDiv();
}
There is no need of this line now var filterId = eventArgs.getDataKeyValue('RowID');
Now you can directly use the parameter filterId
in your js function.
Related Topics
How to Remove Whitespace on Merge
Is There a Jquery-Like CSS/HTML Selector That Can Be Used in C#
Duplicate Keys in .Net Dictionaries
Is There a Built-In Method to Compare Collections
Cause of Error Cs0161: Not All Code Paths Return a Value
Find If Current Time Falls in a Time Range
Onclientclick and Onclick Is Not Working at the Same Time
Can Timers Get Automatically Garbage Collected
C# Store Functions in a Dictionary
Getting the Current Tab's Url from Google Chrome Using C#
SQL Command Insert Is Working But the Data Not Appear in Table
How to Print the Current Stack Trace in .Net Without Any Exception
How to Instantiate a Class Given Its String Name
Why Do These Division Equations Result in Zero
How to Serialize a C# Anonymous Type to a JSON String
How to Get Values of Selected Items in Checkboxlist with Foreach in ASP.NET C#