Customers.aspx
=AJAX, 向web service請求資料。 [script]
function displayCustomers(result) {
$get("customers").innerHTML = result;
}
[script]
[body]
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/CustomerService.asmx" />
</Services>
</asp:ScriptManager>
Enter Country: <input id="Country" type="text" />
<a href="javascript:CustomerService.GetCustomersByCountry($get('Country').value, displayCustomers)">Show Customers</a>
<div id="customers"></div>
[/body]
CustomerService.cs
=這個AJAX用的web service是宣告 System.Web.Script.Services.ScriptService using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class CustomerService : System.Web.Services.WebService {
[WebMethod]
public string GetCustomersByCountry(string country)
{
NorthwindTableAdapters.CustomersTableAdapter customersAdapter = new NorthwindTableAdapters.CustomersTableAdapter();
Northwind.CustomersDataTable customers = customersAdapter.GetCustomersByCountry(country);
if (customers.Rows.Count > 0)
return ViewManager.RenderView("~/App_Views/Customers.ascx", customers);
else
return ViewManager.RenderView("~/App_Views/NoCustomers.ascx", null);
}
}
Customers.ascx.cs
=View using System;
public partial class App_Views_Customers : System.Web.UI.UserControl
{
public object Data;
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = Data;
Repeater1.DataBind();
}
}
ViewManager.cs
=Controller using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Reflection;
public class ViewManager
{
public static string RenderView(string path)
{
return RenderView(path, null);
}
public static string RenderView(string path, object data)
{
Page pageHolder = new Page();
UserControl viewControl = (UserControl) pageHolder.LoadControl(path);
if (data != null)
{
Type viewControlType = viewControl.GetType();
FieldInfo field = viewControlType.GetField("Data");
if (field != null)
{
field.SetValue(viewControl, data);
}
else
{
throw new Exception("View file: " + path + " does not have a public Data property");
}
}
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
}
}
ASP.NET 1.1 只能用在.aspx上 StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute("~/ajax.aspx", output);
return output.ToString();
參考資料:
- 技巧和诀窍:在不用UpdatePanel的情形下可与ASP.NET AJAX 使用的酷UI模板技术
- HttpServerUtility.Execute 方法 (String, TextWriter)
- FieldInfo 成員
- 在CodeFile中取得控制項將輸出Clinet端的HTML內容 - topcat姍舞之間的極度凝聚
WebControl, HtmlControl - 解決GridView使用RenderControl取得HTML出現的問題 - topcat姍舞之間的極度凝聚
16 Responses to [筆記]動態載入ASCX指定資料並取得其輸出的HTML內容
Howdy! I simply wish to give an enormous thumbs up for the good data you could have here on this post.
I might be coming back to your blog for extra soon.
Also visit my homepage - SEM
Hi there! I just would like to give a huge thumbs up
for the good data you have here on this
post. I will be coming back to your blog for extra soon.
Here is my web-site - seoprofiler
Hiya! I just wish to give a huge thumbs up for the
good data you’ve gotten here on this post. I might be coming again to your weblog for more soon.
My blog :: seoul hostels near airport
Good day! I just wish to give a huge thumbs up for the great info you might have here on this post.
I will be coming back to your weblog for more soon.
My website: search engine optimization services agreement
Hey! I just wish to give a huge thumbs up for the good information you may have right here on this post.
I will be coming again to your blog for extra soon.
my blog - semi truck auctions in florida
Great blog! Do you have any recommendations for aspiring writers?
I'm hoping to start my own website soon but I'm
a little lost on everything. Would you recommend starting with a free
platform like Wordpress or go for a paid option? There are so many choices out there that I'm completely confused ..
Any ideas? Thanks a lot!
Also visit my webpage gold ira rollover
An intеresting discussion is definitely woeth commеnt.
I believе that you neеd to publish morе ahout tɦіs topic, it mіght not Ьe a taboo subject buut typically folks don't
talk аbout ѕuch topics. To the next! Kindd гegards!!
my site: buy Proactol - http://proactolplusreview.net/ -
It's in reality a great and helpful piedce of information. I am glad that you simply shared
this useful information with us. Please keeep
us informed like this. Thank you for sharing.
Also visit my web site; temple run 2 glitch
No mɑtter what you do in tҺe gym if your diet is not corrеct it
is all a աaѕte of time. I'll first touch a bit on nutrition and dieting
since I feel it's the most important aspect to fat loss.
Excellent solutions like making a cɦߋice on low
calorie nutrition and planning thіs meals well remain more realistic.
Ϝeel free to suгf to my web-site Turbulence Training Pdf
In this disease actually, fat content of the food is not absorbed
thus causing malabsorption of fat soluble vitamins also.
Power outlets make sure that you are getting problem free experience even with your old laptops because they may turn dark without power outlets.
Find more helpful informative articles at our website.
Here is my web page :: gluten free restaurants coeliac society
I read this post completely concerning the difference of newwest and preceding technologies,
it's remarkable article.
my blog post valuable equipment - -
Οr the meal prepare would most lіkely include tɦings like
hurling ɑ nuimber of varіous meats, salt аnd greens гight intօ a slow-pot οr even slow
cooker. І гeally сould realistically enjjoy foor
ɑ long time οn еnd on а single game and
stіll nevеr have a game concluded effectively.
*Historic Aian mеn and women produced playing cards
thrоughout ninth hundred years.
my homepage; download spider solitaire
It is employed only in wrinkle creams and cosmetics, and is fairly unheard of to see in "drugstore" makes.
Wrinkle creams get reinvented with Juara Sweet Black Tea and Rice Moisturizer.
An anti-aging serum is definitely your best bet to fight
wrinkles and look younger.
my web page wrinkle cream
Thank you for the auspicious writeup. It in fact was
a amusement account it. Look advanced to more added agreeable from you!
However, how could we communicate?
Feel free to visit my webpage ... black mold testing michigan
Wow, amzzing blog structure! How lengthy have you ever been running a
blog for? you maade blogging glance easy.
The total look of your website is magnificent, let alone thee content material!
Here is my site ... Toshiba Computersapplecomputers
converse shoes
converse shoes
off white shoes
louboutin shoes
louboutin
mbt shoes
balenciaga
balenciaga shoes
moncler jackets
russell westbrook shoes
Something to say?