ASP.NET通过id查找控件

时间:2015-02-04 17:05:11

标签: c# asp.net

我正在使用asp.net做一个简单的网站,我在c#后面的代码中通过id查找我的或对象时遇到了麻烦。我有这样的事情:

<asp:ListView ID="InternamentosListView" runat="server"
            DataSourceID="InternamentosBD">
        <LayoutTemplate>
            <table id="camas">

                    <asp:PlaceHolder runat="server" ID="TablePlaceHolder"></asp:PlaceHolder>

            </table>

        </LayoutTemplate>

其余部分无关紧要,然后我在后面的代码中使用它:

Table table = (Table)FindControl("camas");

我也试过了:

Table table = (Table)camas;

Control table= (Table)FindControl("camas");

这一行中的每一行都给我Null。我做错了什么?

编辑:根据您的回答,我这样做了:

<LayoutTemplate>

            <table id="camas" runat="server">


            </table>

        </LayoutTemplate>

并尝试了上述所有事情。同样的结果。

EDIT2:整个代码:

C#

 protected void Page_Load(object sender, EventArgs e)
    {

        Table table = (Table)FindControl("camas");
        HiddenField NCamasHF = (HiddenField)FindControl("NCamas");
        int NCamas = Convert.ToInt32(NCamasHF);
        HiddenField NColunasHF = (HiddenField)FindControl("NColunas");
        HiddenField CorMasc = (HiddenField)FindControl("CorMasc");
        HiddenField CorFem = (HiddenField)FindControl("CorFem");
        int NColunas = Convert.ToInt32(NColunasHF);
        TableRow Firstrow = new TableRow();
        table.Rows.Add(Firstrow);
        for (int i = 1; i <= NCamas; i++)
        {
            if (i % NColunas == 0)
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);
                TableCell cell = new TableCell();
                cell.BackColor = System.Drawing.Color.LightGreen;
                cell.CssClass = "celula";
                row.Cells.Add(cell);
            }
            else
            {
                TableCell cell = new TableCell();
                cell.BackColor = System.Drawing.Color.LightGreen;
                cell.CssClass = "celula";
                Firstrow.Cells.Add(cell);
            }
        }
    }

asp.net

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

     <asp:SqlDataSource
        ID="ParametrosBD"
        ConnectionString ="<%$ ConnectionStrings:principal %>"
        ProviderName = "System.Data.SqlClient"
        SelectCommand = "SELECT par_Id, par_NCamas, par_NColunas, par_CorMasculino, par_CorFeminino FROM Parametros WHERE par_Id=1"
        runat="server">
       </asp:SqlDataSource>
    <asp:ListView ID="InternamentosListView" runat="server"
            DataSourceID="InternamentosBD">
        <LayoutTemplate>

            <table id="camas" runat="server">


            </table>

        </LayoutTemplate>
        <ItemTemplate>
            <asp:HiddenField ID="NCamas" runat="server" Value='<%# Bind("par_NCamas") %>' />
            <asp:HiddenField ID="NColunas" runat="server" Value='<%# Bind("par_NColunas") %>' />
            <asp:HiddenField ID="CorMasc" runat="server" Value='<%# Bind("par_CorMasculino") %>' />
            <asp:HiddenField ID="CorFem" runat="server" Value='<%# Bind("par_CorFeminino") %>' />
            <tr id="cama"></tr>
            </ItemTemplate>
    </asp:ListView>

</asp:Content>

7 个答案:

答案 0 :(得分:3)

正如您已经添加了runat =“server”的步骤1.第二步是按ID查找控件,但您需要递归执行。例如,考虑以下功能:

private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

答案 1 :(得分:3)

要扩展来自MSDN的@Yura Zaletskyy的递归查找控制方法 - 您还可以使用Page本身作为包含控件来开始递归搜索该难以捉摸的表控件(这可能令人抓狂,我知道,我去过那里。)这里有一点方向可能会有所帮助。

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table table = (Table)FindControlRecursive(Page, "camas");
        if (table != null)
        {
            //Do awesome things.
        }
    }

    private Control FindControlRecursive(Control rootControl, string controlID)
    {
        if (rootControl.ID == controlID) return rootControl;

        foreach (Control controlToSearch in rootControl.Controls)
        {
            Control controlToReturn = FindControlRecursive(controlToSearch, controlID);
            if (controlToReturn != null) return controlToReturn;
        }
        return null;
    }
}

答案 2 :(得分:0)

您需要runat="server"才能在代码隐藏中看到它。

e.g。

<table id="camas" runat="server">

答案 3 :(得分:0)

确定。您在列表视图模板中拥有该表。您永远不会直接在服务器端找到控件。 您需要做的是在list.Items数组或项目数据绑定事件中查找控件。

看看这个 - how to find control in ItemTemplate of the ListView from code behind of the usercontrol page?

答案 4 :(得分:0)

您需要像之前所述添加runat服务器,但ID将根据ListView行进行更改。你可以添加clientidmode =&#34; Static&#34;如果你只想要1个表并找到使用InternamentosListView.FindControl(&#34; camas&#34;);否则,您将需要添加ItemDataBound事件。

<asp:ListView ID="InternamentosListView" runat="server"         DataSourceID="InternamentosBD" OnItemDataBound="InternamentosListView_ItemDataBound">
        <LayoutTemplate>
            <table id="camas">
<asp:PlaceHolder runat="server" ID="TablePlaceHolder"></asp:PlaceHolder>
</table>
</LayoutTemplate>

您需要在

后面引入代码
InternamentosListView_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Table tdcamas = (Table)e.Item.FindControl("camas");
            }
}

答案 5 :(得分:0)

现有答案中没有一个提到明显的内置解决方案FindControl。

Detailed here,FindControl是一个函数,该函数将作为调用Control的子级的控件的id作为字符串,如果存在则返回它,否则返回null。

很棒的事情是,您始终可以从Page控件中调用它,该控件将浏览页面中的所有控件。参见下面的示例。

var myTextboxControl = (TextBox)Page.FindControl("myTextboxControlID);
if (myTextboxControl != null) {
    myTextboxControl.Text = "Something";
}

请注意,返回的对象是通用控件,因此,在使用上面的.Text之类的特定功能之前,您需要对其进行强制转换。

答案 6 :(得分:-1)

当您使用母版页时,此代码应该适合您

请注意,您正在寻找htmlTable,而不是asp:Table

//添加名称空间

using System.Web.UI.HtmlControls;

//将占位符的名称更改为您正在使用

的名称
HtmlTable tbl = this.Master.FindControl("ContentPlaceHolder1").FindControl("camas") as HtmlTable;

最佳, 尼

相关问题