Author Topic: ASP.NET Textbox help  (Read 3158 times)

0 Members and 1 Guest are viewing this topic.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
ASP.NET Textbox help
« on: January 07, 2012, 03:24:33 pm »
So I've been fooling around with ASP.NET and I have what would seem to work, but it doesn't... I was just wondering if anyone who knew ASP.NET could help.

Spoiler For Site.master:
<body>
    <form runat="server">
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>

            <asp:TextBox runat="server" ID="txt" />
            <asp:Label runat="server" ID="lbl" />

        </div>
        <div class="clear">
        </div>
    <div class="footer">
       
    </div>
    </form>
</body>

Spoiler For Site.master.cs:
public partial class SiteMaster : System.Web.UI.MasterPage {

    protected void Page_Load(object sender, EventArgs e) {
        txt.AutoPostBack = true;
        txt.TextChanged += new EventHandler(textChanged);
    }

    public void textChanged(object sender, EventArgs e) {
        lbl.Text = Server.HtmlEncode(txt.Text);
    }
}

EDIT: I've modified the source. It works only when the textbox loses focus, though. Also, if there are any HTML-specific characters (such as '>' and '<') there's an error thrown. I thought Server.HtmlEncode() was supposed to take care of that?
« Last Edit: January 07, 2012, 03:35:12 pm by BlakPilar »