码迷,mamicode.com
首页 > 其他好文 > 详细

动态生成页面(二)

时间:2015-07-30 21:32:56      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:动态加载

      在上篇文章动态生成页面(一)——ASP.NET中Literal使用中,介绍了通过Literal容器动态加载页面。但在取值过程中遇到了一些问题。但是因为拼接的控件textbox为服务器控件,所以在取值过程中遇到一些问题。


      几经周转,最后无奈改变了实现方法。直接实例化出控件,然后添加到页面中。具体实现过程如下:


      前台页面:

<body>
    <form id="form1" runat="server">
    <div id="test">
    
    </div>
    </form>
</body>


     后台拼接:

    #region SingleControl-单个组合控件-2015年7月27日-杨晓敏
    /// <summary>
    /// 单个组合控件
    /// </summary>
    /// <param name="model"></param>
    /// <param name="i"></param>
    public void SingleControl(IList<ModelBFieldDef> model, int i)
    {
        string Type = model[i].SFieldUitype;
        Label lblName = new Label();
        lblName.Width = 200;
        lblName.Style.Add("TEXT-ALIGN", "right");
        lblName.Text = model[i].SFieldNamecn;
        bool statusFlag;
        if (status == "edit")
        {
            statusFlag = true;
        }
        else
        {
            statusFlag = false;
        }
        int strRowId = Convert.ToInt32(strFieldPara);
        string strFieldId = model[i].SFieldId;
        IList<ModelBDataDef> ValueModel;
        BDataDefModel.NRowId = strRowId;
        BDataDefModel.SFieldId = strFieldId;
        ValueModel = _BLLBDataDef.GetList(BDataDefModel);
        string strValue = ValueModel[0].SValue;

        switch (Type)
        {
            case "String": //文本框类型 
                TextBox txtContent = new TextBox();
                txtContent.ID = model[i].SFieldId;
                txtContent.MaxLength = Convert.ToInt32(model[i].SFieldLen);
                tableContainer.Controls.Add(new LiteralControl("<br>"));
                tableContainer.Controls.Add(lblName);
                tableContainer.Controls.Add(new LiteralControl(" "));
                tableContainer.Controls.Add(txtContent);
                tableContainer.Controls.Add(new LiteralControl("<br>"));
                if (statusFlag)
                {
                    //string strID = "ctl00$ContentPlaceHolder1$" + model[i].SFieldId;
                    //TextBox strTxt = (TextBox)this.FindControl(strID) as TextBox;// test为获取控件的ID
                    txtContent.Text = strValue;

                }
                break;
            case "CheckBox": //复选框类型
                CheckBox cbxContent = new CheckBox();
                cbxContent.ID = model[i].SFieldId;
                tableContainer.Controls.Add(new LiteralControl("<br>"));
                tableContainer.Controls.Add(lblName);
                tableContainer.Controls.Add(new LiteralControl(" "));
                tableContainer.Controls.Add(cbxContent);
                tableContainer.Controls.Add(new LiteralControl("<br>"));
                if (statusFlag)
                {
                    string strID = "ctl00$ContentPlaceHolder1$" + model[i].SFieldId;
                    CheckBox strTxt = (CheckBox)this.FindControl(strID) as CheckBox;// test为获取控件的ID
                    if (strValue == "on")
                    {
                        strTxt.Checked = true;
                    }
                    else
                    {
                        strTxt.Checked = false;
                    }
                }
                break;            
            case "Select": //下拉框类型
                DropDownList ddl = new DropDownList();
                ddl.ID = model[i].SFieldId;
                if (model[i].SDictType == "1")
                {
                    string str = model[i].SDictId;
                    //以;; , ,分割字符
                    char[] strSplit = { ';', ';', ',', ',' };
                    string[] datas = str.Split(strSplit);

                    ddl.Items.Add("");
                    for (int n = 0; n < datas.Length; n++)
                    {
                        ddl.Items.Add(datas[n]);
                    }
                }
                tableContainer.Controls.Add(new LiteralControl("<br>"));
                tableContainer.Controls.Add(lblName);
                tableContainer.Controls.Add(new LiteralControl(" "));
                tableContainer.Controls.Add(ddl);
                tableContainer.Controls.Add(new LiteralControl("<br>"));

                ddl.SelectedValue = strValue;
                if (statusFlag)
                {
                    //string strID = "ctl00$ContentPlaceHolder1$" + model[i].SFieldId;
                    //TextBox strTxt = (TextBox)this.FindControl(strID) as TextBox;// test为获取控件的ID
                    //strTxt.Text = strValue;
                    ddl.SelectedValue = strValue;
                }
                break;            
            default:
                break;
        }
    }
    #endregion


      获取拼接控件的值


      获取控件的值,可以通过FindControl来取值或赋值。如,要取TextBox1的值,则可以:

protected void Button1_Click(object sender, EventArgs e)
{
    //Control c = this.FindControl("TextBox1");
    //TextBox tb= (TextBox)c;
    //FindControl返回的是一个Control类型的控件,需要强制类型转化成TextBox类型
    TextBox tb=(TextBox)this.FindControl("TextBox1") as TextBox;
    this.Label1.Text = tb.Text;    
}


      需要注意的是,如果TextBox被放在其他容器里,其ID值可能发生改变,如放在Datagrid中,从HTML页面中查看其ID为:

<input name="dg1$ctl02$TextBox1" type="text" id="dg1_ctl02_TextBox1" />



      在获取TextBox控件时,我们就需要改变FindControl中Id的值。在实现过程中因为忽略了这一点,一直取不到值,在调试上浪费了很多时间。








版权声明:本文为博主原创文章,未经博主允许不得转载。

动态生成页面(二)

标签:动态加载

原文地址:http://blog.csdn.net/u010924897/article/details/47154577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!