Monday, March 26, 2012

Working with controls inside of a Server table.

I have a control inside of an asp:Table control. Once I place it in
there, I can't seem to edit it any more (Double click to add events,
drag to reposition, etc...). If I use an HTML table, no problem. Is
there any way around this. I have tried grid layout as well as flow
layout. Sample of the code.
<asp:Table id="Table1" runat="server" Width="512px" Height="344px"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
GridLines="Both">
<asp:TableRow>
<asp:TableCell>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
</asp:Table>Scott,
You've discovered the Page's control hierarchy. I invite you to turn
Trace on to look at the control hierarchy for yourself. When you put server
controls on a page, the designer will automatically generate event handlers
for you. The textbox here is not a child of the page but a child of another
server control (Table), which is a child of the page. The table is in the
Page's Controls collection, but the textbox is in the Table's Controls
collection. To get to the TextBox, consider the following sample code:
//In Page_Load or Init
TextBox txt = (TextBox)tbl.FindControl("txt");
Trace.Write(txt.ID);
txt.TextChanged+=new EventHandler(txt_TextChanged);
private void txt_TextChanged(object sender, EventArgs e)
{
Trace.Warn("Text changed");
}
So since the textbox is inside another server control, it is not a direct
child of the Page, so the Page designer will not act on it.
Best regards,
Jeffrey Palermo
"Scott Kelley" <j_s_kelley@.yahoo.com> wrote in message
news:49898be2.0410220703.53a26b3d@.posting.google.com...
> I have a control inside of an asp:Table control. Once I place it in
> there, I can't seem to edit it any more (Double click to add events,
> drag to reposition, etc...). If I use an HTML table, no problem. Is
> there any way around this. I have tried grid layout as well as flow
> layout. Sample of the code.
> <asp:Table id="Table1" runat="server" Width="512px" Height="344px"
> BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
> GridLines="Both">
> <asp:TableRow>
> <asp:TableCell>
> <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
> </asp:TableCell>
> <asp:TableCell></asp:TableCell>
> </asp:TableRow>
> </asp:Table>
May I assume then that there is no way around this? Controls that are
contained in other controls are just not workable in Visual
Studio.NET. I remember working in VB 5 and 6 and even if a control
was contained in another control, there was still a way to work with
it (click to create functions, drag and position, etc...). Makes me
rethink designing things this way if I can't work with controls in the
GUI editor. Thanks for the info.
Scott.

> Scott,
> You've discovered the Page's control hierarchy. I invite you to turn
> Trace on to look at the control hierarchy for yourself. When you put serv
er
> controls on a page, the designer will automatically generate event handler
s
> for you. The textbox here is not a child of the page but a child of anoth
er
> server control (Table), which is a child of the page. The table is in the
> Page's Controls collection, but the textbox is in the Table's Controls
> collection. To get to the TextBox, consider the following sample code:
> //In Page_Load or Init
> TextBox txt = (TextBox)tbl.FindControl("txt");
> Trace.Write(txt.ID);
> txt.TextChanged+=new EventHandler(txt_TextChanged);
> private void txt_TextChanged(object sender, EventArgs e)
> {
> Trace.Warn("Text changed");
> }
> So since the textbox is inside another server control, it is not a direct
> child of the Page, so the Page designer will not act on it.
> Best regards,
> Jeffrey Palermo
>

0 comments:

Post a Comment