Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在页面上查找控件并获取文本_C#_Asp.net - Fatal编程技术网

C# 在页面上查找控件并获取文本

C# 在页面上查找控件并获取文本,c#,asp.net,C#,Asp.net,我试图获取标签的文本并将其分配给字符串,但它似乎无法找到控件。请注意,这是在我的页面的代码隐藏中完成的(代码隐藏的自动生成功能不正常,因此我必须手动查找) 公共作废获取日期() { var controlList=ProductPromotion.Controls; foreach(controlList中的控件) { if(控件为TableRow) { var newControl=control.FindControl(“StartDate”); if(newControl!=null) {

我试图获取标签的文本并将其分配给字符串,但它似乎无法找到控件。请注意,这是在我的页面的代码隐藏中完成的(代码隐藏的自动生成功能不正常,因此我必须手动查找)

公共作废获取日期()
{
var controlList=ProductPromotion.Controls;
foreach(controlList中的控件)
{
if(控件为TableRow)
{
var newControl=control.FindControl(“StartDate”);
if(newControl!=null)
{
标签startControl=newControl作为标签;
startDate=startControl.Text;
}
}
}
织物。设置提供程序。写入设置(开始日期,开始设置);
}

FindControl
方法不是递归的。尝试已以Linq样式更新的代码,作为扩展方法:

    public static IEnumerable<TControl> FindDescendants<TControl>(this Control parent) 
        where TControl : Control
    {
        if (parent == null) throw new ArgumentNullException("control");

        if (parent.HasControls())
        {
            foreach (Control childControl in parent.Controls)
            {
                var candidate = childControl as TControl;
                if (candidate != null) yield return candidate;

                foreach (var nextLevel in FindDescendants<TControl>(childControl))
                {
                    yield return nextLevel;
                }
            }
        }
    }
公共静态IEnumerable FindDescents(此控件父控件)
其中TControl:Control
{
如果(parent==null)抛出新的ArgumentNullException(“control”);
if(parent.HasControls())
{
foreach(父控件中的控件childControl)
{
var候选者=作为TControl的childControl;
如果(candidate!=null)生成返回候选者;
foreach(FindDescents中的下一级变量(儿童控制))
{
下一级收益率;
}
}
}
}
用法:

    if (control is TableRow)
    {
        var newControl = control.FindDescendants<Label>()
            .Where(ctl=>ctl.ID =="StartDate")
            .FirstOrDefault();

        if (newControl != null)
        {

            startDate = newControl.Text;

        }
    }
if(控件为TableRow)
{
var newControl=control.finddescents()
.其中(ctl=>ctl.ID==“开始日期”)
.FirstOrDefault();
if(newControl!=null)
{
startDate=newControl.Text;
}
}

我猜“StartDate”控件嵌套在另一个控件中,因此
.FindControl
没有看到它

控件.FindControl
的文档中:

此方法仅当控件被直接调用时才会找到该控件 装在指定容器内;也就是说,该方法没有 在控件中的控件层次结构中搜索

进一步:
有关在不知道控件的直接容器时如何查找该控件的信息,请参阅如何:。

我怀疑该控件是嵌套的,无法看到它。在这种情况下,您需要在pages控件集合中递归检查它,例如

private Control FindMyControl(Type type, ControlCollection c, string id)
{
     Control result = null;
     foreach (Control ctrl in c)
     {
         //checks top level of current control collection, if found breaks out and returns result
         if (ctrl.GetType() == type && ctrl.ID == id)
         {
             result = ctrl;
             break;
         }
         else//not found, search children of the current control until it is - if it's not found we will eventually return null
         {
             result = FindMyControl(type, ctrl.Controls, id);

             if (result != null)
             {
                 break;
             }
         }
     }

     return result;
 }
用法示例:-

Literal myLiteral = (Literal)FindMyControl(typeof(Literal), this.Controls, "control id here");

当你说“自动生成”时,你的意思是网页中的控件没有显示在designer中。cs?正确,我花了一个小时到处寻找解决方案,但什么都没用,这正是导致我手动查找根的原因。你能仔细阅读aspx文件代码吗?这会有帮助的。@HelloWorld如果这是你的问题,您的问题可能是名称空间/类名后面的代码与ascx/aspx页上的page属性不匹配所有匹配:)控件没有FindDescents方法我只是更新了代码以包含缺少的
this
关键字,以允许将代码作为扩展方法调用。我不太确定我是否理解了什么您已经在这里完成了,因为我实际上无法在用法中使用FindDescentAnds。这是一个扩展方法。您必须将该方法放在静态类中,并确保有
using
指令指向该类的名称空间。如果您不熟悉扩展方法,只需将该方法添加到当前类并调用
var newControl=finddescents(control).Where(ctl=>ctl.ID==“StartDate”).FirstOrDefault()。这是严格等价的
,其中
也是一种扩展方法。猜想您错过了使用System.Linq的
方向您的方法似乎是唯一一个运行良好的方法,但从我看来,它有点不稳定。可以做哪些改进?
Literal myLiteral = (Literal)FindMyControl(typeof(Literal), this.Controls, "control id here");