我在代码中使用了可扩展的列表视图。我在.aspx文件中使用了一个占位符,并在.aspx.cs文件加载动态文本框中使用了代码。我提供了用于生成文本框的独特ID,但我不知道如何在生成的文本框中输入值。任何帮助都将不胜感激。
当我尝试使用SQ1.text
在文本框中输入的用户输入值时,它不起作用。如何在动态生成的文本框中获取输入的值?
.aspx文件
<div class="form-group inline clearfix"">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<asp:PlaceHolder ID="plhMainItem" runat="server"></asp:PlaceHolder>
</div>
</div>
.aspx.cs文件
private void LoadData(bool postback)
{
List<String> qualifications = new List<String>();
qualifications.Add("Professional Qualifications");
qualifications.Add("Special qualifications");
qualifications.Add("Professional Experience");
for (int i = 0; i < qualifications.Count; i++)
{
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "pnldefault" + i;
newControl.Attributes["Class"] = "panel panel-default";
HtmlGenericControl headraw = new HtmlGenericControl("div");
headraw.ID = "headRawDiv" + i;
headraw.Attributes["class"] = "panel-heading";
headraw.Attributes["role"] = "tab";
HtmlGenericControl bodyraw = new HtmlGenericControl("div");
bodyraw.ID = "bodyRawDiv" + i;
bodyraw.Attributes["class"] = "tbl-body row";
HtmlGenericControl nameraw = new HtmlGenericControl("div");
nameraw.ID = "nameRawDiv" + i;
nameraw.Attributes["class"] = "col-md-7 col-sm-4 col-xs-5 tproname";
HyperLink deslink = new HyperLink();
deslink.Attributes["data-toggle"] = "collapse";
deslink.Attributes["Class"] = "collapsed";
deslink.Attributes["data-parent"] = "#accordion";
deslink.Attributes["href"] = "#MainContent_detailsRawDiv" + i;
deslink.Attributes["aria-expanded"] = "true";
deslink.Attributes["aria-controls"] = "MainContent_detailsRawDiv" + i;
deslink.Text = qualifications[i];
nameraw.Controls.Add(deslink);
bodyraw.Controls.Add(nameraw);
headraw.Controls.Add(bodyraw);
newControl.Controls.Add(headraw);
HtmlGenericControl details = new HtmlGenericControl("div");
details.ID = "detailsRawDiv" + i;
details.Attributes["class"] = "panel-collapse collapse";
details.Attributes["role"] = "tabpanel";
details.Attributes["aria-labelledby"] = "MainContent_headRawDiv" + i;
HtmlGenericControl pnlBody = new HtmlGenericControl("div");
pnlBody.ID = "pnlBodyDiv" + i;
pnlBody.Attributes["class"] = "panel-body";
HtmlGenericControl tableBody = new HtmlGenericControl("div");
tableBody.ID = "tableBodyDiv" + i;
tableBody.Attributes["class"] = "tbl-body row";
HtmlGenericControl tableRawBody = new HtmlGenericControl("div");
tableRawBody.ID = "tableRawBodyDiv" + i;
tableRawBody.Attributes["class"] = "tproname";
HtmlGenericControl ul = new HtmlGenericControl("ul");
ul.ID = "lstDetails" + i;
if (qualifications[i] == "Professional Qualifications")
{
for (int j = 1; j <= 3; j++)
{
TextBox tb = new TextBox();
tb.ID = "PQ" + j.ToString();
tb.Attributes.Add("placeholder", "Professional Qualifications " + j.ToString());
tb.Attributes["class"] = "form-control";
tb.Height = 100;
ul.Controls.Add(tb);
}
}
else if (qualifications[i] == "Special qualifications")
{
for (int j = 1; j <= 3; j++)
{
TextBox tb = new TextBox();
tb.ID = "SQ" + j.ToString();
tb.Attributes.Add("placeholder", "Special qualifications " + j.ToString());
tb.Attributes["class"] = "form-control";
tb.Height = 100;
ul.Controls.Add(tb);
}
}
else if (qualifications[i] == "Professional Experience")
{
for (int j = 1; j <= 3; j++)
{
TextBox tb = new TextBox();
tb.ID = "PE" + j.ToString();
tb.Attributes.Add("placeholder", "Name of the work place " + j.ToString());
tb.Attributes["class"] = "form-control";
ul.Controls.Add(tb);
}
}
tableRawBody.Controls.Add(ul);
tableBody.Controls.Add(tableRawBody);
pnlBody.Controls.Add(tableBody);
details.Controls.Add(pnlBody);
newControl.Controls.Add(details);
plhMainItem.Controls.Add(newControl);
}
}
您可以在plhmainitem上使用FindControl来获取具有ID
的文本框int index = 1;
TextBox tb = null;
do
{
tb = plhMainItem.FindControl("SQ"+i++) as TextBox;
if(tb != null)
{
// You can access the value of TextBox using Text property, tb.Text
}
} while(tb != null)
一种方法是直接使用从页面上获取所有已发布数据的Request.Form
。为了使它起作用,您还需要在控件上添加name
。
这是您可以做到的。
TextBox tb = new TextBox();
tb.ID = "PQ1" ;
// need to add the name, so later to get the post back using that.
tb.Attributes["name"] = "PQ1";
tb.Attributes.Add("placeholder", "Professional Qualifications 1" );
tb.Attributes["class"] = "form-control";
tb.Height = 100;
if (IsPostBack)
txtDebug.Text = "Value is: " + Request.Form["PQ1"];