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>
Related
I have a abc.aspx file that is receiving some value using java-script.
i.e.
<asp:Content ID="Content1" ContentPlaceHolderID="ccont" Runat="Server">
<div id="ccont">
<script type="text/javascript">
function show(id) {
alert('id');
}
</script>
<div class="ccont">
</div>
As seen in the code above i can get certain int value say 1,2,3 and so on.But the value is inside the script tag.so my question is:
How do i get that value inside the body of the abc.aspx file say inside the div tag? ie anywhere outside the script.
How do i pass the value obtained in this abc.aspx file to its abc.axps.cs file
Try using ASP.Net's HiddenField control:
<asp:HiddenField ClientIDMode="static" ID="hiddenId" runat="server"/>
This will render an invisible input on your page will which hold your value.
Using jQuery, you can assign a value to it like this:
$(function(){
var value = $('.ccont').text(); //Get your value somehow
$('#hiddenId').val(value);
});
Once your page is submitted to the server, you will see that the hiddenId control will be populated in the page's OnLoad handler.
I'm trying to access a variable in Javascript from the server side (c#). I've declared this variable as public from the server side:
public string elementSliderAppend;
and accessing in javascript using:
<%=elementSliderAppend %>
So far so good, however the variable doesn't seem to be updated after the update panel refresh in my aspx page.
The variable changes all okay in the backend but the change is not happening in Javascript.
I have placed the accessing of the variable inside the update panel so it should refresh but it doesn't.
below is the code from the aspx page
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="10000">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:Asyncpostbacktrigger controlid="Timer1"> </asp:Asyncpostbacktrigger>
</Triggers>
<ContentTemplate>
<div id="flexViewer" class="flexslider">
</div>
<script type="text/javascript">
function getImgHtml() {
return <%=elementSliderAppend %>;
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
Calling the variable from the below code (also on the aspx page)
function pageLoad(sender, args) {
$("#flexViewer").html("<ul class=slides>" + getImgHtml() + "</ul>");
flexSliderRUN();
}
NEW CODE
I've now inserted the below code inside the updatepanel above
<asp:HiddenField ID="hiddimgHtml" runat="server" />
Im setting the hidden field value from code behind using
hiddimgHtml.value= elementSliderAppend;
using the below in my javascript file to access the value
var value = $("#hiddimgHtml").val();
alert(value);
$("#flexViewer").html("<ul class=slides>" + value + "</ul>");
However on the refresh im getting a very nasty looking error
POST http://:1562/FridayViewer.aspx 500 (Internal Server Error)
ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…NYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:6073
XHR finished loading: "http://:1562/FridayViewer.aspx". ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…NYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:6073
Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500 ScriptResource.axd?d=xz1-gVxgQeQi9AUxmqDjtx8455SyoL-b2LZdBEiJTo8-XZn2n4ZRBb…RNYDXi9xlT_-BAcGHV0ZqGBzbItBzEvsjInrEIWh3G93x0XMte0bSN00lh0&t=6119e399:237
<%= %> is an equivalent of Response.Write which is not intended to work with AJAX (which what UpdatePanel is). If you need to display something in an UpdatePanel - assign it to a property of a control (it could be some standard ASP.NET Control like Label and its Text property or even a SPAN with runat="server" and its innerHTML property)
you can also use hidden field.
Steps
1) set value of hidden field
2) get the value of hidden field using java script or jQuery
Thanks for everyones answers they all help resolve the issue
Im using the hidden control and setting its value from code behind
The reason for the error on postback seemed to be the length of string that was assigned to the hidden field which caused the issues. To resolve I blanked the value in javascript before postback
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 ;-)
I'm trying to call a jquery function from a asp link button. Here is my link button html:
<div style="padding-left:75px">
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
</div>
Here is my jquery function:
function ShowCCControls() {
$('#lblCC').show();
$('#txtCC').show();
} //end ShowCCControls()
When I try to build, I get the error:
ASP.internal_email_aspx does not contain a definition for 'ShowCCControls' and no extension method 'ShowCCControls' accepting a first argument of type 'ASP.internal_email_aspx' could be found (are you missing a using directive or an assembly reference?)
I have this working on another page using a check box:
<asp:CheckBox ID="chkNew" TabIndex="8" runat="server" Text="New Tank" OnClick="SetNewTankControls()"
ClientIDMode="Static" />
Anybody see an issue? Thanks
Here is all of the javascript:
<script type="text/javascript" language="javascript">
//document.ready is used for jquery, waits until the doc is ready to be manipulated
$(document).ready(function () {
HideControls();
}); //end document.ready
function HideControls() {
$('#lblCC').hide();
$('#txtCC').hide();
$('#lblBCC').hide();
$('#txtBCC').hide();
} //end HideControls()
function ShowBCCControls() {
$('#lblBCC').show();
$('#txtBCC').show();
} //end ShowBCCControls
function ShowCCControls() {
$('#lblCC').show();
$('#txtCC').show();
} //end ShowCCControls()
OnClick is for specifying handlers in code-behind. If you want to specify a javascript function, you should use OnClientClick attribute.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclientclick(v=vs.80).aspx
<div style="padding-left:75px">
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" OnClientClick="ShowCCControls()" Text="Add CC"></asp:LinkButton>
</div>
You could just set the handler in your client script like this:
$('#lbAddCC').click(function() {
$('#lblCC').show();
$('#txtCC').show();
});
Since you're not intending to perform server side behaviours with this click event, there isn't any need to define a handler on the server control and then have it render out a call to the client side function when you could just call it directly.
EDIT:
You will of course need to couple the client side script with the removal of the erroneous OnClick handler on the server control as below:
<asp:LinkButton ID="lbAddCC" runat="server" ClientIDMode="Static" Text="Add CC"></asp:LinkButton>
it could be that the page load the function call before loading jquery code, make sure you put the jquery before the link button and reference jquery liabrary
I've got a DIV which contains some text in it.
Ive also got an asp:Button, which when pressed I would like to retrieve this text. However, it appears that as soon as I press the button, the DIV's contents is reset - most likely due to postback.
The DIV has got runat=server". Does anyone have any idea as to what may be done to make this DIV retain its contents on pressing the button? The data is manipulated countless times before the button is pressed, so I would like to try avoiding updating a Session every time.
Try something like this:
getText = function(){
alert($("#<%=Panel1.ClientID%>").text());
return false;
}
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:Button ID="Button1" runat="server" OnClientClick="return getText();" ... />
You can also try this:
$("#<%=Button1.ClientID%>").click(function(){
alert($("#div1").text());
return false;
});
One way would be grabbing button click event in your makrkup, store your DIV content in a hidden field, then fire postback manually in your button click.
Try using an asp:Panel. It renders as a DIV and you have access to all of its properties at all points in the page life cycle.
another way is to use Ajax. specifically - updatepanel control
You can store you DIV content in a hidden field by calling a JavaScript function in onClientClick event of the button - it will execute just before OnClick event, before postback. This way, there's no need to fire postback manually.
Let's say you have one div, one hidden field and button with onClientClick and OnClick events. You use simple JavaScript function for the job:
<div id="myDiv" runat="server" ></div>
<asp:Button ID="btnGetDiv" runat="server" Text="Get div value" OnClick="getValueOnServer" OnClientClick="setHiddenFieldF()"/>
<asp:HiddenField ID="hf" runat="server" />
<script type="text/javascript">
function setHiddenFieldF() {
var nevValue = document.getElementById('<%= myDiv.ClientID %>').innerHTML;
document.getElementById('<%= hf.ClientID %>').value = nevValue;
}
</script>
Then, on the server, just use the value of the hidden field.
You can't read your DIV's content because it is not send to the server in the postback. Only values of form elements (<input>, <textarea>, <select>, etc.) are send.
If you really want to read DIV's content, you can copy it to the hidden input (as wrote Kavousi).
Just add hidden field to your page:
<asp:HiddenField ID="hidden" runat="server" />
And then in code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Page.Form.Attributes["onsubmit"] = string.Format(#"$(""#{0}"").val($(""#{1}"").html());", hidden.ClientID, div.ClientID);
}
}
(div is your div's name).
You can then read div's content by reading hidden.Value.