【问题标题】:Can't find the textbox with FindControl C#使用 FindControl C# 找不到文本框
【发布时间】:2016-07-27 18:43:06
【问题描述】:

我在尝试在同一页面上查找文本框控件时遇到了问题。当我调试它时,它一直给我一个空值。

这是我的代码:

public void UpdateTimeLog(string input)
    {
        string timeNumber = "txtTime" + input;
        TextBox myTextbox = (TextBox)FindControl(timeNumber);

        sqlConnection.Open();
        using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
        {
            command.Parameters.AddWithValue("@JobShtId", jobSheetId);
            command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
            command.ExecuteNonQuery();
        }
        sqlConnection.Close();
    }

问题已解决:我没有指定文本框控件在容器中(例如 ContentPlaceHolder1)。

以下是更正后的代码:

public void UpdateTimeLog(string input)
    {
        var container = Master.FindControl("ContentPlaceHolder1");
        string timeNumber = "txtTime" + input;
        TextBox myTextbox = (TextBox)container.FindControl(timeNumber);

        sqlConnection.Open();
        using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
        {
            command.Parameters.AddWithValue("@JobShtId", jobSheetId);
            command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
            command.ExecuteNonQuery();
        }
        sqlConnection.Close();
    }

【问题讨论】:

  • 而不是 find control.. 请尝试 Page.FindControl 和 this.Page.FindControl 并告诉我是否对您有帮助
  • 文本框在哪里定义?是否在其他一些容器控件中,例如 gridview?
  • 我这样做了,但是没有用。我发现它为什么一直返回null,原因是我没有指定控件在容器中。例如,ContentPlaceHolder1
  • 请尝试它应该可以工作.... var container = Master.FindControl("ContentPlaceHolder1"); var control = container.FindControl("txtNaam1");
  • 谢谢@maulik sakare,我几分钟前才意识到!

标签: c# asp.net .net findcontrol


【解决方案1】:

应该是工作试试吧,

 TextBox myTextBox = (TextBox)(this.Controls[("txtTime" + input)]).Text);

【讨论】:

    【解决方案2】:

    很难通过ID找到控件。所以试试这个代码

    public static Control[] FlattenHierachy(Control root)
            {
                List<Control> list = new List<Control>();
                list.Add(root);
                if (root.HasControls())
                {
                    foreach (Control control in root.Controls)
                    {
                        list.AddRange(FlattenHierachy(control));
                    }
                }
                return list.ToArray();
            }
    

      public void UpdateTimeLog(string input)
            {
                string timeNumber = "txtTime" + input;
               // TextBox myTextbox = (TextBox)FindControl(timeNumber);
                Control[] allControls = FlattenHierachy(Page);
                foreach (Control control in allControls)
                {
                    TextBox textBox = control as TextBox;
                    if (textBox != null && textBox.ID == timeNumber)
                    {
                        textBox.Text = "Hello";//Do your other stuff
                    }
                }
    
              //Rest is ommited
            }
    

    【讨论】:

    • 太棒了!我更喜欢这种方式。谢谢你和我分享这个
    【解决方案3】:

    你应该像这样发送你的文本框作为发送者对象:

    UpdateTimeLog("2",(TextBox)sender);
    

    当然,您也需要修改方法以接受 TextBox 对象。

    只有当您对多个文本框使用相同的事件处理程序时,这才有意义。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多