Code Behind File Function ASP.NET 2.0 Page Load

A

ABHIJIT B

Hi All,

I am calling Code behind file function in one of JavaScript method as
given below,

I don't want to call Code behind file function during Page Load.

Kindly help me out for same.

I tried to call btnDelete.Attributes.Add("onclick",
"javascript:vDelete();");
in if(IsPostBack) and if(!IsPostBack).Still Code Behind function is
called.
---------------------------------------------------------------------------------------------------------------
ASPX Code :

function vDelete()
{
debugger;
window.alert("Hi");

var loginid = document.getElementById('<
%=txtCFirstName.ClientID%>').value;
window.alert(loginid);

if(loginid == '')
{
window.alert("Hello");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"N";
return false;
}
else
{
var ret = window.confirm('Are you sure you want to delete this
record?');

window.alert(ret);

document.getElementById('<%=lblMUSaveError.ClientID
%>').innerHTML = "";

if(ret == true)
{
window.alert("Fine");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"Y";
<%DeleteUser();%>;
return true;
}
else
{
window.alert("Hello");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"N";
return false;
}
}
}
---------------------------------------------------------------------------------------------------------------------------
Code Behind File :

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//code
btnDelete.Attributes.Add("onclick",
"javascript:vDelete();");
}

}


protected void DeleteUser()
{
try
{
if (hidSave.Value.ToString() == "0")
gvUsersList.Rows[0].Cells.Clear(); //GridView
}
catch(Exception ex)
{
//Loggin error in txt file
}
}
 
B

bruce barker

when you attach code to the onclick, the browser generates an anonymous
fuinction:

<input type="submit" onclick="doSomething();">

the browser generates the following code

domObj.click = function() { doSomething(); }

you can see the anonymous function does not return any value, so no matter
what you function returns it has no impact. the following code:

<input type="submit" onclick="return doSomething();">

the browser generates the following code:

domObj.click = function() { return doSomething();}

which will cancel the postback if doSomething returns false.


-- bruce (sqlwork.com)
 
A

ABHIJIT B

Thanks Bruce.

My problem is I am having one TextBox and Delete Button .
I have to check if Textbox.text is empty then display error message if
not then give prompt to user
"Are you sure u want to delete user".If user clicks OK then call Code
Behind function else not.



when you attach code to the onclick, the browser generates an anonymous
fuinction:

<input type="submit" onclick="doSomething();">

the browser generates the following code

domObj.click = function() { doSomething(); }

you can see the anonymous function does not return any value, so no matter
what you function returns it has no impact. the following code:

<input type="submit" onclick="return doSomething();">

the browser generates the following code:

domObj.click = function() { return doSomething();}

which will cancel the postback if doSomething returns false.

-- bruce (sqlwork.com)

ABHIJIT B said:
I am calling Code behind file function in one of JavaScript method as
given below,
I don't want to call Code behind file function during Page Load.
Kindly help me out for same.
I tried to call btnDelete.Attributes.Add("onclick",
"javascript:vDelete();");
in if(IsPostBack) and if(!IsPostBack).Still Code Behind function is
called.
function vDelete()
{
debugger;
window.alert("Hi");
var loginid = document.getElementById('<
%=txtCFirstName.ClientID%>').value;
window.alert(loginid);
if(loginid == '')
{
window.alert("Hello");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"N";
return false;
}
else
{
var ret = window.confirm('Are you sure you want to delete this
record?');

document.getElementById('<%=lblMUSaveError.ClientID
%>').innerHTML = "";
if(ret == true)
{
window.alert("Fine");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"Y";
<%DeleteUser();%>;
return true;
}
else
{
window.alert("Hello");
document.getElementById('<%=hidUserDel.ClientID%>').value =
"N";
return false;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//code
btnDelete.Attributes.Add("onclick",
"javascript:vDelete();");
}

protected void DeleteUser()
{
try
{
if (hidSave.Value.ToString() == "0")
gvUsersList.Rows[0].Cells.Clear(); //GridView
}
catch(Exception ex)
{
//Loggin error in txt file
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top