RustamIS
RustamIS

Reputation: 697

How to get all TextBox values from Repeater that contains UserControls?

I have one TextBox inside a UserControl, and this UserControl is repeating inside Repeater. But, when user fills TextBox with values and after that I can't get values from TextBoxs.

default.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    //filling repeater with dataset
    Repeater1.DataSource = ds;
    Repeater1.DataBind();
}

On button1 click I'm trying to fill List<string> with values from textbox.texts

protected void Button1_Click(object sender, EventArgs e)
{
    List<string> sss = new List<string>();
    foreach (Control i in Repeater1.Controls)
    {
        foreach (Control item in i.Controls)
        {
            if (item is WebUserControl1)
                sss.Add(((WebUserControl1)item).getString);
        }
     }
}

And UserControl code:

public string getString
 {
     get
     { return TextBox1.Text; }
 }

 protected void Page_Load(object sender, EventArgs e)
 {

 }

Upvotes: 4

Views: 3955

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

you should loop on all repeater's items and use FindControl to find your user control then call the getString method on such found instances, pseudo-code (not tested):

foreach(var rptItem in Repeater1.Items)
{
  WebUserControl1 itemUserControl = ((WebUserControl1)rptItem .FindControl("WebUserControl1"))

  if(itemUserControl  != null)
  {
    var itemText = itemUserControl.getString();
  }
}

Upvotes: 4

Related Questions