FindControl -ich flipp noch aus

Kropotkin

Mitglied
Hallo.

Jetzt könnt ich ausflippen. Ich mach ein ganz einfache Sach
Ich will einem control im code ein control aus der site zuweisen.
Das wollte ich mit findControl machen. Aber das bringtnull.

Code:
<tr>
            <td style="width: 48px">
                08.30-09.15</td>
            <td style="width: 136px">
                <asp:TextBox ID="tbl1" runat="server" Height="16px" Width="61px" AutoPostBack="True"></asp:TextBox>
                <asp:ImageButton ID="img1" runat="server" Height="16px" 
                    ImageUrl="~/App_Themes/Icons/icons/Edit.png" Width="16px" 
                    onclick="imgb_Click" />
                <asp:DropDownList ID="ddll1" runat="server" Height="16px" Width="62px" 
                    Visible="False">
                </asp:DropDownList>
            </td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>

jetzt der 'Code'

Code:
TextBox  myControl1 = FindControl("tbl1") as TextBox ; 
        if (myControl1 != null)
        {
            Control myControl2 = myControl1.Parent;
            Response.Write("Parent of the text box is : " + myControl2.ID);
        }
        else
        {
            Response.Write("Control not found");
        }

Ich weiss dass die Textbox da ist. Wieso findet der Code sie nicht?

ciao und danke
 
Wow! so viele Antworten :D

Aber ich hab die Antwort inzwischen selbst gefunden:
Und ich will sie Euch nicht vorenthalten, damit nicht jeder drüber stolpert.
wenn man die site in einer Masterpage hat (war bei mir der Fall) muss ich auch erst über
das ContentPlaceHolder-Control gehen. Dh. ich muss das findcontrol im master-control stattfinden lassen


in meinem Fall heisst das also:
Code:
ContentPlaceHolder c = Master.FindControl("MainContent") as ContentPlaceHolder;
 TextBox tbf = c.FindControl(sTbf) as TextBox;

ciao
 
Zurück