The code behind file seems to work very strange

Discussion in 'Microsoft ASP .NET' started by Tony Johansson, Dec 15, 2010.

  1. Hello!

    I have added a table to the default page using the design mode. In the table
    I have added some labels, textBoxes,dropdownlistbox and a button using the
    design mode.
    When I run this page I can see the table in the browser so all seems to work
    fine.

    But if I add this statement form1.InnerText = "This is a test";
    to the Page_Load statement then the table with the including controls will
    not be displayed in the browser.
    In the browser will only this text be displayed This is a Test

    So my question is how can this row form1.InnerText = "This is a test";
    in the Page_Load cause the the table with all the control not to be
    displayed ??


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
    Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td style="width: 100px">
    <asp:Label ID="labelEvent" runat="server"
    Text="Event:"></asp:Label>
    </td>
    <td style="width:100px">
    <asp:DropDownList ID="dropDownListEvents" runat="server">
    <asp:ListItem>SQL Server 2005 and XML</asp:ListItem>
    <asp:ListItem>Office 2003 and XML</asp:ListItem>
    <asp:ListItem>Introduction to ASP.NET</asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    <asp:Label ID="labelFirstname" runat="server"
    Text="Firstname:"></asp:Label>
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="textFirstname" runat="server"
    Height="16px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    <asp:Label ID="labelLastname" runat="server"
    Text="Lastname:"></asp:Label>
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="textLastname" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    <asp:Label ID="labelEmail" runat="server"
    Text="Email:"></asp:Label>
    </td>
    <td style="width: 100px">
    <asp:TextBox ID="textEmail" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="width: 100px">
    </td>
    <td style="width: 100px">
    <asp:Button ID="buttonSubmit" runat="server" Text="Submit"
    />
    </td>
    </tr>
    </table>

    </div>
    </form>
    </body>
    </html>

    //Code behind file
    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    form1.InnerText = "Detta är ett test";
    }
    }
     
    Tony Johansson, Dec 15, 2010
    #1
    1. Advertisements

  2. Hi,

    What you are doing is your are replacing the whole of HTML content i.e. the controls on the page with the string "This is a test".
    form1.InnerText basically represents the entire HTML content in the form and hence you see the issue.

    Just to clarify, why would you want to do it?

    Regards,
    Vaibhav

    Submitted via EggHeadCafe
    HTML5 And the Case of the Missing Browsers
    http://www.eggheadcafe.com/tutorial...ml5-and-the-case-of-the-missing-browsers.aspx
     
    Vaibhav Gupta, Dec 16, 2010
    #2
    1. Advertisements

  3. Tony Johansson

    Brian Cryer Guest

    "Tony Johansson" <> wrote in message
    news:ieaujq$nme$-september.org...
    > Hello!
    >
    > I have added a table to the default page using the design mode. In the
    > table I have added some labels, textBoxes,dropdownlistbox and a button
    > using the design mode.
    > When I run this page I can see the table in the browser so all seems to
    > work fine.
    >
    > But if I add this statement form1.InnerText = "This is a test";
    > to the Page_Load statement then the table with the including controls will
    > not be displayed in the browser.
    > In the browser will only this text be displayed This is a Test
    >
    > So my question is how can this row form1.InnerText = "This is a test";
    > in the Page_Load cause the the table with all the control not to be
    > displayed ??


    It sounds like y ou don't understand what assigning to InnerText is doing.

    Everything you are putting on your page is inside form1. Assigning to
    InnerText replaces all of the content with the supplied test. So:

    form1.InnerText = "This is a test";

    is saying replace EVERYTHING inside form1 with the text "This is a test". So
    it replaces everything (which includes your table) with the given text. So
    the table isn't displayed because its no longer there, you've assigned new
    content to the form.

    If you want to display a message I suggest you use a label.

    Hope this helps.
    --
    Brian Cryer
    http://www.cryer.co.uk/brian
     
    Brian Cryer, Dec 16, 2010
    #3
    1. Advertisements

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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: Microsoft ASP .NET
    Replies:
    1
    Views:
    654
    Alphonse Giambrone
    Jun 28, 2003
  2. Shawn

    Very very strange. Major bug?

    Shawn, Feb 3, 2004, in forum: Microsoft ASP .NET
    Replies:
    4
    Views:
    173
    Shawn
    Feb 4, 2004
  3. Daniel Manes

    Code-Behind Pain in the Behind!

    Daniel Manes, Jun 10, 2005, in forum: Microsoft ASP .NET
    Replies:
    11
    Views:
    427
  4. keithb
    Replies:
    1
    Views:
    602
    Bruce Barker
    Mar 29, 2006
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: Microsoft ASP .NET
    Replies:
    2
    Views:
    326
    Guest
    Feb 19, 2007
Loading...

Share This Page