在ASP.NET中,如何找到嵌套在DetailsView中的TextBox的控件ID,然后嵌套在AJAX UpdatePanel控件中?

时间:2022-08-25 17:25:56

n ASP.NET how do I find the Control ID of a TextBox that is nested within a DetailsView which is then nested in an AJAX UpdatePanel control ?

在ASP.NET中如何找到嵌套在DetailsView中的TextBox的控件ID,然后嵌套在AJAX UpdatePanel控件中?

The heirachy is: UpdatePanel1 -> dvContentDetail (DetailsView Control) -> TextBox2

heirachy是:UpdatePanel1 - > dvContentDetail(DetailsView控件) - > TextBox2

I have tried something like the following but just says that the object isn't found:

我尝试了类似下面的内容,但只是说找不到对象:

UpdatePanel1.FindControl("dvContentDetail").FindControl("TextBox2").ClientID

2 个解决方案

#1


There is no need to find control from updatepanel, because these controls directly available, so you code will be like this...

没有必要从updatepanel找到控件,因为这些控件直接可用,所以你的代码就像这样......

TextBox TextBox2 = (TextBox)dvContentDetail.FindControl("TextBox2");

#2


You could try something like the code below. But if you know the Hierarchy is not going to change it would be better to do a series of "FindControl" calls. To discover the correct hierarchy, Debug the app and search through the Control Hierarchy.

您可以尝试类似下面的代码。但是如果您知道Hierarchy不会改变,那么最好进行一系列“FindControl”调用。要发现正确的层次结构,请调试应用程序并搜索“控制层次结构”。

public static T FindControlRecursiveInternal<T>(Control startingControl, string controlToFindID) where T : Control
{
    if (startingControl == null || String.IsNullOrEmpty(controlToFindID))
        return (T)null;

    Control foundControl = startingControl.FindControl(controlToFindID);
    if (foundControl == null)
    {
        foreach (Control innerControl in startingControl.Controls)
        {
            foundControl = FindControlRecursiveInternal<T>(innerControl, controlToFindID);
            if (foundControl != null)
                break;
        }
    }

    return (T)foundControl;
}

#1


There is no need to find control from updatepanel, because these controls directly available, so you code will be like this...

没有必要从updatepanel找到控件,因为这些控件直接可用,所以你的代码就像这样......

TextBox TextBox2 = (TextBox)dvContentDetail.FindControl("TextBox2");

#2


You could try something like the code below. But if you know the Hierarchy is not going to change it would be better to do a series of "FindControl" calls. To discover the correct hierarchy, Debug the app and search through the Control Hierarchy.

您可以尝试类似下面的代码。但是如果您知道Hierarchy不会改变,那么最好进行一系列“FindControl”调用。要发现正确的层次结构,请调试应用程序并搜索“控制层次结构”。

public static T FindControlRecursiveInternal<T>(Control startingControl, string controlToFindID) where T : Control
{
    if (startingControl == null || String.IsNullOrEmpty(controlToFindID))
        return (T)null;

    Control foundControl = startingControl.FindControl(controlToFindID);
    if (foundControl == null)
    {
        foreach (Control innerControl in startingControl.Controls)
        {
            foundControl = FindControlRecursiveInternal<T>(innerControl, controlToFindID);
            if (foundControl != null)
                break;
        }
    }

    return (T)foundControl;
}