日期:2014-05-18  浏览次数:20586 次

webform在指定的div中添加动态添加label
RT

------解决方案--------------------
二、动态生成Asp.Net服务器控件,并取其值

C# code

private void LoadProperty(int cateId)
{

    HtmlGenericControl span = new HtmlGenericControl();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {     
         StringBuilder strHtml = new StringBuilder();
         if (i % 2 == 0)
         {
              strHtml.Append("<div >");

              strHtml.Append("<div class=\"layout1\">");
         }
         else
         {                      
                        strHtml.Append("<div >");
          }                   

          TextBox txt = new TextBox();
          Label lbl = new Label();
          HiddenField hdf = new HiddenField();//
          lbl.ID = "lblProp" + i.ToString();
          lbl.Text = ds.Tables[0].Rows[i]["PropName"].ToString() + ":";
          txt.ID = "txtProp" + i.ToString();

          hdf.ID = "hdfProp" + i.ToString();//

          lbl.EnableViewState = true;
          txt.EnableViewState = true;
          hdf.EnableViewState = true;//

          LiteralControl lc = new LiteralControl(strHtml.ToString());

          lc.EnableViewState = true;

          span.Controls.Add(lc);
          span.Controls.Add(lbl);
          span.Controls.Add(txt);
          span.Controls.Add(hdf);//                   
          if (i % 2 == 0)
          {
                span.Controls.Add(new LiteralControl("</div></div>"));
          }
          else
           {
                 span.Controls.Add(new LiteralControl("</div>"));
           }       
       }
 }

动态生成的控件,当点击页面按钮时会刷新页面,所以,生成控件的方法不需要

if(!IsPostBack)
{
}

------解决方案--------------------
JScript code

$(function(){           
                $("#addLabel").click(function(){
                $("#divAddLabel").append("<label>添加的label<label/><br />");
            });
        });