轻松学习Asp.net中的控件

   C/S 结构,即大家熟知的客户机和服务器结构。它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销。目前大多数应用软件系统都是Client/Server形式的两层结构,由于现在的软件应用系统正在向分布式的Web应用发展,Web和Client/Server 应用都可以进行同样的业务处理,应用不同的模块共享逻辑组件;因此,内部的和外部的用户都可以访问新的和现有的应用系统,通过现有应用系统中的逻辑可以扩展出新的应用系统。这也就是目前应用系统的发展方向。 C/S结构即大家熟悉的客户端服务器结构。从进入提高班开始,我们就学习着C/S方面的知识,现在,我们开始接触B/S方面的内容,现在学习的ASP.NET既可以开发C/S,又可以开发B/S,web应用程序的平台。在由C/S向B/S过度的这个阶段,.net平台演绎着举足轻重的作用。这里先从.NET平台中的控件进行简单介绍。

      由于前边我们学习VB 6.0,VB.NET并相继开发了,学生管理系统,机房收费系统,两个客户端服务器应用程序。对于Windows窗体的设计,还是非常熟悉,随着学习的深入,现在我们的视角转移到了.NET平台,但是换汤不换药,大概还是那些东西,只是有些用法不同而已,这个需要我们下来多实践才可以,这块的学习,完全可以对比VB 6.0开发工具来学习,很多控件的很多属性方法都相同,这里我们需要掌握的就是熟悉相同,清楚不同的。 学习每一种语言必不可少的就是控件的学习。每种语言的开发环境中的控件也都大同小异。Asp.net控件很简单,设置一下控件的属性直接把控件拿过来用就可以了,而在Cs中如果控件要验证一下是否为空还要编写代码,这是Asp.net的优势。该篇博文,小编主要简单总结一下Asp.net中的控件,在举一个简单的小例子。如下图所示:

       

       一如我们上图所展示的,在Asp.net中的控件类型总共有四种,每种类型的使用需要我们在实践中多多的联系,方能熟练掌握,在这里,小编主要重点讲解一下Repeater控件的使用,Repeater控件用户显示重复的项目列表,这些项目被限制在该控件。Repeater控件有5个模板,下面来应用一下,如下:

         web窗体代码:

        

  1. <asp:Panel ID="Panel1" runat="server" Height="297px" Width="496px">   
  2.             <asp:Repeater ID="Repeater1" runat="server">   
  3.                 <ItemTemplate>   
  4.                     <%# DataBinder.Eval(Container.DataItem,"pID") %><%# DataBinder.Eval(Container.DataItem,"personName") %>   
  5.                 </ItemTemplate>   
  6.                    
  7.                 <AlternatingItemTemplate>   
  8.                     <font color="red">   
  9.                     <%# DataBinder.Eval(Container.DataItem,"pID") %> <br />   
  10.                     <%# DataBinder.Eval(Container.DataItem,"personName") %>   
  11.                    </font>   
  12.                 </AlternatingItemTemplate>   
  13.                    
  14.                 <HeaderTemplate>    
  15.                     <h3>模板页眉</h3>   
  16.                 </HeaderTemplate>   
  17.                   
  18.                  <FooterTemplate>    
  19.                     <h3>模板页脚</h3>   
  20.                 </FooterTemplate>   
  21.                 <SeparatorTemplate>   
  22.                     <hr />   
  23.                     <hr />   
  24.                 </SeparatorTemplate>   
  25.             </asp:Repeater>   
  26.         </asp:Panel>   
<asp:Panel ID="Panel1" runat="server" Height="297px" Width="496px">  
            <asp:Repeater ID="Repeater1" runat="server">  
                <ItemTemplate>  
                    <%# DataBinder.Eval(Container.DataItem,"pID") %><%# DataBinder.Eval(Container.DataItem,"personName") %>  
                </ItemTemplate>  
                  
                <AlternatingItemTemplate>  
                    <font color="red">  
                    <%# DataBinder.Eval(Container.DataItem,"pID") %> <br />  
                    <%# DataBinder.Eval(Container.DataItem,"personName") %>  
                   </font>  
                </AlternatingItemTemplate>  
                  
                <HeaderTemplate>   
                    <h3>模板页眉</h3>  
                </HeaderTemplate>  
                 
                 <FooterTemplate>   
                    <h3>模板页脚</h3>  
                </FooterTemplate>  
                <SeparatorTemplate>  
                    <hr />  
                    <hr />  
                </SeparatorTemplate>  
            </asp:Repeater>  
        </asp:Panel>  
          后台代码:

         

  1. public partial class repeaterControl : System.Web.UI.Page   
  2.     {   
  3.         protected void Page_Load(object sender, EventArgs e)   
  4.         {   
  5.             if (!this.IsPostBack)   
  6.             {   
  7.                 this.Label3.Text = "1";   
  8.                 this.dataBindToRepeater();   
  9.        
  10.             }   
  11.    
  12.         }   
  13.         /// <summary>   
  14.         /// 构造一个方法,打开数据库和查询数据库中数据,并分页显示   
  15.         /// </summary>   
  16.         private void dataBindToRepeater()   
  17.         {   
  18.             //定义当前页   
  19.             int curPage = Convert.ToInt32(this.Label3.Text);   
  20.               
  21.             //连接打开数据库并查询   
  22.             SqlConnection con = DB.createCon();   
  23.             SqlDataAdapter sda = new SqlDataAdapter();   
  24.             sda.SelectCommand = new SqlCommand("select * from person", con);   
  25.             DataSet ds = new DataSet();   
  26.             sda.Fill(ds,"ado");   
  27.             System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();   
  28.             ps.DataSource = ds.Tables["ado"].DefaultView;   
  29.             ps.AllowPaging = true;   
  30.             //每页显示三行   
  31.             ps.PageSize = 3;   
  32.             ps.CurrentPageIndex = curPage - 1;   
  33.                
  34.             this.Button1.Enabled = true;   
  35.             this.Button2.Enabled = true;   
  36.             //如果是第一页,上一页按钮不能用   
  37.             if (curPage==1)   
  38.             {   
  39.                 this.Button1.Enabled = false;   
  40.             }   
  41.             //如果是最后一页下一页按钮不能用   
  42.             if (curPage==ps.PageCount)   
  43.             {   
  44.                 this.Button2.Enabled = false;      
  45.             }   
  46.    
  47.             //数据源绑定   
  48.             this.Repeater1.DataSource = ps;   
  49.             this.Repeater1.DataBind();   
  50.         }   
  51.    
  52.            
  53.         /// <summary>   
  54.         /// 上一页   
  55.         /// </summary>   
  56.         /// <param name="sender"></param>   
  57.         /// <param name="e"></param>   
  58.         protected void Button1_Click(object sender, EventArgs e)   
  59.         {   
  60.             //将label3的内容强制转换为字符串类型   
  61.             this.Label3.Text =Convert.ToString( Convert.ToInt32(this.Label3.Text)-1);   
  62.             this.dataBindToRepeater();   
  63.         }   
  64.         /// <summary>   
  65.         /// 下一页   
  66.         /// </summary>   
  67.         /// <param name="sender"></param>   
  68.         /// <param name="e"></param>   
  69.         protected void Button2_Click(object sender, EventArgs e)   
  70.         {   
  71.             //将label3的内容强制转换为字符串类型   
  72.             this.Label3.Text = Convert.ToString(Convert.ToInt32(this.Label3.Text) + 1);   
  73.             this.dataBindToRepeater();   
  74.         }   
  75.    
  76.     }   
public partial class repeaterControl : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!this.IsPostBack)  
            {  
                this.Label3.Text = "1";  
                this.dataBindToRepeater();  
      
            }  
  
        }  
        /// <summary>  
        /// 构造一个方法,打开数据库和查询数据库中数据,并分页显示  
        /// </summary>  
        private void dataBindToRepeater()  
        {  
            //定义当前页  
            int curPage = Convert.ToInt32(this.Label3.Text);  
             
            //连接打开数据库并查询  
            SqlConnection con = DB.createCon();  
            SqlDataAdapter sda = new SqlDataAdapter();  
            sda.SelectCommand = new SqlCommand("select * from person", con);  
            DataSet ds = new DataSet();  
            sda.Fill(ds,"ado");  
            System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();  
            ps.DataSource = ds.Tables["ado"].DefaultView;  
            ps.AllowPaging = true;  
            //每页显示三行  
            ps.PageSize = 3;  
            ps.CurrentPageIndex = curPage - 1;  
              
            this.Button1.Enabled = true;  
            this.Button2.Enabled = true;  
            //如果是第一页,上一页按钮不能用  
            if (curPage==1)  
            {  
                this.Button1.Enabled = false;  
            }  
            //如果是最后一页下一页按钮不能用  
            if (curPage==ps.PageCount)  
            {  
                this.Button2.Enabled = false;     
            }  
  
            //数据源绑定  
            this.Repeater1.DataSource = ps;  
            this.Repeater1.DataBind();  
        }  
  
          
        /// <summary>  
        /// 上一页  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button1_Click(object sender, EventArgs e)  
        {  
            //将label3的内容强制转换为字符串类型  
            this.Label3.Text =Convert.ToString( Convert.ToInt32(this.Label3.Text)-1);  
            this.dataBindToRepeater();  
        }  
        /// <summary>  
        /// 下一页  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        protected void Button2_Click(object sender, EventArgs e)  
        {  
            //将label3的内容强制转换为字符串类型  
            this.Label3.Text = Convert.ToString(Convert.ToInt32(this.Label3.Text) + 1);  
            this.dataBindToRepeater();  
        }  
  
    }  
      显示效果如下:

     

        PS:在这里小编主要简单总结一下Repeater控件中的五个模板,如下图所示:

      

      小编寄语:该博文,小编主要简单的介绍了一下Asp.net中的控件,举了一个小例子,对于这块的学习,我们可以对照着以前学习BS方面的内容,控件的使用需要在实践中多多联系,方能熟练掌握,因为古语云,熟能生巧,C/S学习,未完,待续......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值