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

时间:2016-12-07 03:49:45

标签: c# asp.net repeater

在我的项目中,我必须通过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?
        }

    }

1 个答案:

答案 0 :(得分:1)

您可以使用TextBox的NamingContainer,然后使用FindControl方法按ID查找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;
               }
            }
        }

    }