1 Replies - 7029 Views - Last Post: 18 June 2012 - 04:51 AM Rate Topic: -----

#1 michc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-June 12

Updating a control from asp.net using ajax

Posted 11 June 2012 - 07:56 AM

I have an asp.net webform which has a div element and a textbox on it. The div is loaded with some html, which I've retrieved into a string in the codebehind file. Inside this html, I need to be able to mark up various bits of text so that when a user clicks them, it sends a message back to the server, which then populates the text box (which is on the same form) with some other data.

The problem I'm having is how best to communicat back to the server- so in the code below,
1) What should the line alert("You clicked " + x); be, so that it uses ajax to call back to the server, sending the parameter 'hello'
2) What method would be invoked on the server? Would it be Page_Load again?
3) After having called the server, what code would I need to popluate the text box without sending back the whole page again (i.e. still using ajax)? The problem is not retrieving the text on the server, let's just say I receive 'hello' as a parameter on the server, and want to send back 'goodbye' to the text box, again using ajax

So my source aspx file has this in it...
<div id="contents" runat="server"></div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

and in my codebehind file in form_load, I have..

System.IO.StreamReader rdr = System.IO.File.OpenText("c:\\myHtmlFile.html");
string s = rdr.ReadToEnd();
this.contents.InnerHtml = s;

the HTML I'm loading into the div element is this

<script type="text/javascript">
    	    function myfunction(x)
		alert("You clicked " + x);

	<p  onclick="myfunction('hello')">hello</p>

When the user clicks 'hello', I want TextBox1 to say 'goodbye'. Again, it's the server that has to handle things here, without reposting the webpage though..

Anyone have any ideas? The simpler the better please! thanks in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: Updating a control from asp.net using ajax

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Updating a control from asp.net using ajax

Posted 18 June 2012 - 04:51 AM

To use AJAX in ASP.Net, you need to use an UpdatePanel. With the UpdatePanel, you put any controls that will be making the calls to the server AND controls that will be updated from the server into it. So in your example, you would put the textbox and the paragraph([/il]<p>[/il]) into the UpdatePanel.

However, this will still postback to the server, but it will not give the "page flicker". The Page_Load event will execute, as it does with all postbacks, so you will need to handle that using the Page.IsPostBack.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1