Muhamad Jafarnejad
Muhamad Jafarnejad

Reputation: 2571

call a method in code-behind from client side script

I'm using asp.net.

I want to update a part of my page (not whole page) without sending the page request to the server. I want to do this in the client side.

for this, I think I should use AJAX and javascript.

I've created a handler page: handler.ashx

I send a request and get response in plain text:

Javascript

  XMLHttpRequest.open("GET", url);
//ApplyUpdate is a function that get the response in client side.
  XMLHttpRequest.onreadystate = ApplyUpdate; 
  XMLHttpRequest.send(null);

Code on handler.ashx

response.write("plain text as response");

Now in ApplyUpdate function I can use the plain text and show it in client browser.

BUT the problem is this:

I have a placeholder1 control in asp.net:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

How can I assign the plain text (the response !) to a label control and add the label to placeholder1 control !? I mean how I can use these codes in client side:

Label lb1 = new Lable();
lb1.text = plaintext;
placeholder1.controls.add(lb1);

these codes are in c# (code-behind) but how I can use such these codes? I cannot do the same thing by javascript codes !

excuse my bad explanation.

I'll appreciate any help.

Upvotes: 0

Views: 515

Answers (1)

Yatrix
Yatrix

Reputation: 13775

Create the element in JavaScript using the response text and append it to the placeholder. If the placeholder is used explicitly for this label, you can remove it and just append it to its parent.

This is how you create the element: https://developer.mozilla.org/en-US/docs/DOM/document.createElement

Upvotes: 4

Related Questions