How to get all TextBox values โ€‹โ€‹from Repeater that contain UserControls?

I have a TextBox inside a UserControl , and this UserControl repeated inside the Repeater . But when the user fills the TextBox values โ€‹โ€‹and after that I can not get the values โ€‹โ€‹from TextBox s.

default.aspx:

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

In button1 click, I'm trying to populate the List<string> values โ€‹โ€‹from textbox.text s

 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) { } 
+4
source share
1 answer

you should loop around all the elements of the repeater and use FindControl to find your user control, and then call the getString method for such found instances, pseudo-code (not verified):

 foreach(var rptItem in Repeater1.Items) { WebUserControl1 itemUserControl = ((WebUserControl1)rptItem .FindControl("WebUserControl1")) if(itemUserControl != null) { var itemText = itemUserControl.getString(); } } 
+4
source

Source: https://habr.com/ru/post/1394202/


All Articles