Reputation: 203
In my project, I have to create some text box by Repeater object. Then, each text box will be applied a TextChanged
event to do other staff.
The structure of a repeater item:
repeater item:
textbox.id = url
Label.id = webTitle
The question is how can I change its own label.text by using url_TextChanged event?
Full code:
//To create reperater item
repeater.DataSource = myObj;
repeater.DataBind();
foreach (RepeaterItem rptItm in repeater.Items)
{
CalendarObj item = calObj[rptItm.ItemIndex];
rptItm.Controls.Add(new LiteralControl("Enter your URL"));
TextBox url = new TextBox();
url.ID = "url";
url.AutoPostBack = true;
url.TextChanged += new EventHandler(urlTextBox_TextChanged);
url.Text = item.listURl;
rptItm.Controls.Add(url);
rptItm.Controls.Add(new LiteralControl("<br/>"));
rptItm.Controls.Add(new LiteralControl("web Title"));
Label title = new Label();
title.ID = "title";
title.Text = "";
rptItm.Controls.Add(title);
rptItm.Controls.Add(new LiteralControl("<br/>"));
rptItm.Controls.Add(new LiteralControl("<br/>"));
}
// Event
protected void urlTextBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
string theText = textBox.Text;
//How? textbox.parent.title.text = theText?
}
}
Upvotes: 3
Views: 2746
Reputation: 12022
You can use the NamingContainer
of the TextBox and then FindControl
method to find the Label by its id as below,
protected void urlTextBox_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
string theText = textBox.Text;
var item = (RepeaterItem) textBox.NamingContainer;
if(item != null) {
Label titleLabel = (Label)item.FindControl("title");
if(titleLabel != null) {
titleLabel.Text = theText;
}
}
}
}
Upvotes: 3