DynamicPopulate:"穷人的"UpdatePanel

    技术2022-05-11  18

    DynamicPopulate扩展器控件可以让页面在客户端运行时查询某个Web Service或页面中的某个Page Method,并将其返回的HTML字符串插入到页面的某个控件中。 示例运行效果: 图(1) 图(2) 图(3) NewsEntry.cs代码示例: using  System; using  System.Web; using  System.Web.Services; using  System.Web.Services.Protocols; public   class  NewsEntryLight {    private int m_id;    public int Id     {        get         {            return m_id;        }        set        {            m_id = value;        }    }    private string m_title;    public string Title    {        get        {            return m_title;        }        set        {            m_title = value;        }    }    public NewsEntryLight()    {    }    public NewsEntryLight(int id, string title)    {        m_id = id;        m_title = title;    }} public   class  NewsEntry : NewsEntryLight {    private string m_content;    public string Content    {        get        {            return m_content;        }        set        {            m_content = value;        }    }    private string m_author;    public string Author    {        get        {            return m_author;        }        set        {            m_author = value;        }    }    private DateTime m_postDate;    public DateTime PostDate    {        get        {            return m_postDate;        }        set        {            m_postDate = value;        }    }    private int m_views;    public int Views    {        get        {            return m_views;        }        set        {            m_views = value;        }    }    public NewsEntry()    {     }    public NewsEntry(int id, string title, string content, string author, DateTime postDate, int views)        : base(id, title)    {        m_content = content;        m_author = author;        m_postDate = postDate;        m_views = views;    }} NewsData.cs代码示例: using  System; using  System.Data; using  System.Configuration; using  System.Web; using  System.Web.Security; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Web.UI.WebControls.WebParts; using  System.Web.UI.HtmlControls; using  System.Collections.Generic; /// <summary>/// NewsData 的摘要说明/// </summary> public   class  NewsData {    public static List<NewsEntryLight> GetNewsList()    {        List<NewsEntryLight> newsEntryLightList = new List<NewsEntryLight>();        for (int i = 0; i < 10++i)        {            newsEntryLightList.Add(new NewsEntryLight(i, string.Format("News Title {0}", i)));        }        return newsEntryLightList;    }    public static NewsEntry GetNewsDetail(int id)    {        NewsEntry newsEntry = new NewsEntry(                id,                string.Format("News Title {0}",id),                "在本卷第8章中,我们学习了Atlas Control Toolkit中专注于扩展现有ASP.NET服务器端控件的表现样式的表现扩展器控件。在本章中,我将继续介绍Atlas Control Toolkit中的另一类着眼于扩展现有控件功能的扩展器控件——功能扩展器控件。本章对每一个控件的介绍依然会采用第8章一样的方式,即分为应用场景、声明语法常用属性、示例程序、常见问题以及解决方案四个部分,让你不但知道如何使用,且清楚该在什么场景中使用。",                "Kalen_Chen",                DateTime.Now,                123            );        return newsEntry;    }} NewsService.asmx代码示例: using  System; using  System.Web; using  System.Collections; using  System.Web.Services; using  System.Web.Services.Protocols; /// <summary>/// NewsService 的摘要说明/// </summary> [WebService(Namespace  =   " http://tempuri.org/ " )][WebServiceBinding(ConformsTo  =  WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService()] public   class  NewsService : System.Web.Services.WebService  {    [WebMethod]    public string GetNewsDetailsHTMLString(string contextKey)    {        System.Threading.Thread.Sleep(2000);        int newsId;        if (!int.TryParse(contextKey, out newsId))        {            return string.Empty;        }        NewsEntry news = NewsData.GetNewsDetail(newsId);        System.Text.StringBuilder newsHTMLBuilder = new System.Text.StringBuilder();        newsHTMLBuilder.AppendFormat("<div style='text-align: center;'>posted by {0} at {1}, {2} views </div>",news.Author,news.PostDate.ToShortDateString(),news.Views);        newsHTMLBuilder.AppendFormat("<div>{0}</div>",news.Content);        return newsHTMLBuilder.ToString();    }    } DynamicPopulateDemo.aspx代码示例: <% @ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicPopulateDemo.aspx.cs" Inherits="Chapter09_DynamicPopulateDemo"  %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html  xmlns ="http://www.w3.org/1999/xhtml"   > < head  runat ="server" >      < title > DynamicPopulate Demo </ title >      < link  href ="styleSheet.css"  rel ="stylesheet"  type ="text/css"   />      < style  type ="text/css" >         .gridRow        {            padding:5px;        }        .updating        {            background-image:url('images/progress.gif');            background-repeat:no-repeat;            padding:2px;            height:2em;            margin:5px;        }     </ style > </ head > < body >      < form  id ="DynamicPopulateForm"  runat ="server" >          < asp:ScriptManager  ID ="sm"  runat ="server"   />          < div  class ="demoheading" > '穷人的'UpdatePanel--新闻管理 </ div >          < asp:GridView  ID ="newsList"  runat ="server"  AutoGenerateColumns ="false"  DataSourceID ="newsDataSource"             Width ="100%"  ShowHeader ="false" >              < Columns >                  < asp:TemplateField  ItemStyle-CssClass ="gridRow" >                      < ItemTemplate >                          < asp:HyperLink  ID ="hlMore"  Text ="more..."  NavigateUrl ="#"  runat ="server"   />                          < asp:Label  ID ="lbTitle"  runat ="server"  Text ='<%#  Bind("Title") % > ' Font-Bold="true" />                         < asp:Panel  ID ="newsDetails"  runat ="server"   />                          < ajaxToolkit:DynamicPopulateExtender  ID ="dp"  runat ="server"                              ContextKey ='<%  #Eval("Id") % > '                            PopulateTriggerControlID="hlMore"                            ServiceMethod="GetNewsDetailsHTMLString"                            ServicePath="NewsService.asmx"                            TargetControlID="newsDetails"                            UpdatingCssClass="updating"                            ClearContentsDuringUpdate="true" />                         <!--                             ContextKey:传递给ServiceMethod的上下文字符串,ServiceMethod可以借此区分不同的情况并相应地生成不同的结果                            PopulateTriggerControlID:触发目标控件更新的控件的ID。当且仅当用户点击了该控件,即对应着点击(Click)                                                      事件时,更新才会被触发                            ServiceMethod:Web Service中的Web Method名称或页面中Page Method的名称,该方法用来取得HTML字符串                            ServicePath:服务器端用来取得HTML字符串的Web Service的URL                            TargetControlID:该扩展器目标控件的ID。该控件中的内容将运行时被服务器端返回的一段HTML字符串所替代                            UpdatingCssClass:在更新目标控件的过程中,目标控件上所应用的CSS Class                            CustomScript:传递给ServiceMethod的上下文字符串,ServiceMethod可以借此区分不同的情况并相应地生成不同的结果                            ClearContentsDuringUpdate:若该属性值设置为true,那么在更新目标控件的过程中将清除其中的原有内容,否则将保留                         -->                      </ ItemTemplate >                  </ asp:TemplateField >              </ Columns >          </ asp:GridView >                  < asp:ObjectDataSource  ID ="newsDataSource"  runat ="server"             SelectMethod ="GetNewsList"             TypeName ="NewsData" >          </ asp:ObjectDataSource >              </ form > </ body > </ html >  

    最新回复(0)