May 29, 2011

How to Create an TextboxAutoPostBack in ASP.NET

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        lblSearchResults.Text = "Search for: " + txtSearch.Text;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>TextBox AutoPostBack</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    <asp:Label id="lblSearch" Text="Search:" Runat="server" />
    <asp:TextBox id="txtSearch" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" Runat="server" />
    <br />
    <hr />
    <asp:Label id="lblSearchResults" Runat="server" />    
    </div>
    </form>
</body>
</html>

Output:

How to Create an AutoComplete Textbox Control in ASP.NET

<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Show AutoComplete</title>
</head>
<body>
<form id="form1" runat="server">
<div>    
<asp:Label id="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName" Runat="server" />
<br />
<asp:TextBox id="txtFirstName" AutoCompleteType="FirstName" Runat="server" />    
<br /><br />    
<asp:Label id="lblLastname" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" />
<br />
<asp:TextBox id="txtLastName" AutoCompleteType="LastName" Runat="server" />    
<br /><br />
<asp:Button id="btnSubmit" Text="Submit" Runat="server" />    
</div>
</form>
</body>
</html>
Output:



May 22, 2011

How to Enable or Disable ViewState Property in ASP.NET

ViewState:
ViewState allows the state of objects to be stored in a hidden field on the page. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source. ViewState is used to retain the state of server-side objects between post backs.


Enable ViewState Property:
It allows the page to save the users input on a form across postbacks. It saves the server-side values for a given control into ViewState, which is stored as a hidden value on the page before sending the page to the clients browser


Show ViewState:



<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">    

    protected void btnAdd_Click(object sender, EventArgs e)

    {

        lblCounter.Text = (Int32.Parse(lblCounter.Text) + 1).ToString();

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Show View State</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>    

    <asp:Button id="btnAdd" Text="Add" OnClick="btnAdd_Click" Runat="server" />    

    <asp:Label id="lblCounter" Text="0" Runat="server" />    

    </div>

    </form>

</body>

</html>


Output:




How to Create a First Asp.net web page using CodeBehind


ASPX Page:

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

<!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 id="Head1" runat="server">

    <title>First Page Code-Behind</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>    

    <asp:Button id="Button1" Text="Click Here" OnClick="Button1_Click" Runat="server" /> <br /><br />        

    <asp:Label id="Label1" Runat="server" />    

    </div>

    </form>

</body>

</html>
Code Behind Page:
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;


public partial class FirstPageCodeBehind : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        Label1.Text = "Click the Button";

    }
  

    protected void Button1_Click(object sender, EventArgs e)

    {

        Label1.Text = "Welcome ASP.NET Example..!";

    }

}

Output: