2011年3月1日 星期二

動態載入User control

做一個按鈕來動態載入User control,按一次增加一個
必須在Postback後保留。在網路上找到章老師的文章參考後,利用Viewstate來解決
取出動態載入的uc 取其值進行運算儲存。

引用
<%@ Reference Control="~/UserControl/EFlow/EFlowConditional.ascx" %>

加減按鈕
        <asp:ImageButton ID="YTImgBut_cdtl_add" runat="server" ToolTip="增加條件式" ImageUrl="~/images/icon/list-add.png"
            OnClick="YTImgBut_cdtl_add_Click" />&nbsp;
        <asp:ImageButton ID="ImgBut_cdtl_Remove" runat="server" ImageUrl="~/images/icon/trash.gif"
            OnClick="ImgBut_cdtl_Remove_Click" />
容器
<asp:PlaceHolder ID="PlaceHolder_cdtl1" runat="server"></asp:PlaceHolder>

新增時ViewState遞增
protected void YTImgBut_cdtl_add_Click(object sender, EventArgs e)
        {
            ViewState["DesignerMode"] = "Add";
            if (ViewState["cdtlID"] == null) ViewState["cdtlID"] = 1;
            else ViewState.Add("cdtlID", Convert.ToInt32(ViewState["cdtlID"]) + 1);
            SetUserControl(ViewState["cdtlID"].ToString());
        }

poskback回來後要如數加回
 if (IsPostBack)
            {
                SetUserControl();
                int ci = 0;
                if (int.TryParse(ViewState["cdtlID"].ToString(), out ci))
                {
                    for (int i = 1; i <= ci; i++)
                    {
                        SetUserControl(i.ToString());
                    }
                }
            }
            else
            {
                ViewState["DesignerMode"] = "Init";
                ViewState["cdtlID"] = 0;
                SetUserControl();

                for (int i = 1; i <= _SelectCount - 1; i++)
                {
                    ViewState["DesignerMode"] = "Add";
                    ViewState.Add("cdtlID", Convert.ToInt32(ViewState["cdtlID"]) + 1);
                    SetUserControl(i.ToString());
                }
            }

動態取值
 if (int.TryParse(ViewState["cdtlID"].ToString(), out ci))
            {
                for (int i = 0; i <= ci; i++)
                {
                    string controlid = string.Format("yt_eflw_ctdl_{0}", i.ToString());
                    UserControl_EFlow_EFlowConditional cdtl;
                    if (i == 0) cdtl = (UserControl_EFlow_EFlowConditional)PlaceHolder_cdtl0.FindControl(controlid);
                    else cdtl = (UserControl_EFlow_EFlowConditional)PlaceHolder_cdtl1.FindControl(controlid);
                    string n = cdtl.ConditionalName;
                    int sch = cdtl.ConditionalSch;
                    string fd = cdtl.ConditionalField;
                    string lg = cdtl.ConditionalLogi;
                    string v = cdtl.ConditionalValue;
                    string flid = cdtl.FlowID;
                    bool en = cdtl.IsEnable;
                    if (SaveConfig(_FormID, n, sch, fd, lg, v, flid, en)) i_ok++;
                    i_all++;
                }
p.s.注意to me (textbox)不要用到yt版!!
--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言: