Andrey
Andrey

Reputation: 21285

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 retrieving those in the wrong place?

Thanks! Andrey

Upvotes: 0

Views: 807

Answers (2)

Naeem Sarfraz
Naeem Sarfraz

Reputation: 7430

try this:

void Page_Load(...)
{
    if (!IsPostBack)
    {
        rptTemplateParams.DataSource =Params;  // Params is List<string>
        rptTemplateParams.DataBind();
    }
}

Upvotes: 1

David
David

Reputation: 73574

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.

Upvotes: 2

Related Questions