【发布时间】:2014-04-07 11:12:17
【问题描述】:
我想通过单击按钮来添加一些控件。再次单击时,此控件会重复。然后点击另一个按钮保存所有控件的值。
所以我使用具有此控件的用户控件(FilterParameters):
<div class="main">
<asp:label ID="FilterByParam" runat="server" Text="filter" ></asp:label>
<asp:TextBox ID="txtFilter" runat="server" CssClass="NormalTextBox" Width="200px"></asp:TextBox>
</div>
<div class="main">
<asp:label ID="FilterRule" runat="server" Text="Rule"></asp:label>
<asp:TextBox ID="txtRule" runat="server" Width="330px" Height="50px" TextMode="MultiLine"></asp:TextBox>
</div>
我在我的代码中使用这个代码来加载这个用户控件(我添加了一个占位符来添加这个用户控件):
protected void lnkAddNew_Click(object sender, EventArgs e)
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
count++;
ViewState["count"] = count;
CreateControls();
}
private void CreateControls()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
while (placeholder.Controls.Count < count)
{
Common.FilterParameters fp = (Common.FilterParameters)LoadControl("~/Common/FilterParameters.ascx");
fp.ID = "fp" + placeholder.Controls.Count.ToString();
placeholder.Controls.Add(fp);
}
}
这个控件加载小心。但我从这个控件保存数据有问题。我使用此代码,但没有工作。 (placeholder.controls.count 始终为 0)
private void SaveFilters()
{
int count = 0;
if (ViewState["count"] != null)
{
count = (int)ViewState["count"];
}
string str=string.Empty;
for (int i = 0; i <= placeholder.Controls.Count; i++)
{
Common.FilterParameters fp = (Common.FilterParameters)placeholder.FindControl("fp"+i.ToString());
if(fp!=null)
{
string param = fp.filterparamText;
string rule = fp.RuleText;
str+="{"+param+"+"+rule+"}";
}
}
if(!string.IsNullOrEmpty(str))
{
save(str);
}
}
如何从这个用户控件获取数据?
【问题讨论】:
标签: c# asp.net user-controls