【问题标题】:Asp.Net Repeater How to find another control on TextBox "TextChanged" eventAsp.Net Repeater 如何在 TextBox“TextChanged”事件上找到另一个控件
【发布时间】:2016-12-07 03:49:45
【问题描述】:

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

    }

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

    您可以使用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;
                   }
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      相关资源
      最近更新 更多