ASP.NET動態建立控制項之絕境求生

來源:互聯網
上載者:User

在ASP.NET中動態建立一個控制項總是不那麼順利,特別是當對頁面的Life Cycle不是那麼瞭然的情況下!這裡簡單描述一下要求,然後提供一個解決方案,大家看看有沒有更好的Idea,如果有的話就是我的大幸了,呵呵!

要求:頁面上有一個Add按鈕,每點擊一次該按鈕,頁面上動態建立一個WebPartZone!
提醒:WebPartZone只能在OnInit或之前才能建立,否則報異常!

大家都知道,按鈕的點擊事件是在RaisePostbackEvent時觸發的,這意味著點擊事件在OnLoad階段之後才執行,遠遠落後於OnInit階段,而且ViewState在OnLoad時才準備好,OnInit以及之前的階段根本就不能使用ViewState!如果試圖在按鈕點擊事件裡面建立WebPartZone等控制項,唯一的後果就是頁面出錯;而如果在OnInit裡面建立控制項,由於ViewState沒有準備好,那麼有些資料比如當前需要建立的個數(存在ViewState裡面)就無法獲得!

目前對這個問題我還沒有找到什麼好的解決方案,經過實驗,勉強得出一個不怎麼優雅的方案,就是利用HiddenField儲存資料,然後直接使用Request.Form["XXX"]在OnInit階段取得資料;而判斷是否點擊按鈕也是通過Request.Form是否存在對應資料來判斷的!廢話不多說了,大家看看代碼吧!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            <br />
            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />&nbsp;
            <asp:HiddenField ID="hfCount" runat="server" Value="0" />
        </div>
    </form>
</body>
</html>

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;

public partial class _Default : System.Web.UI.Page 
{
    private int _count = 0;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        // 取得以前已建立控制項的個數
        if (!String.IsNullOrEmpty(this.Request["hfCount"]))
        {
            _count = Convert.ToInt32(this.Request["hfCount"]);
        }

        // 假如按下“Add”按鈕,那麼count加一
        string target = this.Request["btnAdd"];
        if (target == "Add")
        {
            _count++;
        }

        // 動態建立控制項
        for (int i = 0; i < _count; i++)
        {   // 這裡以TextBox為例,實際上需要建立的是WebPartZone
            TextBox newTextbox = new TextBox();
            newTextbox.ID = "TXT" + i.ToString();
            this.PlaceHolder1.Controls.Add(newTextbox);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        hfCount.Value = _count.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        // 不能在此添加WebPartZone控制項,只能在OnInit或之前,否則報異常
    }
}

希望大家能提出更好的解決方案,我拭目以待,呵呵@_@

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.