webform在指定的div中添加动态添加label,该怎么解决
1个回答
展开全部
动态生成Asp.Net服务器控件,并取其值
C# codeprivate 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 />");
});
});
C# codeprivate 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 />");
});
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询