Stop execution of server side event from client side

Dear All

 I have a text box and i wrote code on the server side event( On Text Change). So when i write something on the text box it goes to server and execute the server side code. Now i want to control this sever execution from client side. i mean i want to cancel this postback from client side under some conition.

can anyone plz help me?

Regards



0
aminulbari
7/10/2007 7:52:30 AM
asp.net.client-side 24353 articles. 2 followers. Follow

22 Replies
5049 Views

Similar Articles

[PageSpeed] 33

Sure if you let us know what you want to be done. here is an example

<asp:textbox id="txtFile" OnTextChanged="javascript:RefreshPage();" Visible=true Width=500 Runat=server/>

function RefreshPage() {
    //Do something
}


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
7/10/2007 8:12:09 AM

THis is my text box

<asp:TextBox ID="TextBox1" runat="server" onchange="CancelPostBack()" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

THis is my server side code

protected void TextBox1_TextChanged(object sender, EventArgs e)

{

//Any code

}

THis is my client script code

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

function CancelPostBack()

{

//for a particular condition i want to cancel the post back here

//But i dont know how to cancel the post back so that the text of the box will chage but will not go to server

}

</script>

 



-1
aminulbari
7/10/2007 9:38:55 AM

Hi then you can just straigtaway use my code, exactly as shown but remove the server side code first.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
-1
naturehermit
7/10/2007 10:04:27 AM

probably i cannot make u understand. I need the server side code also. if i write something on the text box i go to client side on change event. i will check there some condition. if the condition fails then i want to stop going to server and if the condition ok i will let it go to esrver.

Thanks



-1
aminulbari
7/10/2007 10:26:02 AM

Ok...whats the condition, igve an example and then I will show how?

 


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
-1
naturehermit
7/10/2007 10:41:02 AM

condition is numeric value check. The main focus, that i am searching, is what is the client side code to cancel the post back event.



-1
aminulbari
7/10/2007 11:13:56 AM

THis is my client script code

check this way hope this will help you  Smile 

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

function CancelPostBack()

{

if

{

//for a particular condition i want to cancel the post back here

return true;

}

else

{

//But i dont know how to cancel the post back so that the text of the box will chage but will not go to server

return false;

}

 

}

</script>


If this will help you do not forget to "Mark as Answer"

Sanjay D.
0
sanjayd
7/10/2007 11:22:07 AM

ok here you go

<asp:TextBox ID="TextBox1" runat="server" onchange=return confirm (\"Are you sure?\");

AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

THis is my server side code

protected void TextBox1_TextChanged(object sender, EventArgs e)

{

//Any code

}


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
7/10/2007 11:33:22 AM

i dont want any confirmation box. is there any other way?



0
aminulbari
7/10/2007 12:22:20 PM

<script language=JavaScript>
<!--
function cancelPostBack()
{
 if ( Some Condition )
  return true; // Execute the PostBack...

 // Stop the PostBack...
 return false;
}
// -->
</script>

<asp:TextBox ID="TextBox1" runat="server" onchange=return cancelPostBack();");
     AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

 

0
NC01
7/10/2007 1:00:45 PM

aminulbari:

i dont want any confirmation box. is there any other way?

There is another way and that is to handle all code in client side. You can include a hidden input type in page and get the value you want to check on that. In your code behind you can just read those values, and take action ie. to continue with your postback or not.


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
7/10/2007 1:04:12 PM

Not working. I used return true/.false. but it posts back  to server. can anyone give me a project as an attachment?



0
aminulbari
7/11/2007 10:00:23 AM

If you send me yours, I can modify and send it back to you..


Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
0
naturehermit
7/11/2007 10:06:25 AM

Write your existing  java script code and specify what you wnat to do then, I will help you


If this will help you do not forget to "Mark as Answer"

Sanjay D.
0
sanjayd
7/11/2007 10:10:54 AM

I posted a solution to this exact problem for a ListBox here: http://forums.asp.net/p/1131792/1797865.aspx#1797865

The exact same thing applies for a TextBox. Set AutoPostBack to false and set the client-side onchange to a function similar to this:

<script language=JavaScript>
<!--
function cancelPostBack()
{
 if ( Some Condition )
  <%= GetPostBackEventReference(this, string.Empty) %>; // Execute the PostBack...
}
// -->
</script>

Just remember that this will only stop a PostBack by the TextBox, but unless you create some type of mechanism to stop it, the server-side event will be still fired if any other control causes a PostBack.

NC...

0
NC01
7/11/2007 11:55:35 AM

 HI NC.

Its nice and working but the post back event is pending. so for any other post back it also goes to text box change event. so is there any way to protect this pending postback?



0
aminulbari
7/11/2007 12:16:19 PM

Try something like this:

//////////////////////////////////////////////
// In the aspx file
//////////////////////////////////////////////

<form id="Form1" method="post" runat="server">
 <asp:TextBox id="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
 <INPUT type="hidden" id="textBox1IsValidPostBack" name="textBox1IsValidPostBack" runat="server">
</table>
</form>

<script language=JavaScript>
<!--
function textBox1OnChange(elementRef)
{
     document.getElementById('<%= textBox1IsValidPostBack.ClientID %>').value = 'N';

     if ( elementRef.value != '123' )
     {
          document.getElementById('<%= textBox1IsValidPostBack.ClientID %>').value = 'Y';
          <%= GetPostBackEventReference(this, string.Empty) %>; // Execute the PostBack...
     }
}
// -->
</script>

//////////////////////////////////////////////
// In the aspx.cs file
//////////////////////////////////////////////

private void Page_Load(object sender, System.EventArgs e)
{
 this.TextBox1.Attributes.Add("onchange", "textBox1OnChange(this);");
}

protected void TextBox1_TextChanged(object sender, System.EventArgs e)
{
 bool isValidPostBack = (this.textBox1IsValidPostBack.Value == "Y") ? true : false;
 this.textBox1IsValidPostBack.Value = string.Empty;

 if ( !isValidPostBack )
  return;

 this.Response.Write("TextBox1_TextChanged fired<br>");
}

NC...

0
NC01
7/11/2007 12:43:05 PM

Hi Aminul,

From you description, I understand your that concern is: when the client condition is fulfilled, how to prevent the “OnTextChanged” server side event from firing. If there has been any misunderstanding, please let me know.

For this purpose, here is the demo for your reference. The condition is: if the data length in TextBox1 less then 6, the OnTextChanged will not fire. By the way, The TextBox2 in the code is for creating doPostBack script purpose.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
        function CheckPostBack(textbox)
        {
           if(textbox.value.length >= 6)
           {
               __doPostBack(textbox.id,'');
           }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" onchange="CheckPostBack(this)"  OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        </div>
        <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="true" style="display:none"></asp:TextBox>
    </form>
</body>
</html>


************* code behind
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            Response.Write("PostBack");
        }

 


Sincerely,
Benson Yu
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
0
Benson
7/12/2007 8:13:01 AM

Benson,

1. The pending OnTextChanged will still fire if any other control on the page causes a PostBack.
2. What is the hidden TextBox for?
3. Try the following and you'll see what I mean.
4. The code that I posted just before yours is the only real way that I could fix the problem.

<form id="form1" runat="server">
 <div>
  <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="false" onchange="CheckPostBack(this)"  OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
 </div>
 <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="true" style="display:none"></asp:TextBox>
 <div>
  <asp:button id="Button1" runat="server" text="Post Back"></asp:button>
 </div>
</form>

NC...

0
NC01
7/12/2007 11:27:58 AM
Make following changes is in your code behind file page_load event: TextBox1.AutoPostBack =false;TextBox1.Attributes.Add(“onchange”,”JavaScript:CheckValue()”);  

In your ASPX write down following JavaScript function:

 <script language=”javascript”>function CheckValue(){            var txtval = document.all(‘TextBox1’).value; //Write down condition on which you want post back            if (txtval!=’’) // This is your condition{            __doPostBack(‘TextBox1’,’’); // This will do post back for you}else{            //Cheers no post backJ}                        }</script>

 

0
lad
7/13/2007 7:45:12 AM

Hi NC,

You are right. The OnTextChange will still fire for other postback. I didn’t test with a button before. Thank you for pointing it out.

 


Sincerely,
Benson Yu
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
0
Benson
7/13/2007 10:09:12 AM

No problem Benson. FYI, there are 2 ways that I found to stop the pending PostBack. One is the way that I posted with the hidden HTML element, the other is to store the original value in the control and put it back when you cancel the PostBack.

NC...

0
NC01
7/13/2007 1:52:39 PM
Reply:

Similar Artilces:

Firing both Client side event and server side event for server side button
I am having some difficulties getting this to work.. I have button_click (Asp.net) and ClientClick (Javascript).  Javascript is firing fine.  Server script is not. may be you have return return false in the in the javascript which is why its not firing server side event.You should add javascript evelt like thisbutton1.onclientclick = "functionname" Function should nopt return false. Vikram www.vikramlakhotia.comPlease mark the answer if it helped you...

executing my own function @ server side for a client side event
hi all, I have a client side dropdownlist control with onchange event. I am calling form1.submit() method in onchange event. is it possible to catch that event at server side ???ThanksPrashanth.J Not if you call form1.submit(). Try replacing that with <%= ClientScript.GetPostBackEventReference(DropDownList1, string.Empty) %>; Then in the Page_Load handler server-side: private void Page_Load(object sender, System.EventArgs e){ if ( this.IsPostBack ) {  // Place any code that needs to be executed ONLY on a post-back here.  string eventTarget = (this.Req...

Help with client side events Vs. server side events
Hi, Can someone look as the following script and tell me why marking the check box doesn't generate a client side event eventhough there's: onclick="checkClick()"? marked.  Thanks in advance, Sivan <%@ Page Language="c#" %><HTML> <HEAD>  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body>  <SCRIPT language="c#" runat="Server">  void Page_Load(object s, EventArgs e) {  if (!Page.IsPostBack)  {   Label1.Text = Label...

Server-side execution vs. client-side
Hello, I am doing aspx web programming using vb asp.net in MS Visual Studio.  I am trying to make my program to when the user clicks on a button that my program launches another software package that we own (in this case the other software package is called Syteline).  The way that I am trying to accomplish this is via the CreateObject function route.  My program is running on the web server.  While I am sitting on the server that contains this Syteline application, I open up Internet Explorer and launch my web program.  When I click on the button, it gives me ...

Client side to execute server side code
I am maintaining an ASP.NET C# application written by someone else.  I am essentially a newbie to web development especially client side stuff.  Based on reading these forums I understand that I can't directly call a server side method from the client side and I have to use post pack in some way (using a webservice is not an option here).  My confusion comes from the HTML setup being used.The page has three frames; a header, body and footer.  Each frame is filled by it's own ASPX page.  The footer contains buttons for navigating to another page and some of the b...

Client Side events and Server side binding
I want to filter a dataset, server side and bind it to a select control. Problem is my filtering must be done based on the text written by the user in a text input. To get this text i'm using client side keyboard events. I tried to use client side binding and it works fine, but the problem is my dataset has more than 10000 rows, and so the performance is awful. Only thing i can do i think is to rebind the select control to a filtered dataset, improving performance. This is my code at the moment: In the aspx: static HtmlSelect _control = null; [WebMethod(EnableSession = true)] publi...

Client side/server side event race.
 Hi.  I've got a page that 1) Takes some text fields as input2) Does some math calculations on the input (server side via a "calculate" button click)3) Then opens a new window with some detailed information (client side, via a "view report" button click)The server needs to "calculate" before the user can click "view report" so I start with "view report" disabled.  I've got input validation on the "calculate" click, and at the end, I enable the "view report" button.  Everything is working well wi...

Execute the client side and server side code
Hi, Hi, I have two drop down lists one ddl1, ddl2. when ddl1 index is changed I have to change the label before the ddl1 and contents of ddl2. 1) to change the label before ddl2, I am using the javascript like this:ddl1.Attributes.Add("onchange", "changeLabelText(this); return false;"); Javascript function<script language="javascript"> function changeLabelText(ddl) { var indx = ddl.selectedIndex; var lbl = document.getElementById('<%= lblSelect.ClientID %>'); if(indx == 0) lbl.innerHTML = "Select A:"; if(indx == 1)lbl.i...

Server-side or Client-side
In general, I'm wondering if there are any criteria to use when choosing between using a client side element or a server side element. For example, if I'm going to put an HTML table on a page, how do I decide whether or not to use a server control?  Should the server side always be chosen or if there is not explicit server side functionality required is it better to opt for a "normal" client side table? Obviously, if there is a need for server side control the question is answered, I'm looking more at more ambiguous situations. Thanks  This is from Micros...

What is Client Side and Server Side?
I am New of ASP.Net What we can and should do client side and Server Side? What uses? Please any one clear my doubt. Thanking YouEndendrum Priyamudan P m R Clientside is basically what a person can see and serverside is where actions get processed. that's kinda simply puyt but thats the deal, you code serverside, and the html and javascript it generates let's the users for example click a button, then serverside you have coded what should happen when a button gets clicked for example turn text red. Then the server sends a new page with red text to the client (clientside...

client-side and server-side?
hi, can someone tells me how diff is client side and server side are these two? When I tried to do validation ... I would use Javascript ... which has something like: document.forms[0] ... I think that's client side. My understanding on server side is the "form" ... like u will have <form action=".." method=post> ..... </form> ... so it will send to server ... am I on the right track? Are there any other type of server side examples? Hi, ASP.NET itself is server side. Every piece of code processed is done on a server. What comes out of this is sent ...

Server Side or Client Side
Hi I'm a bit confused about this whole server side / client side thing. Are applets, embeded objects consider client side? If I have a form within my aspx page which connects to a database on the server side what is it considered? If I have some business logic written within a VB file when the client access the functions within that business logic (for example some kind of validation function) does the client browser actually downloads that VB file? Can someone suggest how to differentiate between client side and server side components within a asp.net based website?  Hi, i can&#...

Client side and server side
We have a application, where we have some validation on the clientside using EnableClientSideValidation. If you don't have fullscreen you can't see some of the error-messages. What we want is to jump to the place on the page where the message is displayed. One problem is that the button is a serverbutton (runat="server"). How can we accomplish this on the clientside using the same button. I have tried to use "onClientClick" and a link something like this:   <A HREF=http://forums.asp.net/AddPost.aspx?ForumID=18#form>form<...

doing client-side action after server-side event ends
 I have a asp-button;I want it to perform a client-side action after completion of the sever-side action((like:calling a javascript function after the click event of the button ends); You can inject some javascript from the server side button click event so that when the page renders on the client side it will invoke the function or javascript fragment. You can use the method Page.RegisterStartupScript to inject javascript, or you can assign the javascript fragment to a literal control which would be interpreted when the page renders on client side.  Can u add some code snippe...

Web resources about - Stop execution of server side event from client side - asp.net.client-side

Execution of Joseph Wood - Wikipedia, the free encyclopedia
Wood was convicted of two counts of first degree murder and two counts of aggravated assault against a police officer. He was sentenced to death ...

Facebook Ad Issues: Weak Execution, Or Faulty Product?
To the executive class, Facebook looks an awful lot like an old-school media company. That’s because its business model is built on earning revenue ...

SAP Retail Execution on the App Store
Read reviews, compare customer ratings, see screenshots, and learn more about SAP Retail Execution. Download SAP Retail Execution and enjoy it ...

Execution Bed - Flickr - Photo Sharing!
... the microphone; it is why I am here. There are few acts graver than when a government takes the life of one of its own citizens. Executions ...

(Raw Video) Taliban Execution of Two Women in Afghanistan - YouTube
PLEASE THIS IS VERY serious WARNING This video may be disturbing to many and what you are about to see may be hurtful to watch this story is ...

Can Creative Execution Ever Catch Up To Facebook Targeting?
... a max return on the combination of these two investments. Story Based Variation: When you’re considering what kinds of variations in execution ...

Philadelphia: Police Shooter Confesses to 'Execution' for Islam
A gunman ambushed 33-year old Philadelphia police officer Jesse Hartnett on Thursday. His attacker claims it was an "execution" in the name of ...

Saudi Arabia Spending Millions On Public Relations To Spin Executions And Brutality
The Saudis spend millions for years on public relations – it has been reported they spent over $100 million from 2000 to 2010

Five Reasons Most Companies Fail at Strategy Execution
... likely be doomed. By Quy Huy, INSEAD Professor of Strategic Management It’s no longer a secret that most companies struggle with strategy execution. ...

ISIS extremist reportedly kills his mother in public execution in Syria
ISIS extremist reportedly kills his mother in public execution in Syria

Resources last updated: 1/19/2016 8:14:19 PM