admin管理员组

文章数量:1022210

I want to display result of this javascript in a label control on my asp page, instead of alert. how can i do so?

<script type="text/javascript" language="Javascript" src=".7.1/jquery.min.js"></script> <!-- Require EasyJQuery After JQuery --><script type="text/javascript" language="Javascript" src=".js"></script> <script type="text/javascript" language="Javascript">
    // 1. Your Data Here
    function my_callback(json) {
        alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
    }

    function my_callback2(json) {

       alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
    }

    // 2. Setup Callback Function
   // EasyjQuery_Get_IP("my_callback"); // fastest version
    EasyjQuery_Get_IP("my_callback2","full"); // full version
</script> 

I want to display result of this javascript in a label control on my asp page, instead of alert. how can i do so?

<script type="text/javascript" language="Javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script> <!-- Require EasyJQuery After JQuery --><script type="text/javascript" language="Javascript" src="http://api.easyjquery./easyjquery.js"></script> <script type="text/javascript" language="Javascript">
    // 1. Your Data Here
    function my_callback(json) {
        alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
    }

    function my_callback2(json) {

       alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
    }

    // 2. Setup Callback Function
   // EasyjQuery_Get_IP("my_callback"); // fastest version
    EasyjQuery_Get_IP("my_callback2","full"); // full version
</script> 
Share Improve this question edited Jun 9, 2012 at 9:33 Aristos 66.6k16 gold badges116 silver badges153 bronze badges asked Jun 9, 2012 at 9:09 ZoyaZoya 4053 gold badges10 silver badges22 bronze badges 2
  • What is the id of your label. – Priyank Patel Commented Jun 9, 2012 at 9:16
  • Do you want to call the javascript function from code behind – Priyank Patel Commented Jun 9, 2012 at 9:20
Add a ment  | 

4 Answers 4

Reset to default 4

Using Dynamic ID

The label control of asp is rendered by default as span So you can set the text inside by find this control by ID, and type inside using the text as:

  $('#<%=txtLabel.ClientID%>').text("message");

Using Static ID

alternative (ver 4+) you can set ClientIDMode="Static" on your control so the id not change

<asp:Label runat="server" ID="txtName" ClientIDMode="Static">Test</asp:Label>

render as <span id="txtName">Test</span> and write inside him as

$('#txtName').text("message");

Your function will then be

function my_callback(json) {
    $('#txtName').text("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
}

function my_callback2(json) {
   $('#txtName').text("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
}

When you use server side controls their Client ID changes.As Aristos correctly pointed out ,if you are using (v4+) you can set the ClientIDMode property to static so that its ID remains the same. If the ID of your label is myLabel and you set ClientIDMode=static ,then the ID of the label will be myLabel itself. Otherwise the ID is rendered something like this #ctl00_ContentPlaceHolder1_myLabel. This should do the trick for you.

$('#<%= myLabel.ClientID %>').text("set whatever you need");

Hope this helps you.

head runat="server">
    <script language="Javascript" type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>





<script language="Javascript" type="text/javascript" src="http://api.easyjquery./easyjquery.js"></script>

<script language=Javascript>
    function setup_ip(json) {
        var htmlx = " Your IP Address: <b>" + json.IP;
        htmlx += "</b> | Country: " + json.countryName;
        if (json.cityName != "Unknown" || json.regionName != "Unknown") {
            htmlx += " | City: " + json.cityName + " / " + json.regionName;
        } else {
            htmlx += " | Your Time: " + json.localTimeZone;
        }

        $("#myipx").html(htmlx);
    }

    $(document).ready(function() {


        EasyjQuery_Get_IP("setup_ip", "full");

    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="inner">
<p class="wele-message">
<a href="http://www.easyjquery./detect-get-clients-ip-address-country-using-javascript-php/"id="topid" title="Javascript, PHP jQuery API Detect Clients IP Address and Country - Geo Location"   ><asp:Label ID="myipx" runat="server"  ClientIDMode="Static"></asp:Label>Detecting Clients IP Address - Country - City</span></a>
<br />
<asp:Label ID="lbl1" runat="server"  ClientIDMode="Static"></asp:Label>
                </p>



            <!-- END #footer-texture -->
            </div>

    </div>
    </form>
</body>

Find the ID or name of your label and set its text like this:

$('label#myLabel').text('foo');

I want to display result of this javascript in a label control on my asp page, instead of alert. how can i do so?

<script type="text/javascript" language="Javascript" src=".7.1/jquery.min.js"></script> <!-- Require EasyJQuery After JQuery --><script type="text/javascript" language="Javascript" src=".js"></script> <script type="text/javascript" language="Javascript">
    // 1. Your Data Here
    function my_callback(json) {
        alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
    }

    function my_callback2(json) {

       alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
    }

    // 2. Setup Callback Function
   // EasyjQuery_Get_IP("my_callback"); // fastest version
    EasyjQuery_Get_IP("my_callback2","full"); // full version
</script> 

I want to display result of this javascript in a label control on my asp page, instead of alert. how can i do so?

<script type="text/javascript" language="Javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script> <!-- Require EasyJQuery After JQuery --><script type="text/javascript" language="Javascript" src="http://api.easyjquery./easyjquery.js"></script> <script type="text/javascript" language="Javascript">
    // 1. Your Data Here
    function my_callback(json) {
        alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
    }

    function my_callback2(json) {

       alert("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
    }

    // 2. Setup Callback Function
   // EasyjQuery_Get_IP("my_callback"); // fastest version
    EasyjQuery_Get_IP("my_callback2","full"); // full version
</script> 
Share Improve this question edited Jun 9, 2012 at 9:33 Aristos 66.6k16 gold badges116 silver badges153 bronze badges asked Jun 9, 2012 at 9:09 ZoyaZoya 4053 gold badges10 silver badges22 bronze badges 2
  • What is the id of your label. – Priyank Patel Commented Jun 9, 2012 at 9:16
  • Do you want to call the javascript function from code behind – Priyank Patel Commented Jun 9, 2012 at 9:20
Add a ment  | 

4 Answers 4

Reset to default 4

Using Dynamic ID

The label control of asp is rendered by default as span So you can set the text inside by find this control by ID, and type inside using the text as:

  $('#<%=txtLabel.ClientID%>').text("message");

Using Static ID

alternative (ver 4+) you can set ClientIDMode="Static" on your control so the id not change

<asp:Label runat="server" ID="txtName" ClientIDMode="Static">Test</asp:Label>

render as <span id="txtName">Test</span> and write inside him as

$('#txtName').text("message");

Your function will then be

function my_callback(json) {
    $('#txtName').text("IP :" + json.IP + " COUNTRY: " + json.COUNTRY);
}

function my_callback2(json) {
   $('#txtName').text("IP :" + json.IP + " COUNTRY: " + json.COUNTRY + " City: " + json.cityName + " Region Name: " + json.regionName);
}

When you use server side controls their Client ID changes.As Aristos correctly pointed out ,if you are using (v4+) you can set the ClientIDMode property to static so that its ID remains the same. If the ID of your label is myLabel and you set ClientIDMode=static ,then the ID of the label will be myLabel itself. Otherwise the ID is rendered something like this #ctl00_ContentPlaceHolder1_myLabel. This should do the trick for you.

$('#<%= myLabel.ClientID %>').text("set whatever you need");

Hope this helps you.

head runat="server">
    <script language="Javascript" type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>





<script language="Javascript" type="text/javascript" src="http://api.easyjquery./easyjquery.js"></script>

<script language=Javascript>
    function setup_ip(json) {
        var htmlx = " Your IP Address: <b>" + json.IP;
        htmlx += "</b> | Country: " + json.countryName;
        if (json.cityName != "Unknown" || json.regionName != "Unknown") {
            htmlx += " | City: " + json.cityName + " / " + json.regionName;
        } else {
            htmlx += " | Your Time: " + json.localTimeZone;
        }

        $("#myipx").html(htmlx);
    }

    $(document).ready(function() {


        EasyjQuery_Get_IP("setup_ip", "full");

    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="inner">
<p class="wele-message">
<a href="http://www.easyjquery./detect-get-clients-ip-address-country-using-javascript-php/"id="topid" title="Javascript, PHP jQuery API Detect Clients IP Address and Country - Geo Location"   ><asp:Label ID="myipx" runat="server"  ClientIDMode="Static"></asp:Label>Detecting Clients IP Address - Country - City</span></a>
<br />
<asp:Label ID="lbl1" runat="server"  ClientIDMode="Static"></asp:Label>
                </p>



            <!-- END #footer-texture -->
            </div>

    </div>
    </form>
</body>

Find the ID or name of your label and set its text like this:

$('label#myLabel').text('foo');

本文标签: javascriptdisplay results of jquery in a label in aspnetStack Overflow