only My site

Thursday, July 17, 2008

ASP.Net - DoPostBack

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

<!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>

<script language="javascript" type="text/javascript">

function enablePostBack()

{

alert("Hi");

//TextBox1 is the first argument(name of control) second argument is "TextChanged" (Serverside event name)

__doPostBack("TextBox1","TextChanged");

}

</script>

</head>

<body>

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

<div>

<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

</div>

</form>

</body>

</html>


Server side Code

using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class DoPostback : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.GetPostBackEventReference(TextBox1);

if ( !IsPostBack )
{
TextBox1.Attributes.Add("onblur", "Javascript:enablePostBack();");
}
}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox2.Text = (Int16.Parse(TextBox1.Text) * 45).ToString();
}
}

No comments: