Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The TextBox1 is not registering a number or string when typed in before the Button1 is pressed executing GetDateTime():

JavaScript
var jsonid = $("#<%= TextBox1.ClientID %>").text();
var json1 = "{'id': '" + jsonid + "'}";

There is no error code there is just nothing printed out after "ID:".
Default.aspx
JavaScript
<script type="text/javascript" src="../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    function GetDateTime() {
        var jsonid = $("#<%= TextBox1.ClientID %>").text();
        var json1 = "{'id': '" + jsonid + "'}";
        $.ajax
        ({
            type: "POST",
            url: "Default.aspx/GetServerDateTime",
            data: json1,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                alert(result.d);
            },
            error: function (status, ex) {
                alert("Error Code: Status: " + status + " Ex: " + ex);
            }
        });
    }
</script>

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="AJAX" OnClick="Button1_Click" />

Default.aspx.cs
C#
protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "javascript", "Javascript:GetDateTime();", true);
}
[System.Web.Services.WebMethod]
public static string GetServerDateTime(string id)
{
    string datetimeid1 = "ID: " + id + " Date&Time: " + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
    return datetimeid1;
}
Posted
Updated 7-Nov-14 9:33am
v3

Assuming you want the value of the textbox, use the .val() method[^].

You'll also need to properly encode the text before inserting it into a JSON string. You can use the JSON.stringify(...) function[^] to convert a javascript object to JSON. It's supported in all modern browsers[^], including IE8+. If you need support for earlier browsers, Douglas Crockford provides a polyfill[^] on GitHub.

JavaScript
var jsonid = $("#<%= TextBox1.ClientID %>").val();
var json1 = JSON.stringify({id: jsonid});
 
Share this answer
 
That is because you call and get the value of the input fields not their text. Text method is to get the visual representation of the HTML markup. Inputs don't provide anything like that, they just provide you with a value provided by the user.

This would work for you,

JavaScript
// get the val()
var jsonid = $("#<%= TextBox1.ClientID %>").val();


But since you JSON seems to be asking for an id, why not simply put the value of that variable there instead of getting text or value?

http://api.jquery.com/val/[^]
 
Share this answer
 
v2
Comments
Teledextri 7-Nov-14 16:25pm    
The textbox can be any alphanumeric combo but needs to be is the form "{'id':'alphanumeric'}" to work in AJAX. var json1 = JSON.stringify({id: jsonid}); does not put it into that form.

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