I am calling Showmenu() JavaScript function from C# and passing one variable to this function. Now I want to use this variable in another function of JavaScript.
<script type="text/javascript" >
var strmenu;
function ShowMenu(strmenu) {
alert(strmenu);
}
alert(strmenu);
ddsmoothmenu.init({
mainmenuid: strmenu,
orientation: 'h',
classname: 'ddsmoothmenu',
contentsource: "markup")}
</script>
I am calling ShowMenu(strmenu) function in c sharp.....like
menu_Sysadmin.Attributes.Add("OnClick", "javascript:return ShowMenu('sysadmin')");
I want to use strmenu from showmenu() in ddsmoothmenu.init() as a parameter. alert shows value but when I am trying to use strmenu as globally, it is not working.
Your strmenu parameter hides the global variable. You have to rename either the global variable or the parameter of your function and perform an assignment in your function.
var menu;
function ShowMenu(strmenu) {
menu = strmenu;
}
Related
I look it up for a while but couldn't find the answers I'm looking for.
I'm create an new TextBox from scratch using the IScriptControl and ScriptManager.
everything works fine inside my scriptcontrol but I'm wondering how I can trigger a function inside the scriptcontrol from outside.
to give an example. Lets say I have this:
Type.registerNamespace('QuyoDevLib.Web.UI');
QuyoDevLib.Web.UI.InputBox = function (element) {
QuyoDevLib.Web.UI.InputBox.initializeBase(this, [element]);
this._element = this.get_element();
}
QuyoDevLib.Web.UI.InputBox.prototype = {
initialize: function () {
QuyoDevLib.Web.UI.InputBox.callBaseMethod(this, 'initialize');
},
dispose: function () {
QuyoDevLib.Web.UI.InputBox.callBaseMethod(this, 'dispose');
},
doSomething: function() {
//do something usefull
}
}
QuyoDevLib.Web.UI.InputBox.registerClass('QuyoDevLib.Web.UI.InputBox', Sys.UI.Control);
if (typeof (Sys) !== 'undefined')
Sys.Application.notifyScriptLoaded();
$create(QuyoDevLib.Web.UI.InputBox, null, null, null, $get("TestControl"))
How can I make this underlaying javascript work to trigger the "doSomething" function inside my scriptcontrol?
What I had in mind you see under here but this doesn't work I already tested it.
<script>
function test() {
$get("TestControl").doSomething();
}
</script>
k I looked a bit more and found my own solution.
I share it here so everyone with the same question or problem can use it.
I had to change $get into $find to make it work.
<script>
function test() {
$get("TestControl").doSomething();
}
</script>
Context:
I converted HTML Select to Multiselect checkboxes on runtime. This required that I write my javascript code on the jquery event :
$(window).load(function () {
mulDC(); //this func converts loaded HTML select to ul with checkboxes
});
Now whatever user selects/checks on the run, I need those values back in my C# code behind.
What I have already tried is,
I created Hiddenfield and used it to populate it:
$('#<%=hdnDC.ClientID %>').val(arrSelectedValues);
This is returning me empty since this piece of code is inside .load method. I cannot use document.ready.
Need help here :)
UPDATE:
Function mulDC(). If code for .multiselect (external js file) is important, I can attach that too.
function mulDC() {
$('#chkDC').multiselect({
includeSelectAllOption: true,
renderInDiv: '#accDC',
showSelectedIn: '#selDC',
showSelectedValueIn: '#selDCVal',
chkBoxesID: 'chkDC'
});
getAllValueDC();
$('#chkDC li input:checkbox').on('change', function () {
getAllValueDC()
});
function getAllValueDC() {
var sThisVal = $('#chkDC :checkbox:checked').map(function () {
return this.value + ',';
}).get();
$('#<%=hdnDC.ClientID %>').val(sThisVal);
}
}
coming from c family language (i use c#) makes Javascript model confusing somehow.
in c# you have to have a class. then instantiate the object from that class to use it.
in Javascript there is no class . it is all about functions. Function in itself is an object and it can be used directly without instantiating .
for example:
function sayHi(name){
alert ("hiiiii " + name);
}
sayHi("John");
So the function sayHi is already an object and instantiated and working !!
Now if you want to access its property prototype you can do that like following:
function sayHi(name){
alert ("hiiiii " + name);
}
sayHi.prototype.nn = 20;
sayHi("John");
alert(sayHi.nn);
but the above code will fail to alert the nn as 20; it will give undefined !!
however if you make the sayHi function a constructor function to another variable then this prototype property will be accessable like the followin:
function sayHi(name){
alert ("hiiiii " + name);
}
sayHi.prototype.nn = 20;
sayHi("John");
alert(sayHi.nn);
var hi2 = new sayHi("May");
alert(hi2.nn);
now alert(hi2.nn) give you 20 ; which means the prototype of the sayHi was not accessible till we instantiate it using the word new assigning it to another variable.
My question:
Isn't this similar to the c# class instantiation?
and since the function sayHi is already a Function object ; what
is the purpose of making its prototype not accessible unless it
is a constructor function to another variable?
CODE DEMO
I have a function named "callfunction()" in JavaScript(Mypage.aspx) .This function should call another function "func()" in C# (Mypage.aspx.cs )
Something like this:
(in Mypage.aspx)
function callfunction()
{
// i have to call func() function here .....
}
</script>
(in Mypage.aspx.cs file)
public void func()
{
// My code goes here
}
I have researched alot because of this and i ended up so far with 2 conclusions:
1st was to use Json, but my superiors said clearly that they dont want me to do so.
2nd was that i cant do as i wish because of the client, server aspnet protocol
Please give me any kind of tip in how to do this, i am getting desperate
Ok....Try using page methods
First add a script manager on your aspx page
<asp:ScriptManager ID="scpt" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Then go to your aspx.cs page and declare a function something like
[System.Web.Services.WebMethod]
public static string ValidateUser(string emailId, string password)
{
//Your logic code
return returnString;
}
Then from your javascript call the c# method like
PageMethods.ValidateUser(email, password, CallSuccess_Login, CallFailed_Login);
And also in ur javascript create 2 call back functions CallSuccess_Login and CallFailed_Login
Hope it helps
If it's a webforms project (not MVC) and you don't want to use AJAX, you can use __doPostBack.
<script type="text/javascript">
function callfunction(parameter)
{
__doPostBack('func', parameter)
}
</script>
C#:
public void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"]; // parameter
var senderObject = Request["__EVENTTARGET"]; // func
if(senderObject == "func")
{
//call your function here, or write the implementation
}
}
below are the options available to you
If your using asp.net then use Ajax tools to create this
if you don’t want to user Ajax toolkit use JavaScript __doPostBack
or other option write server side function in the web service and call web method using JavaScript
Sorry I could not find the appropriate title
Here is my problem
I am using a method which returns a string value like hh:mm(12:45)
this method is named as DeliveryTimeCalc()
I am using a jQuery timepicker to take input on my aspx page
this timepicker has to be validated by mintime
the minimum time should be the value returned by the method DeliveryTimeCalc()
this serverside method has to be called during the jQuery is initialized
so I did the below method
<script type="text/javascript">
$(document).ready(function () {
//window.onload = pageMethodConcept.callServerSideMethod;
var time = pageMethodConcept.callServerSideMethod;
alert(time);
var hm = time.split(':');
var h = hm[0]; var m = hm[1];
$("#tb_DeliveryTime").timepicker({ showPeriodLabels: false,
onHourShow: OnHourShowCallback,
onMinuteShow: OnMinuteShowCallback
});
function OnHourShowCallback(hour) {
if ((hour < h)) {
return false; // not valid
}
return true; // valid
}
function OnMinuteShowCallback(hour, minute) {
if ((hour == h) && (minute <= m)) { return false; } // not valid
return true; // valid
}
});
</script>
<script language="javascript" type="text/javascript">
pageMethodConcept = {
callServerSideMethod: function () {
PageMethods.DeliveryTimeCalc(pageMethodConcept.callback);
},
callback: function (result) {
alert(result);
//return result;
}
}
//window.onload = pageMethodConcept.callServerSideMethod;
</script>
but the problem is that it is not returning the value (hh:mm)
I am getting a alert box which contains output like
function () {
PageMethods.DeliveryTimeCalc(pageMethodConcept.callback);
}
even if I use return I am getting the same value
but if I use
window.onload = pageMethodConcept.callServerSideMethod;
I am getting a alert box which contains output like hh:mm
Please Help!
Try simply changing:
var time = pageMethodConcept.callServerSideMethod();
with trailing () : you have to assign the return value of the function to the variable, not the function itself, but:
window.onload = pageMethodConcept.callServerSideMethod;
works as expected because you're assigning an handler to an event and you have no parameters to pass along with the function: this in fact it could be written as well as:
window.onload = function() {
pageMethodConcept.callServerSideMethod();
}
Thank you guys for helping
but i have found the solution myself
Sol 1
Using Web Services
http://markitup.com/WebServices/TimeZones.asmx?op=CurrentDateTime
this is a web service in which we have to enter the name of the time zone
Ex:(India Standard Time)
Sol 2
Write a method in the pageload and in that method write your code.
When you have to return the result
place the result in a labe text and make the visibility of the label to false
(Note: You can not find the label in javascript if the visibility is set to false
So do this instead of making visibility false. style="display: none;" this does the same
but in JavaScript you can find the label)
you can find the label in javascript by using
var time = $("#lbl_time").text();
Since we coded the jquery as $(document).ready(function () the jquery will be executed only after the page load is executed
So Problem Solved
Thank You
-Krishna Thota