How to get value from javascript to C# code behind ? I have an idea with the code below. In javascript code I want to assign the value of "HiddenField" Control with string value and then I want to take this value from "HiddenField" in code behind. But with this code I cannot do it. Can you tell me how to make it ?
<script>
$(function () {
document.getElementById('HiddenField').value = "active";
console.log(<%= this.HiddenField.Value %>)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
you need to use ClientID property of control to get actual element ID in DOM.
<script>
$(function () {
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
console.log(document.getElementById('<%= HiddenField.ClientID%>').value)
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" Visible="true" />
Use the Control ID for HTML markup that is generated by ASP.NET.
document.getElementById('<%= HiddenField.ClientID%>').value = "active";
When a Web server control is rendered as an HTML element, the id
attribute of the HTML element is set to the value of the ClientID
property. The ClientID value is often used to access the HTML element
in client script by using the document.getElementById method.
Then send the Hidden value through the javascript function as a variable while calling the controller
Surely works,
Cheers
Phani*
you may take a look at mshtml
As far as I know you call with this C# functions from your javascript code ;-)
Related
I need to get a value that I got in Javascript to my code behind file..
My design code is:
<asp:DropDownList ID="ddlStatusOverview" runat="server" Width="95px" onchange="CheckSelectedItem(this)" >
</asp:DropDownList>
I'm using the dropwonlist in "ItemTemplate" of asp:ListView.
My javascript coding is:
<script type="text/javascript">
function CheckSelectedItem(ddl) {
alert(ddl.value);
}
I want to get that "dll.value" value in code behind.. I already used Webmethod concept. But my problem is I need to get the value in ".ascx" page.. How to do that..?
I don't know how to use hidden field value concept too..
Do this :
Add a hidden field element :
<input name='lala' id='lala' type='hidden'/>
Add this :
function CheckSelectedItem(ddl) {
document.getElementByID('lala').value=ddl.value;
}
On server side :
you can get the value by :
Request.Form["lala"].ToString();
Accoring to your comment :
If I want to call a server side function from javascript with this
"ddl.value" means, how to do that?
Please read this
ASP.NET pass a javascript value in server side
Introduce a hidden field.
<script type="text/javascript">
function CheckSelectedItem(ddl) {
document.getElementByID('hfStatus').value = ddl.value;
// if using jQuery
// $("hfStatus").val(ddl.value);
}
</script>
<asp:HiddenField runat="server" ID="hfStatus" />
Now you can directly access hfStatus from code-behind.
When I call <%= this.HiddenField.Value %> the value of HiddenField control in this case remains the same state (5) ? But when I call it with console.log(document.getElementById('<%= this.HiddenField.ClientID %>').value); this return the changed state in this case "active", why? How I can get the changed value in code behind (I want <%= this.HiddenField.Value %> to return "active"(the changed value)) ?
code:
<script>
$(function () {
document.getElementById('<%= this.HiddenField.ClientID %>').value = "active";
console.log(document.getElementById('<%= this.HiddenField.ClientID %>').value); // this return te changed value "active"
console.log('<%= this.HiddenField.Value %>') //this again is 5 not "active"
});
</script>
<asp:HiddenField ID="HiddenField" runat="server" Value="5" />
You are confusing server side and client side code and when they run.
Server side code runs first. This is what's in the <%=%> blocks. In it, when using this.HiddenField.Value, it will output the server side value of the control, before it has been changed client side. this.HiddenField.ClientID will output the control ID as output by the server so you can get it from client side code.
When the page first loads, the <%=%> sections will be replaced with the server side values.
This would look like (in the browser once the page loads do - view source to see what was actually rendered to the browser):
<script>
$(function () {
document.getElementById('someId').value = "active";
console.log(document.getElementById('someId').value); // this return te changed value "active"
console.log('5') //this again is 5 not "active"
});
</script>
<input type="HiddenField" ID="someId" Value="5" />
Then, and only then, will client side code run, with the results you have seen.
In the following line
console.log('<%= this.HiddenField.Value %>')
<%= this.HiddenField.Value %> is evaluated at server so in the browser its
console.log('5')
because that's the value of the expression.
<%= whatever %> evaluates whatever when the page is rendered on the server.
If you look at the HTML sent to your client, you will find something like
$(function () {
document.getElementById('blahblah_HiddenField').value = "active";
console.log(document.getElementById('blahblah_HiddenField').value);
console.log('5')
});
The 5 is rendered by the server - there's nothing on the client's end linking that 5 to the value in the hidden field, so it doesn't get updated when the hidden field's value changes.
It happens because the asp.net control shows the "server" value, that is 5. With javascript you are modifing the dom on the client side, the asp.net control value doesn't change until you post the page.
Or you can use ClientIDMode
<asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static" />
Set and Get;
in jquery
$('#HiddenField1').val('active');
console.log($('#HiddenField1').val());
in javascript
document.getElementById('HiddenField1').value = 'active';
console.log(document.getElementById('HiddenField1').value);
I am trying to call c# properties in .aspx page, where myfunc is jquery function.
this function take parameters which i am passing through c# public properties. and I want to call this function as soon as div is rendered.previously i have done this on prerender event in ascx control. wha is exact ssyntax for below code and calling jquery function from here is valid?
<div class ="v" onload ="Javascript:myfunc('<%= this.articleID %> '
,'" +<%=this.UserID %>+"','" +<%=this.EncryptedUserID %>" +','" +<%#
this.ItemIndex %>)">
thanks
Edited
Ok If i do like this way
<div class ="v" >
<script language ="javascript" >
JGetTotalVotes(<%= this.articleid %> ,<%
this.ThisEncryptedUserID %>,<% this.ItemIndex %>,'aaa');
</script>
I do not want server side hidden fields solutions and if i use hiden field ( non server html tag) then i still need to call <%%> to fetch value from server side to hiddent field
I see you are missing single quote after the last parameter if that is not the problem.
You can use hidden fields and call the function in javascript
Add this to your page
<asp:HiddenField runat="server" ID="hdnArticleId" ClientIDMode="Static" />
<asp:HiddenField runat="server" ID="hdnUserId" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnEncryptedUserID" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnItemIndex" ClientIDMode="Static"/>
bind those hiddenfields in Page_Load event remember to add ClientIDMode="Static" to set the id as you set not a generated one by asp.net to have the ability to use them in javascript
Add To your javascritp
function YourFunction(){
var ArticleId = document.getElementById("hdnArticleId").value;
var UserId= document.getElementById("hdnUserId").value;
var EncryptedUserID= document.getElementById("hdnEncryptedUserID").value;
var ItemIndex= document.getElementById("hdnItemIndex").value;
// your code goes here
}
and don't forget to call your javascript function in page onload
Div elements don't pull in external content, so they don't have load events.
If you want to run some JS after a div element has been parsed:
<div></div>
<script> foo(); </script>
<% string s=LinkButton_ID.Text; %>
<asp:LinkButton ID="LinkButton_ID" runat="server" Text="LinkButton_Text" onclientclick="JscriptFunction(...)">
</asp:LinkButton>
I want to send the "s" as parameter to JscriptFunction. I try JscriptFunction(<% s %>); but doesnt work. Do you have any suggestions? thank you
Try this:
JscriptFunction('<%= s %>');
Don't forget to quote the text value in your JS function call.
EDIT:
I didn't notice that your call to JscriptFunction is inside your server tag. If you don't have access to jQuery, the easiest way to pass the client-side text of the button is probably this:
onclientclick="JscriptFunction(this.innerHTML)"
Here's an example working on jsFiddle.
You can also pass the client-side ID of the control and manipulate the value using JavaScript. Make sure you have ClientIDMode="Predictable" or ClientIDMode="Static" set on the control if you do it this way.
You'll need to declare your code section to be javascript, and then you need to output the asp.net/c# value as a string. The easiest way is something like:
<script type="text/javascript">
var s = <% Response.write(LinkButton_ID.Text); %>;
//do your other javascript stuff
</script>
or
<script type="text/javascript">
var s = <%= LinkButton_ID.Text %>;
//do your other javascript stuff
</script>
If you needed to do more logic around this, you could also stick a literal control there and output to it in the code-behind:
<script type="text/javascript">
var s = <asp:literal id="litMyValue" runat="server">;
//do your other javascript stuff
</script>
Simple way u can pass/ instead you can use it in the function itself
like
<script>
function JscriptFunction()
{
var s = <%= LinkButton_ID.Text %>;
// do what you want here.
}
</script>
<asp:LinkButton ID="LinkButton_ID" runat="server" Text="LinkButton_Text" onclientclick="JscriptFunction();"></asp:LinkButton>
I'm writing a bit of aspx code to call a JavaScript function from a server control. Here's the JavaScript:
<script type="text/javascript">
function checkInfoPortalUnpin(portalareaid) {
if(portalareaid == 1) {
alert('a message');
}
}
</script>
And here's the calling aspx code:
<asp:ImageButton OtherFields="omitted..."
OnClientClick='checkInfoPortalUnpin(<%# Eval("PortalAreaID") %>);' />
When I view the source of that line in the browser (IE8) it is rendering as this:
... onclick="checkInfoPortalUnpin(<%# Eval("PortalAreaID") %>);"
and I get a syntax error when I click on the ImageButton. I know the OnClientClick does work because if I replace the <%# ... %> with a hard-coded '1' the function runs fine. Am I missing something?
Use this :
... OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" ...
Just use:
<asp:ImageButton runat="server" OnClientClick="checkInfoPortalUnpin('<%# Eval(\"PortalAreaID\") %>');" />
You forget to put single quote outside the server tag.