Panel Syntax

Discussion in 'ASP .Net Web Controls' started by TC, Jan 6, 2006.

  1. TC

    TC Guest

    In the below example of code,

    What is the syntax to access the contents of TextBox1?

    Also, what is the syntax to iterate all occurrences of the TextBoxes?

    Thanks,

    TC

    <%@ Page Language="C#" AutoEventWireup="True" %>
    <html>
    <head>

    <script runat="server">

    void Page_Load(Object sender, EventArgs e) {

    // Show or hide the Panel contents.

    if (Check1.Checked) {
    Panel1.Visible=false;
    }
    else {
    Panel1.Visible=true;
    }

    // Generate the Label controls.

    int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);

    for (int i=1; i<=numlabels; i++) {
    Label l = new Label();
    l.Text = "Label" + (i).ToString();
    l.ID = "Label" + (i).ToString();
    Panel1.Controls.Add(l);
    Panel1.Controls.Add(new LiteralControl("<br>"));
    }

    // Generate the Textbox controls.

    int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);

    for (int i=1; i<=numtexts; i++) {
    TextBox t = new TextBox();
    t.Text = "TextBox" + (i).ToString();
    t.ID = "TextBox" + (i).ToString();
    Panel1.Controls.Add(t);
    Panel1.Controls.Add(new LiteralControl("<br>"));
    }
    }

    </script>

    </head>
    <body>

    <h3>Panel Example</h3>

    <form runat=server>

    <asp:panel id="Panel1" runat="server"
    BackColor="gainsboro"
    Height="200px"
    Width="300px">

    Panel1: Here is some static content...
    <p>

    </asp:panel>

    <p>

    Generate Labels:
    <asp:DropDownList id=DropDown1 runat="server">
    <asp:ListItem Value="0">0</asp:ListItem>
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
    <asp:ListItem Value="4">4</asp:ListItem>
    </asp:DropDownList>

    <br>

    Generate TextBoxes:
    <asp:DropDownList id=DropDown2 runat="server">
    <asp:ListItem Value="0">0</asp:ListItem>
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
    <asp:ListItem Value="4">4</asp:ListItem>
    </asp:DropDownList>

    <p>
    <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>

    <p>
    <asp:Button Text="Refresh Panel" runat="server"/>


    </form>

    </body>
    </html>
     
    TC, Jan 6, 2006
    #1
    1. Advertising

  2. TextBox tb = (TextBox)Panel1.FindControl("TextBox1");

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "TC" wrote:

    >
    > In the below example of code,
    >
    > What is the syntax to access the contents of TextBox1?
    >
    > Also, what is the syntax to iterate all occurrences of the TextBoxes?
    >
    > Thanks,
    >
    > TC
    >
    > <%@ Page Language="C#" AutoEventWireup="True" %>
    > <html>
    > <head>
    >
    > <script runat="server">
    >
    > void Page_Load(Object sender, EventArgs e) {
    >
    > // Show or hide the Panel contents.
    >
    > if (Check1.Checked) {
    > Panel1.Visible=false;
    > }
    > else {
    > Panel1.Visible=true;
    > }
    >
    > // Generate the Label controls.
    >
    > int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);
    >
    > for (int i=1; i<=numlabels; i++) {
    > Label l = new Label();
    > l.Text = "Label" + (i).ToString();
    > l.ID = "Label" + (i).ToString();
    > Panel1.Controls.Add(l);
    > Panel1.Controls.Add(new LiteralControl("<br>"));
    > }
    >
    > // Generate the Textbox controls.
    >
    > int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
    >
    > for (int i=1; i<=numtexts; i++) {
    > TextBox t = new TextBox();
    > t.Text = "TextBox" + (i).ToString();
    > t.ID = "TextBox" + (i).ToString();
    > Panel1.Controls.Add(t);
    > Panel1.Controls.Add(new LiteralControl("<br>"));
    > }
    > }
    >
    > </script>
    >
    > </head>
    > <body>
    >
    > <h3>Panel Example</h3>
    >
    > <form runat=server>
    >
    > <asp:panel id="Panel1" runat="server"
    > BackColor="gainsboro"
    > Height="200px"
    > Width="300px">
    >
    > Panel1: Here is some static content...
    > <p>
    >
    > </asp:panel>
    >
    > <p>
    >
    > Generate Labels:
    > <asp:DropDownList id=DropDown1 runat="server">
    > <asp:ListItem Value="0">0</asp:ListItem>
    > <asp:ListItem Value="1">1</asp:ListItem>
    > <asp:ListItem Value="2">2</asp:ListItem>
    > <asp:ListItem Value="3">3</asp:ListItem>
    > <asp:ListItem Value="4">4</asp:ListItem>
    > </asp:DropDownList>
    >
    > <br>
    >
    > Generate TextBoxes:
    > <asp:DropDownList id=DropDown2 runat="server">
    > <asp:ListItem Value="0">0</asp:ListItem>
    > <asp:ListItem Value="1">1</asp:ListItem>
    > <asp:ListItem Value="2">2</asp:ListItem>
    > <asp:ListItem Value="3">3</asp:ListItem>
    > <asp:ListItem Value="4">4</asp:ListItem>
    > </asp:DropDownList>
    >
    > <p>
    > <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
    >
    > <p>
    > <asp:Button Text="Refresh Panel" runat="server"/>
    >
    >
    > </form>
    >
    > </body>
    > </html>
    >
     
    Phillip Williams, Jan 6, 2006
    #2
    1. Advertising

  3. To iterate all occurrences of the TextBoxes:

    foreach(Control cntrl in Panel1.Controls)
    {
    if (cntrl is TextBox)
    {
    TextBox myTextBox = (TextBox)cntrl;
    //process based on the textbox
    }
    }

    "Phillip Williams" wrote:

    > TextBox tb = (TextBox)Panel1.FindControl("TextBox1");
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "TC" wrote:
    >
    > >
    > > In the below example of code,
    > >
    > > What is the syntax to access the contents of TextBox1?
    > >
    > > Also, what is the syntax to iterate all occurrences of the TextBoxes?
    > >
    > > Thanks,
    > >
    > > TC
    > >
    > > <%@ Page Language="C#" AutoEventWireup="True" %>
    > > <html>
    > > <head>
    > >
    > > <script runat="server">
    > >
    > > void Page_Load(Object sender, EventArgs e) {
    > >
    > > // Show or hide the Panel contents.
    > >
    > > if (Check1.Checked) {
    > > Panel1.Visible=false;
    > > }
    > > else {
    > > Panel1.Visible=true;
    > > }
    > >
    > > // Generate the Label controls.
    > >
    > > int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);
    > >
    > > for (int i=1; i<=numlabels; i++) {
    > > Label l = new Label();
    > > l.Text = "Label" + (i).ToString();
    > > l.ID = "Label" + (i).ToString();
    > > Panel1.Controls.Add(l);
    > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > }
    > >
    > > // Generate the Textbox controls.
    > >
    > > int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
    > >
    > > for (int i=1; i<=numtexts; i++) {
    > > TextBox t = new TextBox();
    > > t.Text = "TextBox" + (i).ToString();
    > > t.ID = "TextBox" + (i).ToString();
    > > Panel1.Controls.Add(t);
    > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > }
    > > }
    > >
    > > </script>
    > >
    > > </head>
    > > <body>
    > >
    > > <h3>Panel Example</h3>
    > >
    > > <form runat=server>
    > >
    > > <asp:panel id="Panel1" runat="server"
    > > BackColor="gainsboro"
    > > Height="200px"
    > > Width="300px">
    > >
    > > Panel1: Here is some static content...
    > > <p>
    > >
    > > </asp:panel>
    > >
    > > <p>
    > >
    > > Generate Labels:
    > > <asp:DropDownList id=DropDown1 runat="server">
    > > <asp:ListItem Value="0">0</asp:ListItem>
    > > <asp:ListItem Value="1">1</asp:ListItem>
    > > <asp:ListItem Value="2">2</asp:ListItem>
    > > <asp:ListItem Value="3">3</asp:ListItem>
    > > <asp:ListItem Value="4">4</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > > <br>
    > >
    > > Generate TextBoxes:
    > > <asp:DropDownList id=DropDown2 runat="server">
    > > <asp:ListItem Value="0">0</asp:ListItem>
    > > <asp:ListItem Value="1">1</asp:ListItem>
    > > <asp:ListItem Value="2">2</asp:ListItem>
    > > <asp:ListItem Value="3">3</asp:ListItem>
    > > <asp:ListItem Value="4">4</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > > <p>
    > > <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
    > >
    > > <p>
    > > <asp:Button Text="Refresh Panel" runat="server"/>
    > >
    > >
    > > </form>
    > >
    > > </body>
    > > </html>
    > >
     
    Phillip Williams, Jan 6, 2006
    #3
  4. TC

    TC Guest

    Phillip,

    Thanks you.

    One question, If I created the panel on load pre-postback, and inputed
    content into the panel. Would I use the same syntax to access the contents
    after postback?

    I get the following message when I play with the Immediate Window (after
    postback):
    "function 'Panel1.FindControl' evaluated and returned null".

    Thanks again.
    TC

    "Phillip Williams" wrote:

    > To iterate all occurrences of the TextBoxes:
    >
    > foreach(Control cntrl in Panel1.Controls)
    > {
    > if (cntrl is TextBox)
    > {
    > TextBox myTextBox = (TextBox)cntrl;
    > //process based on the textbox
    > }
    > }
    >
    > "Phillip Williams" wrote:
    >
    > > TextBox tb = (TextBox)Panel1.FindControl("TextBox1");
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "TC" wrote:
    > >
    > > >
    > > > In the below example of code,
    > > >
    > > > What is the syntax to access the contents of TextBox1?
    > > >
    > > > Also, what is the syntax to iterate all occurrences of the TextBoxes?
    > > >
    > > > Thanks,
    > > >
    > > > TC
    > > >
    > > > <%@ Page Language="C#" AutoEventWireup="True" %>
    > > > <html>
    > > > <head>
    > > >
    > > > <script runat="server">
    > > >
    > > > void Page_Load(Object sender, EventArgs e) {
    > > >
    > > > // Show or hide the Panel contents.
    > > >
    > > > if (Check1.Checked) {
    > > > Panel1.Visible=false;
    > > > }
    > > > else {
    > > > Panel1.Visible=true;
    > > > }
    > > >
    > > > // Generate the Label controls.
    > > >
    > > > int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);
    > > >
    > > > for (int i=1; i<=numlabels; i++) {
    > > > Label l = new Label();
    > > > l.Text = "Label" + (i).ToString();
    > > > l.ID = "Label" + (i).ToString();
    > > > Panel1.Controls.Add(l);
    > > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > > }
    > > >
    > > > // Generate the Textbox controls.
    > > >
    > > > int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
    > > >
    > > > for (int i=1; i<=numtexts; i++) {
    > > > TextBox t = new TextBox();
    > > > t.Text = "TextBox" + (i).ToString();
    > > > t.ID = "TextBox" + (i).ToString();
    > > > Panel1.Controls.Add(t);
    > > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > > }
    > > > }
    > > >
    > > > </script>
    > > >
    > > > </head>
    > > > <body>
    > > >
    > > > <h3>Panel Example</h3>
    > > >
    > > > <form runat=server>
    > > >
    > > > <asp:panel id="Panel1" runat="server"
    > > > BackColor="gainsboro"
    > > > Height="200px"
    > > > Width="300px">
    > > >
    > > > Panel1: Here is some static content...
    > > > <p>
    > > >
    > > > </asp:panel>
    > > >
    > > > <p>
    > > >
    > > > Generate Labels:
    > > > <asp:DropDownList id=DropDown1 runat="server">
    > > > <asp:ListItem Value="0">0</asp:ListItem>
    > > > <asp:ListItem Value="1">1</asp:ListItem>
    > > > <asp:ListItem Value="2">2</asp:ListItem>
    > > > <asp:ListItem Value="3">3</asp:ListItem>
    > > > <asp:ListItem Value="4">4</asp:ListItem>
    > > > </asp:DropDownList>
    > > >
    > > > <br>
    > > >
    > > > Generate TextBoxes:
    > > > <asp:DropDownList id=DropDown2 runat="server">
    > > > <asp:ListItem Value="0">0</asp:ListItem>
    > > > <asp:ListItem Value="1">1</asp:ListItem>
    > > > <asp:ListItem Value="2">2</asp:ListItem>
    > > > <asp:ListItem Value="3">3</asp:ListItem>
    > > > <asp:ListItem Value="4">4</asp:ListItem>
    > > > </asp:DropDownList>
    > > >
    > > > <p>
    > > > <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
    > > >
    > > > <p>
    > > > <asp:Button Text="Refresh Panel" runat="server"/>
    > > >
    > > >
    > > > </form>
    > > >
    > > > </body>
    > > > </html>
    > > >
     
    TC, Jan 6, 2006
    #4
  5. TC

    TC Guest

    I found my answer. Because of managed code I should not be playing in the
    postback world. Instead I should place my code into an event (like a button
    click).


    Thanks

    "TC" wrote:

    > Phillip,
    >
    > Thank you.
    >
    > One question, If I created the panel on load pre-postback, and inputed
    > content into the panel. Would I use the same syntax to access the contents
    > after postback?
    >
    > I get the following message when I play with the Immediate Window (after
    > postback):
    > "function 'Panel1.FindControl' evaluated and returned null".
    >
    > Thanks again.
    > TC
    >
    > "Phillip Williams" wrote:
    >
    > > To iterate all occurrences of the TextBoxes:
    > >
    > > foreach(Control cntrl in Panel1.Controls)
    > > {
    > > if (cntrl is TextBox)
    > > {
    > > TextBox myTextBox = (TextBox)cntrl;
    > > //process based on the textbox
    > > }
    > > }
    > >
    > > "Phillip Williams" wrote:
    > >
    > > > TextBox tb = (TextBox)Panel1.FindControl("TextBox1");
    > > >
    > > > --
    > > > HTH,
    > > > Phillip Williams
    > > > http://www.societopia.net
    > > > http://www.webswapp.com
    > > >
    > > >
    > > > "TC" wrote:
    > > >
    > > > >
    > > > > In the below example of code,
    > > > >
    > > > > What is the syntax to access the contents of TextBox1?
    > > > >
    > > > > Also, what is the syntax to iterate all occurrences of the TextBoxes?
    > > > >
    > > > > Thanks,
    > > > >
    > > > > TC
    > > > >
    > > > > <%@ Page Language="C#" AutoEventWireup="True" %>
    > > > > <html>
    > > > > <head>
    > > > >
    > > > > <script runat="server">
    > > > >
    > > > > void Page_Load(Object sender, EventArgs e) {
    > > > >
    > > > > // Show or hide the Panel contents.
    > > > >
    > > > > if (Check1.Checked) {
    > > > > Panel1.Visible=false;
    > > > > }
    > > > > else {
    > > > > Panel1.Visible=true;
    > > > > }
    > > > >
    > > > > // Generate the Label controls.
    > > > >
    > > > > int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);
    > > > >
    > > > > for (int i=1; i<=numlabels; i++) {
    > > > > Label l = new Label();
    > > > > l.Text = "Label" + (i).ToString();
    > > > > l.ID = "Label" + (i).ToString();
    > > > > Panel1.Controls.Add(l);
    > > > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > > > }
    > > > >
    > > > > // Generate the Textbox controls.
    > > > >
    > > > > int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
    > > > >
    > > > > for (int i=1; i<=numtexts; i++) {
    > > > > TextBox t = new TextBox();
    > > > > t.Text = "TextBox" + (i).ToString();
    > > > > t.ID = "TextBox" + (i).ToString();
    > > > > Panel1.Controls.Add(t);
    > > > > Panel1.Controls.Add(new LiteralControl("<br>"));
    > > > > }
    > > > > }
    > > > >
    > > > > </script>
    > > > >
    > > > > </head>
    > > > > <body>
    > > > >
    > > > > <h3>Panel Example</h3>
    > > > >
    > > > > <form runat=server>
    > > > >
    > > > > <asp:panel id="Panel1" runat="server"
    > > > > BackColor="gainsboro"
    > > > > Height="200px"
    > > > > Width="300px">
    > > > >
    > > > > Panel1: Here is some static content...
    > > > > <p>
    > > > >
    > > > > </asp:panel>
    > > > >
    > > > > <p>
    > > > >
    > > > > Generate Labels:
    > > > > <asp:DropDownList id=DropDown1 runat="server">
    > > > > <asp:ListItem Value="0">0</asp:ListItem>
    > > > > <asp:ListItem Value="1">1</asp:ListItem>
    > > > > <asp:ListItem Value="2">2</asp:ListItem>
    > > > > <asp:ListItem Value="3">3</asp:ListItem>
    > > > > <asp:ListItem Value="4">4</asp:ListItem>
    > > > > </asp:DropDownList>
    > > > >
    > > > > <br>
    > > > >
    > > > > Generate TextBoxes:
    > > > > <asp:DropDownList id=DropDown2 runat="server">
    > > > > <asp:ListItem Value="0">0</asp:ListItem>
    > > > > <asp:ListItem Value="1">1</asp:ListItem>
    > > > > <asp:ListItem Value="2">2</asp:ListItem>
    > > > > <asp:ListItem Value="3">3</asp:ListItem>
    > > > > <asp:ListItem Value="4">4</asp:ListItem>
    > > > > </asp:DropDownList>
    > > > >
    > > > > <p>
    > > > > <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
    > > > >
    > > > > <p>
    > > > > <asp:Button Text="Refresh Panel" runat="server"/>
    > > > >
    > > > >
    > > > > </form>
    > > > >
    > > > > </body>
    > > > > </html>
    > > > >
     
    TC, Jan 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harry
    Replies:
    3
    Views:
    562
    Harry Singh
    Nov 10, 2003
  2. HS1
    Replies:
    3
    Views:
    508
  3. HS1
    Replies:
    4
    Views:
    8,346
    Babu Kalakrishnan
    Dec 8, 2004
  4. JC
    Replies:
    1
    Views:
    761
    Michael Rauscher
    Aug 9, 2006
  5. Ulf Meinhardt
    Replies:
    0
    Views:
    638
    Ulf Meinhardt
    Jul 7, 2009
Loading...

Share This Page