PC Review


Reply
Thread Tools Rating: Thread Rating: 3 votes, 1.00 average.

Code Behind File Function ASP.NET 2.0 Page Load

 
 
ABHIJIT B
Guest
Posts: n/a
 
      7th Mar 2008
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
}
}
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      7th Mar 2008
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" wrote:

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

 
Reply With Quote
 
ABHIJIT B
Guest
Posts: n/a
 
      7th Mar 2008
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.



On Mar 7, 4:19 pm, bruce barker
<brucebar...@discussions.microsoft.com> wrote:
> 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" wrote:
> > 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
> > }
> > }


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create the Page.Load event code AAaron123 Microsoft C# .NET 4 21st Jan 2009 02:30 PM
Run Javascript function upon Page Load from Page Load event handle Matcon Microsoft ASP .NET 3 28th May 2008 06:15 PM
calling a function without a page load event? darrel Microsoft Dot NET Framework 5 7th Dec 2005 02:33 PM
IE freeze/page will not load after code is run alphanode Microsoft ASP .NET 1 25th Apr 2005 08:10 AM
Calling code-behind function from in-line code page Fred Armitage Microsoft ASP .NET 2 26th Aug 2003 05:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:14 PM.