从客户端脚本调用代码隐藏中的方法 [英] call a method in code-behind from client side script
问题描述
我正在使用asp.net。
我想更新页面的一部分(不是整页)而不发送页面请求服务器。我想在客户端这样做。
为此,我想我应该使用AJAX和javascript。
我已经创建了一个处理程序页面:handler.ashx
我发送请求并以纯文本形式获得响应:
(在javascript中)
...
XMLHttpRequest.open(GET,url);
XMLHttpRequest.onreadystate = ApplyUpdate; // ApplyUpdate是一个在客户端获取响应的函数(javascript)。
XMLHttpRequest.send(null);
...
(在handler.ashx中)
...
response.write(纯文本作为回应);
...
现在在ApplyUpdate函数中我可以使用纯文本并在客户端浏览器中显示它。
但是
问题是:
我在asp.net中有一个placeholder1控件:< asp:PlaceHolder ID =PlaceHolder1runat = server>
如何将纯文本(响应!)分配给标签控件并将标签添加到placeholder1控件中??
我的意思是我如何在客户端使用这些代码:
Label lb1 = new Lable();
lb1.text = plaintext;
placeholder1.controls.add(lb1);
这些代码在c#中(代码隐藏)但我如何使用这些代码?
我不能通过javascript做同样的事情代码!
原谅我的错误解释。
我会感激任何帮助。
Hi,
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:
(in javascript)
...
XMLHttpRequest.open("GET", url);
XMLHttpRequest.onreadystate = ApplyUpdate; // ApplyUpdate is a function that get the response in client side(javascript).
XMLHttpRequest.send(null);
...
(in 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">
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.
推荐答案
你正朝着正确的方向前进。但你不必重新发明轮子。
一方面,您可以获得任何一般情况的webmethod场景。请参阅: http:/ /www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/ [ ^ ],另一方面,你有 UpdatePanel [< a href =http://msdn.microsoft.com/en-us/library/bb399001(v=vs.100).aspx\"target =_ blanktitle =New Window> ^ ]和工具包中的许多其他Ajax控件[ ^ ]。
You are facing in the right direction. But you don''t have to reinvent the wheel.
On one hand you have the webmethod scenario for any general case. See: http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/[^], on the other hand you have the UpdatePanel[^] and many other Ajax controls in the toolkit[^].
这篇关于从客户端脚本调用代码隐藏中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!