javascript - display results of jquery in a label in asp.net - Stack Overflow

I want to display result of this javascript in a label control on my asp page, instead of alert. how ca

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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745568033a4633513.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信