Reading value of hiddenfield after javascript on server side - c#

I have a problem with asp:hiddenfield, when I change its value in client side and wants to get it in server side , it gives me null...
here is client side code :
function pageLoad() {
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
}
and I want to get the value of hiddenfield in server side code :
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = HiddenField1.Value;
}
but the result for text of button is null... why??
thanks in advance:)

After this line:
var gV = $('#<%=HiddenField1.ClientID %>').val();
gV is a string, so gV.val("1") doesn't make sense.
Try this:
var gV = $('#<%=HiddenField1.ClientID %>');
gV.val("1");
Now, that shouldn't cause HiddenField1.Value to be null... did you mean empty?

Could you try with document ready?
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
});
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
Or - Use ScriptManager to call pageLoad
<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>
<asp:HiddenField runat="server" ID="HiddenField1" />
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function pageLoad() {
var gV = $('#<%= HiddenField1.ClientID %>');
gV.val("1");
}
</script>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />

Related

Changing Iframe's src with Jquery

I am trying to change iframe's src attribute with jQuery. But this code isnt working. Even the alert doesnt show up.
JS:
<script src="../Scripts/jquery-1.11.0.js" type="text/javascript"></script>
<script type="text/javascript">
function loadIframe(url) {
var $iframe = $('#' + <%=iPage.ClientID%>);//Also tried $('#<%=iPage.ClientID%>')
if ( $iframe.length ) {
$iframe.attr('src',url);
return false;
}
return true;
}
</script>
ASPX:
<li>
<asp:LinkButton id="link1" runat="server" OnClientClick="loadIframe(
'www.asd1234.com')" Text="Test"></asp:LinkButton>
</li>
<asp:updatepanel...>
//.....
<iframe id="iPage" runat="server"></iframe>
</asp:updatepanel>
As you are using
<iframe id="iPage" runat="server"></iframe>
You need to use Control.ClientID
<asp:LinkButton
id="link1"
runat="server"
OnClientClick="loadIframe('<%= iPage.ClientID %>', 'www.google.com')"
Text="Test"></asp:LinkButton>
Also move function out of the document ready handler.

not able to get value from aspx page to aspx.cs

This is my javascript function where value comes from other page and it is received perfectly, but How can i retrieve "divConversation" value in cs page .
This is my code
function myLoad() {
document.getElementById('divConversation').innerText = getParameterByName("id");
}
The Main Problem With Your Requirement is that the server side code is executed first, So what you need is to receive the passed value on page_load instead of receiving on aspx page.
And this can be done by
String passedValue=Request.QueryString["id"] as string;
This is a really simple demonstration:
aspx/markup; this will set the value of your hidden field as you type
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
<script>
document.getElementById('<%= TextBox1.UniqueID %>').onkeyup = function (evt) {
document.getElementById('<%= HiddenField1.UniqueID %>').value = document.getElementById('<%= TextBox1.UniqueID %>').value;
}
</script>
Code behind (.cs)
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(HiddenField1.Value);
}

Need to access variable from code behind. What i found shows how to go about it but i but the value keep displaying null

How to access and use javascript variables from code behind (C#). Upon research, i implemented the proccess in accessing the variables but i still do not get the values of the variables. Where am i going wrong?
//Javascript
<script type="text/javascript">
function OnSucceeded() {
var status = 65;
var state = "pass value"
document.getElementById("getValue").value = status;
document.getElementById("getvalues").value = state;
}
</script>
//
<asp:HiddenField ID="getValue" runat="server" Value="" />
<asp:HiddenField ID="getvalues" runat="server" Value="" />
<asp:Button ID="Button1" runat="server" OnClientClick="OnSucceeded()" Text="Button"
OnClick="Button1_Click" />
//C# code behind
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + getvalues.Value + "');", true);
Use ClientID to render correct IDs of server controls
<script type="text/javascript">
function OnSucceeded() {
var status = 65;
var state = "pass value"
document.getElementById("<%= getValue.ClientID %>").value = status;
document.getElementById("<%= getvalues.ClientID %>").value = state;
}
</script>

Jquery not called?

<div>
<asp:Button ID="btnCalculate" runat="server" Text="Calculate Claim" OnClientClick="cfrm();"/>
</div>
<div style="visibility: hidden;">
<asp:Button ID="btnYes" runat="server" OnClick="btnYes_Clicked" />
</div>
<script language="javascript" type="text/javascript">
function cfrm() {
var fee = $('[id$=lblTotalProcedureFee]').text();
if (fee > 500) {
if (confirm('Are you sure to do this operation?')) {
$('#<%= this.btnYes.ClientID %>').click();
}
}
}
</script>
I am trying to call "btnYes_Clicked" from the query. Refer to above code. It doesn't work.. then i edited the code just to test. First click, it doesn't work. 2nd click, it goes to the btnYes_Clicked event. I'm using master page which has update panels. Please help. Thanks..
<script language="javascript" type="text/javascript">
function cfrm() {
$('#<%= this.btnYes.ClientID %>').click();
}
</script>
Maybe you can try something like this, using return in OnClientClick and in cfrm to prevent form unwanted form submitting :
<div>
<asp:Button ID="btnCalculate" runat="server" Text="Calculate Claim" OnClientClick="return(cfrm());"/>
</div>
<script language="javascript" type="text/javascript">
function cfrm() {
var fee = $('[id$=lblTotalProcedureFee]').text();
if (fee > 500) {
if (confirm('Are you sure to do this operation?')) {
$('#<%= this.btnYes.ClientID %>').click();
}
}
return false;
}
</script>
Hope this will help

How to retrieve a value from jquery in C# user control?

I have a visitorID variable in ToDo function in external javascript.
I want to assign its' value in a user control. Front End Code:
<asp:HiddenField ID="hidVisitorID" runat="server" Value="-1"/>
<script type="text/javascript">
$j('#<%= hidVisitorID.ClientID %>').val(ToDo.visitorID);
</script>
In the back end it says, that hidVisitorID.Value is null (or -1 in this case). How do I assign value from jquery variable to hidVisitorID ?
Try this code:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="my_TO_DO.js"></script>
<script type="text/javascript">
$(document).ready(function () {
alert('my todo varname is: ' + ToDo.variableName);
$('#<%= hidVisitorID.ClientID %>').val("foobar");
});
</script>
<asp:HiddenField ID="hidVisitorID" runat="server" Value="-1"/>
<asp:Button Text="sub" runat="server" onclick="Click" />
When you click the button, it'll post back.
protected void Click(object sender, EventArgs e)
{
string valFromHidden = hidVisitorID.Value;
//valFromHidden is now foobar
}
Ensure your jQuery reference is ABOVE your other .js reference.

Categories