Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET find control by id

Tags:

c#

asp.net

I am doing a simple web site using asp.net and i am having troubles finding my or objects by id in the code behind in c#. I have something like this:

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

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

            </table>

        </LayoutTemplate>

the rest is irrelevant, and then i use this in the code behind:

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

i also tried:

Table table = (Table)camas;

and

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

and each one of this lines gives me Null. am i doing something wrong ?

EDIT: From your answers i did this:

<LayoutTemplate>

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


            </table>

        </LayoutTemplate>

and tried all the things stated above. same result.

EDIT2: Whole Code:

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>
like image 374
Pedro Costa Avatar asked Nov 01 '25 12:11

Pedro Costa


1 Answers

To expand on @Yura Zaletskyy's recursive find control method from MSDN -- you can also use the Page itself as the containing control to begin your recursive search for that elusive table control (which can be maddening, I know, I've been there.) Here is a bit more direction which may help.

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;
    }
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!