My asp.net webform includes a div element called 'contents' with some HTML content loaded into it from the codebehind file. I am trying to mark up certain text within this HTML so that when clicked by the user, it sends a message back to the server and populates a textbox on the form with different data.
The challenge I'm facing is figuring out how to communicate back to the server. Here are the specific questions:
- How can I use AJAX to send a parameter 'hello' back to the server upon clicking a marked-up text?
- Which method on the server should be invoked? Would it be another Page_Load or something else?
- After sending the request to the server, how do I update the textbox without reloading the entire page, still using AJAX? Assume receiving 'hello' and sending back 'goodbye' to the textbox
In my aspx file:
And in the codebehind file within form_load:
System.IO.StreamReader rdr = System.IO.File.OpenText("c:\\myHtmlFile.html");
string s = rdr.ReadToEnd();
this.contents.InnerHtml = s;
The HTML content loaded into the div has a JavaScript function and an onclick event:
<script type="text/javascript">
function myfunction(x)
{
alert("You clicked " + x);
}
</script>
<p onclick="myfunction('hello')">hello</p>
When 'hello' is clicked, I want TextBox1 to display 'goodbye'. The server needs to handle this interaction without refreshing the webpage.
If you have any suggestions or solutions, please keep them simple. Thank you in advance!