Using Repeater for an updateable form
I have a list of parameter names for which I want a user to type in some values, so I do this:
<div>
<asp:Repeater runat="server" ID="rptTemplateParams" EnableViewState="true">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label runat="server"><%#Container.DataItem%></asp:Label>
<asp:TextBox runat="server" ID="textParamValue"></asp:TextBox>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
<asp:Button runat="server" ID="Send" Text="Send Email" OnClick="Send_Click" />
and on server side:
void Page_Load(...)
{
rptTemplateParams.DataSource =Params; // Params is List<string>
rptTemplateParams.DataBind();
}
public void Send_Click(object sender, EventArgs e)
{
ParamDict = new Dictionary<string, string>();
foreach (RepeaterItem item in rptTemplateParams.Items)
{
if (item.ItemType == ListItemType.Item)
{
TextBox textParamValue = (TextBox)item.FindControl("textParamValue");
if (textParamValue.Text.Trim() != String.Empty)
{
// IT NEVER GETS HERE - textParamValue.Text IS ALWAYS EMPTY!!!
ParamDict.Add(item.DataItem.ToString(), textParamValue.Text);
}
}
}
}
As I put in the comment, i can't retrieve text box values - they are always empty. Am I r开发者_如何学编程etrieving those in the wrong place?
Thanks! Andrey
Try modifying your page_load like this
if(!Page.IsPostBack)
{
rptTemplateParams.DataSource =Params; // Params is List<string>
rptTemplateParams.DataBind();
}
The databinding wipes out existing controls and replaces them with new blank controls. You only want to databind when necessary.
try this:
void Page_Load(...)
{
if (!IsPostBack)
{
rptTemplateParams.DataSource =Params; // Params is List<string>
rptTemplateParams.DataBind();
}
}
精彩评论