Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
Hello every one,
I am doing a e-commerce project.In that i am using java script for getting users country and system's IP address.I have taken those values in labels.using that value from the label i have to update currency using asp controls.But when i run the code it is saying there is no value in innerHTML.I have to get label value from java script and have use it in code behind.How can i over come this.Please help me.My sample code is below.
XML
<script src="http://code.jquery.com/jquery-1.8.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        var strip, strcountry, strcity, strregion, strlatitude, strlongitude, strtimezone
        function GetUserInfo(data) {
            strip = data.host; strcountry = data.countryName; strcity = data.city;
            strregion = data.region; strlatitude = data.latitude; strlongitude = data.longitude;
            strtimezone = data.timezone;
        }
        $(function () {
            BindUserInfo();
        })
        function BindUserInfo() {
            document.getElementById('lblIP').innerHTML = strip;
            document.getElementById('lblCountry').innerHTML = strcountry;
</script>

    <script type="text/javascript" src="http://smart-ip.net/geoip-json?callback=GetUserInfo"></script>
</head>

<body>
    <div>
        <table id="tbDetails" cellpadding="2" cellspacing="2" style="border: 1px solid #000;<br mode=" hold=" />            font-family: Verdana;">
            <tr style="background-color: #DC5807; color: White; font-weight: bold">
                <td colspan="2" align="center">
                    User Information
                </td>
            </tr>
            <tr style="border: solid 1px #000000">
                <td align="right">
                    IP:
                </td>
                <td>
                    <label id="lblIP" />
                </td>
            </tr></table></div>
Posted
Updated 5-Jan-14 19:26pm
v4
Comments
Nandakishore G N 6-Jan-14 0:56am    
is it server side control or html tag? If it is server side add this and try--
$("#<%=lblIP.ClientID%>").text() // using jquery
document.getElementById("<%=lblIP.ClientID %>").innerHTML//using javascript

and paste the design side of the html page.
BK 4 code 6-Jan-14 1:09am    
Hi did you have place country label in your webpage?

HI,


use innerText to assign values to the label control in javascript
JavaScript
document.getElementById('lblIP').innerText = "some";


in asp.net you cannot get the values of label control in code behind which is updated in the client side ( ie javascript )

simple way is you can use hidden field to store the values in the javascript and you can read the same in the code behind.


example:

whenever u r assigning a value to the label, update the corresponding hidden field as well
JavaScript
document.getElementById('lblIP').innerText = "some";
            document.getElementById('hdn_lblIP').value = "some";


create a hidden control in your page..
ASP.NET
<asp:HiddenField ID="hdn_lblIP" runat="server" />


in the server ( code behind ) you can read like this

C#
string ipvalue =  hdn_lblIP.Value;


hope you understood.
 
Share this answer
 
Comments
Member 10194425 6-Jan-14 2:25am    
i tried this but i am not getting any value in that string.It is showing "" when debugged.
<script type="text/javascript">
var strcountry;
function GetUserInfo(data) {
strcountry = data.countryName;
}
$(function () {
BindUserInfo();
})
function BindUserInfo() {

document.getElementById('hdfld').value = strcountry;

}
</script>
code behind-
string cntry = hdfld.Value;
TextBox1.Text = hdfld.Value;
Karthik_Mahalingam 6-Jan-14 2:53am    
try with this
document.getElementById('<%= hdfld.getClientID %>').value = 'somevalue';

if it is resolved, pls close this post.
Member 10194425 6-Jan-14 4:47am    
It is showing error saying " system.UI.Webcontrols.hiddenfield does not contain a definition for getClientID "
Karthik_Mahalingam 6-Jan-14 4:50am    
oops forgot add ().

try hdfld.getClientID () or hdfld.ClientID

Chris_Fowler_ 30-Jan-18 16:08pm    
Use a static ID mode.
Then you can set the value with that id.

<asp:HiddenField ID="hdn_lblIP" ClientIDMode="Static" runat="server" />
First of all you need to make sure of the data which you are assigning to innerHTML.

Put a debugger inside BindUserInfo and GetUserInfo function.
Then check if it is hitting the debugger in GetUserInfo first or not.

If it is hitting, then check what are the values of strip and strcountry. If the values are correct, then see if it is hitting the other debugger inside functon BindUserInfo .

If it hits that function, then check what are the values of strip and strcountry.

Last, but not the least check if the Label IDs are the same in the rendered HTML in browser.
Just view source and try to locate those Labels and see if the IDs are same or not.
 
Share this answer
 
If your working platform is ASP.NET then you can use Hidden value field to access the javascript values (front end values).
check this link
 
Share this answer
 
Hi,

Assign value to hidden field through your javascript function. Its easy to access hidden field value in code behind file.

Thank you
Nagaraj.N.M.
 
Share this answer
 
Comments
Member 10194425 6-Jan-14 2:36am    
Can you help me with sample code.Please.
Hi,

.aspx page:
C#
<form id="ecnTest" method="post" runat="server" onSubmit="return validateForm()">
  //your code goes here
<asp:hiddenfield id="hdfTestValue" runat="server" />
</form>

<script language="Javascript" type="text/javascript">
     function fnTest()
     {
       ecnTest.hdfTestValue.value = "value";
     }        
</script>


.cs page:
any where in .cs page event / function access easily hidden field value.

Thank you & best regards
Nagaraj.N.M.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900