Click here to Skip to main content
12,406,342 members (67,878 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
Dear members,

I have written the below code for client side validation,

function isStatValid(btn) {
           var ddlist = document.getElementById('<%=ddlChngStatus.ClientID %>');
           if (ddlist.options[ddlist.selectedIndex].value == 0) {
               alert('Please select status');
               return false;
           }
 
           if ((eval(totDebit) == 0) && (eval(totCredit) == 0)) {
               alert('Please select atleast one template');
               return false;
           }
           else {
               btn.disabled = true;
               btn.value = 'Updating Status...';
               return true;
           }
           return true;
       }

and onclientclick i am calling the above function
<asp:Button ID="btnUpdStat" runat="server" Text="Update Status" Visible="false" usesubmitBehavior="false" OnClientClick="javascript:return isStatValid(this)"/>

this function perform client validation perfectly, but by any reason fails to raise server side button click event.

please suggest some solutions.
Posted 6-Feb-13 4:09am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You must see that you have actually have a server side event for this



<asp:button id="Button2" runat="server" text="Button" onclick="Button2_Click" onclientclick="javascript:return isStatValid(this)" xmlns:asp="#unknown" /> 


This button has 2 events one client side and one server side
First it executes client side event and if that returns true it will execute server side code

protected void Button2_Click(object sender, EventArgs e)
{
 
}


This is the Event Method you will find on CS code page.

In your case there is no server event for the button onclick="Button2_Click"
  Permalink  
Comments
Ankur\m/ 6-Feb-13 10:39am
   
It doesn't matter if a button has an click event attached, it will still postback.
Irbaz Haider Hashmi 6-Feb-13 11:09am
   
It will post back but it will do nothing.
Ankur\m/ 6-Feb-13 11:25am
   
Why not? The page life cycle will execute. All code in Page_Load and other life cycle events will execute.
Irbaz Haider Hashmi 6-Feb-13 11:27am
   
Yes you are right but i think he wants to execute the code particularly specified for the button.
Ankur\m/ 6-Feb-13 12:14pm
   
He has already said that he has event written for that.
This is a known problem. Disabling button before postback, doesn't post the form. Google and you will find many discussion for the same.
vikashbk 6-Feb-13 10:50am
   
Hi Irbaz,

I am using vb.net as code behind language and i have mentioned the server event in code behind file
Irbaz Haider Hashmi 6-Feb-13 11:10am
   
If you are using Visible="false" then the button is not on the page after render. have you set the button visibility true in the code side?
Irbaz Haider Hashmi 6-Feb-13 11:11am
   
Also one more thing remove this usesubmitBehavior="false"
vikashbk 6-Feb-13 12:54pm
   
@irbaz: Yes i have made button visible=true in the code side and even tried by removing usesubmitbehaviour property
Irbaz Haider Hashmi 6-Feb-13 17:30pm
   
Still you are having issue?
Have you attached the debugger?
Does it goes to page load when you hit the button? Does the screen splashes that shows post back? if screen splashes then it must go to Page Load. Attach your debugger there.

It looks like we all have to diagnose the problem. You are working in Visual Studio? Open the design double click the button and then a function will be added. Add your code there.

One more thing is there any update panel on the page?
vikashbk 6-Feb-13 22:01pm
   
i even checked it by attaching debugger.
Yes the screen splashes that shows postback,and i am using this in visual studio,
i have used updatepanel and even tried by specifying trigger for the button both async and postback,But still the problem persists.
Irbaz Haider Hashmi 7-Feb-13 2:40am
   
Ok, Is your button is inside update panel and after it posts back does it changes anything that is outside of update panel? Try doing it by removing update panel.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

It won't because you have disabled the Button before it posts back. Browsers do not allow a disabled button to postback. But there is a nice way of implementing the functionality you want. Dave Ward explains it very well here - Disable a button control during postback.[^]

You may also optionally do an explicit postback after your validation code using - __doPostBack('btnUpdStat','');

Hope this helps!
  Permalink  
Comments
vikashbk 6-Feb-13 10:54am
   
Hi Ankur,
It didnt worked, still server side event doesnt gets filred.
Ankur\m/ 6-Feb-13 11:02am
   
Which option did you try?
vikashbk 6-Feb-13 12:52pm
   
@ankur: i did use the above code, even it gets post back,but my button click event doesnt gets fired
Ankur\m/ 7-Feb-13 0:00am
   
You mean, __doPostback one? See if you are passing the correct name of the button as first parameter.
Did you try the link that I gave you. I have used that before and it has worked pretty well for me.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You have not specified the onclick event for the button.
For example, you need to do something like below...
<asp:button id="btnUpdStat" 
            runat="server"
            Text="Update Status"
            OnClientClick="javascript:return isStatValid(this)"
            onclick="SubmitBtn_Click" />
</asp:button>
void SubmitBtn_Click(object sender, EventArgs e)
{
    Message.Text = "Hello World!";    
}
And remove the usesubmitBehavior="false", it is creating the problem.
It should be true. As the default is true, so no need to specify it.

Refer the examples given in MSDN links -
1. Button.UseSubmitBehavior Property[^].
2. Button.OnClientClick Property[^]
  Permalink  
v3
Comments
Ankur\m/ 6-Feb-13 10:44am
   
Defining click event doesn't really matter. The page still posts.
Tadit Dash 6-Feb-13 13:42pm
   
Yes, exactly.
But I said this as the OP will know what are the prerequisites before calling the button click event.

Still the issue is not solved with all our answers... Very strange problem... :P
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Return False Stoping Button to fire Server side Event.
and there is not server side Method in ur button onClick Event Hmmm | :|
  Permalink  
Comments
Ankur\m/ 6-Feb-13 23:58pm
   
No it doesn't. It will only if while defining the method call from an element you add 'return'. What I mean is: <asp:Button id="btnSubmit" onclick="javascript: return someFunction();" .... />
Now if you return false, it won't postback. Otherwise it will.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100