Asp.net C#和#x27;FindControl';在当前上下文中不存在 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 使用系统配置; 使用System.Data.SqlClient; 使用System.Web.UI; 公共文本框(字符串查询) { 字符串[]Vars=query.Split('@'); 对于(int x=1;x

Asp.net C#和#x27;FindControl';在当前上下文中不存在 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 使用系统配置; 使用System.Data.SqlClient; 使用System.Web.UI; 公共文本框(字符串查询) { 字符串[]Vars=query.Split('@'); 对于(int x=1;x,asp.net,webforms,findcontrol,Asp.net,Webforms,Findcontrol,我收到错误“当前上下文中不存在名称'FindControl'。我正试图根据字符串提供的名称填充文本框数组 e、 文本框[2]=Vars[x]+文本框 我是否缺少一些名称空间?当我查找FindControl时,它简单地告诉我名称空间是“System.Web.UI”,并且我在引用中添加了“System.Web”。正如@Equalsk所说,我需要使用控件。Find(…);我只是使用了错误的东西。我想我应该将此作为一个答案来发布,以确保它脱颖而出,因为虽然这可能是一件非常简单的事情,但我(和其他人一样)

我收到错误“当前上下文中不存在名称'FindControl'。我正试图根据字符串提供的名称填充文本框数组

e、 文本框[2]=Vars[x]+文本框


我是否缺少一些名称空间?当我查找FindControl时,它简单地告诉我名称空间是“System.Web.UI”,并且我在引用中添加了“System.Web”。

正如@Equalsk所说,我需要使用控件。Find(…);我只是使用了错误的东西。我想我应该将此作为一个答案来发布,以确保它脱颖而出,因为虽然这可能是一件非常简单的事情,但我(和其他人一样)完全不知道这个功能

` 静态文本框[]值

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using System.Configuration;  
using System.Data.SqlClient;  
using System.Web.UI;  

public TextBoxes(string query)  
    {  
        String[] Vars = query.Split('@');  
        for (int x = 1;x < Vars.Length;x++)  
        {  
            TextBox tb = (TextBox)FindControl(Vars[x] + "TextBox") as TextBox;  
        }  
    }  
publicstatictextbox[]文本框(字符串查询,表单当前)
{
字符串[]Vars=query.Split('@');
字符串[]NewVars=新字符串[Vars.Length-1];
值=新文本框[Vars.Length-1];
对于(int x=0;x
`


这是更新后的代码。“Form Current”只是允许我将其用于我的任何表单,因为它位于一个全局类中。

这是一个web项目还是WinForms?我猜您只需要
控件。Find(…);
-请参阅为什么在同一句话中强制转换和“as”?
(TextBox)FindControl(Vars[x]+“TextBox”)正如TextBox
Here
FindControl()
看起来是一个自定义方法,而不是控件类方法。这不是实际的代码,它不在类中。您将此代码放在哪里了?它在类中,但我不想复制和粘贴整个代码。它是一个windows窗体。
    public static TextBox[] TextBoxes(string query, Form Current)
    {
        String[] Vars = query.Split('@');

        String[] NewVars = new String[Vars.Length - 1];
        Values = new TextBox[Vars.Length - 1];

        for (int x = 0; x < Vars.Length - 1; x++)
        {
            NewVars[x] = Vars[x + 1].Remove((Vars[x + 1].Length - 1));
            Values[x] = Current.Controls.Find(NewVars[x] + "TextBox", true).FirstOrDefault() as TextBox;
        }

        return Values;
    }