Saturday, January 13, 2007

How to detect and control Browser close event

How to Control browser close event

It is too easy to control .But we can't use the server side script because this event happen in client side.I will explain with the javascript.Now,create the one web form and add one datagrid control .In the datagrid column add one check box and one text box.

And then binding the data in code behind. like this -

private void Page_Load(object sender, System.EventArgs e)
{
//for retrieve the data from database
string connStr="Data Source= DEV-Kavan; Initial Catalog= Test;
user
id=sa;pwd=velkiri";
SqlConnection conn=new SqlConnection(connStr);
SqlDataAdapter ad=new SqlDataAdapter("Select * from SUser",conn);
DataSet ds=new DataSet();
ad.Fill(ds,"0");

//bind the data
DataGrid1.DataSource=ds;
DataGrid1.DataMember="0";
DataGrid1.DataBind();
}

this is really simple.Now let go to aspx .write the javascript method

function testfun() {
var count=2;
var str="";
while(document.getElementById("DataGrid1").hasChildNodes()){
if(document.getElementById("DataGrid1__ctl"+count+"_CheckBox1").checked==true)
{ str=str+document.getElementById("DataGrid1__ctl"+count+"_TextBox1").value+",";
alert(str);
}
count=count+1;
}
alert("final"+str);
opener.document.getElementByID("b").value="aaa";
opener.document.forms(0).submit();
}



Get the datagrid object through
getElementById method.when we look in the datagrid is table.So i use the hasChildNodes() method and check it have any child root.
And then I check our check box is checkup or not.Now add this two statement.
opener.document.getElementByID("b").value="aaa";
opener.document.forms(0).submit();


Get my sample code -
aspx file
cs file

No comments:

Google Ad