Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I write a C# program: LoadUserControl to read the TextBox1 from the json form:
C#
[WebMethod]
        public static string LoadUserControl(string message)
        {
           //Help with code to read json of the TextBox1????
        }

JavaScript
<script type = "text/javascript">
    $(document).ready(function () {
        $("#<%= Button1.ClientID %>").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/LoadUserControl",
                data: "{message: '" + $("#<%= TextBox1.ClientID %>").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    $("#Content").append(r.d);
                },
                error: function(req, status, error){
                    alert("R: " + req + "S: " + status + " E: " + error);
                }
            });
        });
    });
</script>
Posted
Comments
[no name] 13-Oct-14 7:28am    
explain your problem in detail
Teledextri 13-Oct-14 7:39am    
I want to take the data from Textbox1 and put it in a JSON and then read the the data in a [Web Method] of a c# program. I am just using this as a learning example for later projects.

Please see my recent answer: How To Convert object type to C# class object type[^].

And also this one, based on the above: how to conver multi level json data to C# Object?[^].

—SA
 
Share this answer
 
I would like to advise you to use Newtonsoft.JSON library to work with JSON data. I like it myself, example code

C#
string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<movie>(json);

string name = m.Name;
// Bad Boys
</movie>


You can get more information and resources from the website, or you can always ask questions if you get into trouble.

http://james.newtonking.com/json[^]
 
Share this answer
 
JavaScript
$("#<%= Button1.ClientID %>").click(function () {
  var url = '@Url.Content("Default.aspx/LoadUserControl")';
            $.getJSON(url, { passedtext:$("#<%= TextBox1.ClientID %>").val() }, function (data) {
})
})

now add this
C#
public static string LoadUserControl(string passedtext)
        {
         //in the passedtext there will be the value of text box and what ever you want to do with this you can do
  
        }
 
Share this answer
 

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