2016-12-07 106 views
2

在我的项目中,我必须通过Repeater对象创建一些文本框。然后,每个文本框将应用TextChanged事件来完成其他人员。Asp.Net Repeater如何找到TextBox“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

您可以使用文本框的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; 
       } 
      } 
     } 

    } 
相关问题