Asp.Net Repeater 如何在 TextBox "TextChanged" 事件上找到另一个控件

Asp.Net Repeater How to find another control on TextBox "TextChanged" event

在我的项目中,我必须通过 Repeater 对象创建一些文本框。然后,每个文本框将应用一个 TextChanged 事件来做其他工作人员。

中继器项目的结构:

repeater item:
textbox.id = url
Label.id = webTitle

问题是如何使用 url_TextChanged 事件改变它自己的 label.text?

完整代码:

        //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?
        }

    }

您可以使用 TextBox 的 NamingContainer 然后 FindControl 方法通过其 id 找到标签,如下所示,

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;
               }
            }
        }

    }