The code behind file seems to work very strange

Discussion in '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. Advertising

  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. Advertising

  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. 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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    524
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,729
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    474
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    548
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,384
Loading...

Share This Page