0

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

Andrey
  • 20,487
  • 26
  • 108
  • 176

2 Answers2

2

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.

David
  • 72,686
  • 18
  • 132
  • 173
1

try this:

void Page_Load(...)
{
    if (!IsPostBack)
    {
        rptTemplateParams.DataSource =Params;  // Params is List<string>
        rptTemplateParams.DataBind();
    }
}
Naeem Sarfraz
  • 7,360
  • 5
  • 37
  • 63