輕鬆學習Asp.net控制項

來源:互聯網
上載者:User

          學習每一種語言必不可少的就是控制項的學習。每種語言的開發環境中的控制項也都大同小異。Asp.net控制項很簡單,設定一下控制項的屬性直接把控制項拿過來用就可以了,而在Cs中如果控制項要驗證一下是否為空白還要編寫代碼,這是Asp.net的優勢。

       而每當點擊ASP.NET的Web網頁上的Button、LinkButton或ImageButton等控制項時,表單就會被發送到伺服器上。如果控制項的AutoPostBack屬性設定成true的話,每次控制項的狀態改變,將發送一個request到伺服器,這種頻繁的與伺服器打交道,增加伺服器的壓力,所以現在使用了Ajax。


asp.net控制項總結:

            

        asp.net控制項總共有四種類型,每種類型的使用也都相同,只要知道各自的不同的地方就可以輕鬆掌握了。

舉例子:

          Repeater控制項的使用:Repeater控制項使用者顯示重複的項目列表,這些項目被限制在該控制項。Repeater控制項有5個模板,下面來應用一下。

      web表單代碼:

<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>


後台代碼:

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();        }    }


顯示結果:

        

 這樣就做好了。

    總結:

           Repeater控制項的5個模板分別是

          <ItemTemplate></ItemTemplate>:為資料來源中每個資料項目都要呈現一次的HTML元素和控制項。
         <AlternatingItemTemplate>  </AlternatingItemTemplate>:為資料來源中每個資料項目都要呈現一次的HTML元素和控制項。
         <HeaderTemplate></HeaderTemplate>:包含在列表的開頭呈現的文本和控制項
         <FooterTemplate> </FooterTemplate>:包含在列表的結束呈現的文本和控制項
         <SeparatorTemplate></SeparatorTemplate>:包含在每項之間呈現的元素。典型樣本是一條直線。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.