Buttons and

  • Thread starter Thread starter TarTar
  • Start date Start date
T

TarTar

I have two buttons on my ASP.NET 2.0 form:

<asp:Button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click"
CausesValidation="true" runat="server"></asp:Button>
<asp:Button id="btnCancel" Text="Cancel" OnClick="btnCancel_Click"
CausesValidation="false" runat="server"></asp:Button>

The following HTML code is generated:

<input type="submit" name="btnSubmit" value="Update"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;btnSubmit&quot;, &quot;&quot;, true,
&quot;&quot;, &quot;&quot;, false, false))" id="btnSubmit" />
<input type="submit" name="btnCancel" value="Cancel" id="btnCancel" />

Why the onclick event handler is called for the first button that has
CausesValidation="true", but not for the other one that has
CausesValidation="false"? The CausesValidation property should not have any
influence on the event handler. Yet when I set CausesValidation="true" for
the second button - the onclick event is called.

Thanks,
Leszek
 
That's how Validation is done.
When button has CausesValidation=true then ASP.NET automatically generates
code to run JavaScript validation before submitting the form.

Obviously when CausesValidation=false the button becomes regular submit
button. Since no JavaScript needs to be run.

George.
 
Thanks.

The problem was that the button did not generate postbacks unless
CausesValidation was set to true.
It was a very weird problem indeed. I have solved it. It turned out I had to
use the following tags to include java script code:
<script type="text/javascript" src="grid.js"></script>
instead of
<script language="javascript" src="grid.js" />

Strange - but now the form is working like a charm.

Leszek
 
Back
Top