Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear members,

I have written the below code for client side validation,

C#
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
XML
<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
v2

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



C#
<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

C#
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"
 
Share this answer
 
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.
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!
 
Share this answer
 
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.
You have not specified the onclick event for the button.
For example, you need to do something like below...
XML
<asp:button id="btnUpdStat" 
            runat="server"
            Text="Update Status"
            OnClientClick="javascript:return isStatValid(this)"
            onclick="SubmitBtn_Click" />
</asp:button>

C#
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[^]
 
Share this answer
 
v3
Comments
Ankur\m/ 6-Feb-13 10:44am    
Defining click event doesn't really matter. The page still posts.
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
Return False Stoping Button to fire Server side Event.
and there is not server side Method in ur button onClick Event :|
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900